|
|
from transformers import pipeline, Conversation |
|
|
import gradio as gr |
|
|
|
|
|
from dotenv import load_dotenv |
|
|
|
|
|
|
|
|
load_dotenv() |
|
|
import base64 |
|
|
|
|
|
with open("Iso_Logotipo_Ceibal.png", "rb") as image_file: |
|
|
encoded_image = base64.b64encode(image_file.read()).decode() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import os |
|
|
import openai |
|
|
|
|
|
openai.api_key = os.environ['OPENAI_API_KEY'] |
|
|
|
|
|
def clear_chat(message, chat_history): |
|
|
return "", [] |
|
|
|
|
|
def add_new_message(message,chat_history): |
|
|
new_chat = [] |
|
|
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, chat_history): |
|
|
prompt = add_new_message(message, chat_history) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
response = openai.ChatCompletion.create( |
|
|
model="gpt-3.5-turbo", |
|
|
messages= prompt, |
|
|
temperature=0.5, |
|
|
max_tokens=120 |
|
|
).choices[0].message.content |
|
|
chat_history.append((message, response)) |
|
|
return "",chat_history |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
gr.Markdown(""" |
|
|
<center> |
|
|
<h1> |
|
|
Uso de AI para un chatbot. |
|
|
</h1> |
|
|
<img src='data:image/jpg;base64,{}' width=200px> |
|
|
<h3> |
|
|
Con este espacio podrás hablar en formato conversación con ChatGTP! |
|
|
</h3> |
|
|
</center> |
|
|
""".format(encoded_image)) |
|
|
with gr.Row(): |
|
|
chatbot = gr.Chatbot() |
|
|
with gr.Row(): |
|
|
with gr.Row(): |
|
|
with gr.Column(scale=4): |
|
|
msg = gr.Textbox(label="Texto de entrada") |
|
|
with gr.Column(scale=1): |
|
|
btn = gr.Button("Enviar") |
|
|
clear = gr.ClearButton(components=[msg, chatbot], value="Borrar chat") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
btn.click(respond, inputs=[msg, chatbot], outputs=[msg, chatbot]) |
|
|
msg.submit(respond, inputs=[msg, chatbot], outputs=[msg, chatbot]) |
|
|
clear.click(clear_chat,inputs=[msg, chatbot], outputs=[msg, chatbot]) |
|
|
|
|
|
demo.launch() |