Spaces:
Sleeping
Sleeping
| import os | |
| import openai | |
| from transformers import pipeline, Conversation | |
| import gradio as gr | |
| import json | |
| from dotenv import load_dotenv | |
| # Load environment variables from the .env file de forma local | |
| load_dotenv() | |
| import base64 | |
| with open("Iso_Logotipo_Ceibal.png", "rb") as image_file: | |
| encoded_image = base64.b64encode(image_file.read()).decode() | |
| openai.api_key = os.environ['OPENAI_API_KEY'] | |
| def clear_chat(message, chat_history): | |
| return "", [] | |
| def add_new_message(message,topic,age_range, chat_history): | |
| new_chat = [] | |
| new_chat.append({"role": "system", "content": 'La idea es que te comportes como juego de chat de preguntas y respuestas. También deberás contabilizar los puntos del juego. Es un juego de preguntas y respuestas sobre el tema {} para estudiantes {}. Empieza con la introducción del juego y la primera pregunta. Es muy importante que solo hagas una pregunta por vez, así el usuario puede ir respondiendo una a una las preguntas. Será un total de 4 preguntas y al finalizar debes indicar el resultado final. Cada respuesta correcta vale 10 puntos y debes dar 3 opciones de respuesta por cada pregunta.'.format(topic,age_range)}) | |
| for turn in chat_history: | |
| user, bot = turn | |
| new_chat.append({"role": "user", "content": user}) | |
| new_chat.append({"role": "assistant","content":bot}) | |
| new_chat.append({"role": "user","content":message}) | |
| return new_chat | |
| def respond(message, person,age_range, chat_history): | |
| prompt = add_new_message(message, person, age_range, chat_history) | |
| response = openai.ChatCompletion.create( | |
| model="gpt-4-0125-preview", | |
| messages= prompt, | |
| temperature=0.5, | |
| max_tokens=1000, | |
| stream=True, | |
| ) | |
| token_counter = 0 | |
| partial_words = "" | |
| counter=0 | |
| for chunk in response: | |
| chunk_message = chunk['choices'][0]['delta'] | |
| if(len(chat_history))<1: | |
| # print("entró acaá") | |
| partial_words += chunk_message.content | |
| chat_history.append([message,chunk_message.content]) | |
| else: | |
| # print("antes", chat_history) | |
| if(len(chunk_message)!=0): | |
| if(len(chunk_message)==2): | |
| partial_words += chunk_message.content | |
| chat_history.append([message,chunk_message.content]) | |
| else: | |
| partial_words += chunk_message.content | |
| chat_history[-1] =([message,partial_words]) | |
| yield "",chat_history | |
| def start( person,age_range, chat_history): | |
| message= "Quiero empezar!" | |
| yield "",respond(message, person,age_range, chat_history) | |
| with gr.Blocks() as demo: | |
| gr.Markdown(""" | |
| <center> | |
| <img src='data:image/jpg;base64,{}' width=200px> | |
| <h3> | |
| En este espacio podrás jugar a responder preguntas sobre el tema que te guste y sumar puntos! | |
| </h3> | |
| </center> | |
| """.format(encoded_image)) | |
| with gr.Row(): | |
| topic = gr.Textbox(label="Escribí el tema:") | |
| choice_age = gr.Radio( | |
| [ | |
| ("<12", "menores de 12 años"), | |
| ("12-15", "entre 12 y 15 años"), | |
| ("15-18", "entre 15 y 18 años"), | |
| (">18" ,"mayores de 18"), | |
| ], | |
| label="Selecciona según tu edad:", | |
| ) | |
| start_btn = gr.Button("¡Quiero comenzar!") | |
| with gr.Row(): | |
| chatbot = gr.Chatbot( height=250) #just to fit the notebook | |
| with gr.Row(): | |
| with gr.Row(): | |
| with gr.Column(scale=4): | |
| msg = gr.Textbox(label="Texto de entrada", value="Empecemos") | |
| with gr.Column(scale=1): | |
| btn = gr.Button("Enviar") | |
| clear = gr.ClearButton(components=[msg, chatbot], value="Borrar chat") | |
| btn.click(respond, inputs=[msg,topic,choice_age, chatbot], outputs=[msg, chatbot]) | |
| msg.submit(respond, inputs=[msg, topic,choice_age,chatbot], outputs=[msg, chatbot]) #Press enter to submit | |
| clear.click(clear_chat,inputs=[msg, chatbot], outputs=[msg, chatbot]) | |
| start_btn.click(respond, inputs=[msg,topic,choice_age, chatbot], outputs=[msg, chatbot]) | |
| demo.queue() | |
| demo.launch() |