Spaces:
Sleeping
Sleeping
mrolando
commited on
Commit
·
db23382
1
Parent(s):
b770563
first commit
Browse files- .gitignore +2 -0
- Iso_Logotipo_Ceibal.png +0 -0
- app.py +117 -0
- requirements.txt +6 -0
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.env
|
| 2 |
+
env
|
Iso_Logotipo_Ceibal.png
ADDED
|
app.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import openai
|
| 3 |
+
from transformers import pipeline, Conversation
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import json
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
|
| 8 |
+
# Load environment variables from the .env file de forma local
|
| 9 |
+
load_dotenv()
|
| 10 |
+
import base64
|
| 11 |
+
|
| 12 |
+
with open("Iso_Logotipo_Ceibal.png", "rb") as image_file:
|
| 13 |
+
encoded_image = base64.b64encode(image_file.read()).decode()
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
openai.api_key = os.environ['OPENAI_API_KEY']
|
| 17 |
+
|
| 18 |
+
def clear_chat(message, chat_history):
|
| 19 |
+
return "", []
|
| 20 |
+
|
| 21 |
+
def add_new_message(message,topic,age_range, chat_history):
|
| 22 |
+
new_chat = []
|
| 23 |
+
|
| 24 |
+
new_chat.append({"role": "system", "content": 'Sos encargado de hacer preguntas y llevar los puntos de un juego. Es un juego de preguntas y respuestas, tú tienes que hacer las preguntas del tema {} para un rango de edad de {}. Empieza con las preguntas desde el comienzo y haz 1 pregunta por vez, hasta 4 preguntas para finalmente dar el resultado final. Da 3 opciones de respuesta por cada pregunta.'.format(topic,age_range)})
|
| 25 |
+
|
| 26 |
+
for turn in chat_history:
|
| 27 |
+
user, bot = turn
|
| 28 |
+
new_chat.append({"role": "user", "content": user})
|
| 29 |
+
new_chat.append({"role": "assistant","content":bot})
|
| 30 |
+
new_chat.append({"role": "user","content":message})
|
| 31 |
+
return new_chat
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def respond(message, person,age_range, chat_history):
|
| 36 |
+
prompt = add_new_message(message, person, age_range, chat_history)
|
| 37 |
+
response = openai.ChatCompletion.create(
|
| 38 |
+
model="gpt-3.5-turbo",
|
| 39 |
+
messages= prompt,
|
| 40 |
+
temperature=0.5,
|
| 41 |
+
max_tokens=1000,
|
| 42 |
+
stream=True,
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
token_counter = 0
|
| 46 |
+
partial_words = ""
|
| 47 |
+
|
| 48 |
+
counter=0
|
| 49 |
+
for chunk in response:
|
| 50 |
+
chunk_message = chunk['choices'][0]['delta']
|
| 51 |
+
if(len(chat_history))<1:
|
| 52 |
+
# print("entró acaá")
|
| 53 |
+
partial_words += chunk_message.content
|
| 54 |
+
chat_history.append([message,chunk_message.content])
|
| 55 |
+
else:
|
| 56 |
+
# print("antes", chat_history)
|
| 57 |
+
if(len(chunk_message)!=0):
|
| 58 |
+
if(len(chunk_message)==2):
|
| 59 |
+
partial_words += chunk_message.content
|
| 60 |
+
chat_history.append([message,chunk_message.content])
|
| 61 |
+
else:
|
| 62 |
+
partial_words += chunk_message.content
|
| 63 |
+
chat_history[-1] =([message,partial_words])
|
| 64 |
+
yield "",chat_history
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def start( person,age_range, chat_history):
|
| 69 |
+
message= "Quiero empezar!"
|
| 70 |
+
yield "",respond(message, person,age_range, chat_history)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
with gr.Blocks() as demo:
|
| 76 |
+
gr.Markdown("""
|
| 77 |
+
<center>
|
| 78 |
+
<h1>
|
| 79 |
+
Uso de AI para un juego de preguntas.
|
| 80 |
+
</h1>
|
| 81 |
+
<img src='data:image/jpg;base64,{}' width=200px>
|
| 82 |
+
<h3>
|
| 83 |
+
Con este espacio podrás jugar a responder preguntas de manera correcta a un tema y sumar puntos!
|
| 84 |
+
</h3>
|
| 85 |
+
</center>
|
| 86 |
+
""".format(encoded_image))
|
| 87 |
+
with gr.Row():
|
| 88 |
+
topic = gr.Textbox(label="Escribí el tema:")
|
| 89 |
+
choice_age = gr.Radio(
|
| 90 |
+
[
|
| 91 |
+
("<12", "menores de 12 años"),
|
| 92 |
+
("12-15", "entre 12 y 15 años"),
|
| 93 |
+
("15-18", "entre 15 y 18 años"),
|
| 94 |
+
(">18" ,"mayores de 18"),
|
| 95 |
+
],
|
| 96 |
+
label="Cuál es tu edad?",
|
| 97 |
+
)
|
| 98 |
+
start_btn = gr.Button("Quiero comenzar!")
|
| 99 |
+
with gr.Row():
|
| 100 |
+
chatbot = gr.Chatbot( height=550) #just to fit the notebook
|
| 101 |
+
with gr.Row():
|
| 102 |
+
with gr.Row():
|
| 103 |
+
with gr.Column(scale=4):
|
| 104 |
+
msg = gr.Textbox(label="Texto de entrada", value="Empecemos")
|
| 105 |
+
with gr.Column(scale=1):
|
| 106 |
+
btn = gr.Button("Enviar")
|
| 107 |
+
clear = gr.ClearButton(components=[msg, chatbot], value="Borrar chat")
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
btn.click(respond, inputs=[msg,topic,choice_age, chatbot], outputs=[msg, chatbot])
|
| 113 |
+
msg.submit(respond, inputs=[msg, topic,choice_age,chatbot], outputs=[msg, chatbot]) #Press enter to submit
|
| 114 |
+
clear.click(clear_chat,inputs=[msg, chatbot], outputs=[msg, chatbot])
|
| 115 |
+
start_btn.click(respond, inputs=[msg,topic,choice_age, chatbot], outputs=[msg, chatbot])
|
| 116 |
+
demo.queue()
|
| 117 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
einops
|
| 2 |
+
openai==0.28
|
| 3 |
+
gradio
|
| 4 |
+
transformers
|
| 5 |
+
python-dotenv
|
| 6 |
+
torch
|