Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
|
| 4 |
+
openai.api_key = open("key.txt", "r").read().strip("\n")
|
| 5 |
+
|
| 6 |
+
# Se almacena el historial de mensajes entre el usuario y el chat.
|
| 7 |
+
message_history = [{"role": "user", "content": f"You are a bookseller bot that knows all the books in the world and their categories. I will specify the name of the book and its corresponding SKU in my messages and you will respond with the category and subcategory to which the book that I mention in my messages belongs to. Always answer category and subcategory. Give me the categories and subcategories in Spanish. If you understand, say OK."},
|
| 8 |
+
{"role": "assistant", "content": f"OK"}]
|
| 9 |
+
|
| 10 |
+
def predict(input):
|
| 11 |
+
# tokenizar la nueva oraci贸n de entrada
|
| 12 |
+
message_history.append({"role": "user", "content": f"{input}"})
|
| 13 |
+
|
| 14 |
+
completion = openai.ChatCompletion.create(
|
| 15 |
+
model="gpt-3.5-turbo", # $0.002 por 1k tokens
|
| 16 |
+
messages=message_history
|
| 17 |
+
)
|
| 18 |
+
# Respuesta:
|
| 19 |
+
reply_content = completion.choices[0].message.content
|
| 20 |
+
|
| 21 |
+
message_history.append({"role": "assistant", "content": f"{reply_content}"})
|
| 22 |
+
|
| 23 |
+
#Almacenamos la preguntas y respuestas en una lista
|
| 24 |
+
response = [(message_history[i]["content"], message_history[i+1]["content"]) for i in range(2, len(message_history)-1, 2)]
|
| 25 |
+
return response
|
| 26 |
+
|
| 27 |
+
# Creamos una interfaz para el caht con Gradio
|
| 28 |
+
with gr.Blocks() as demo:
|
| 29 |
+
|
| 30 |
+
# crea la instancia de un chatbot que se utilizar谩 para procesar las entradas de texto del usuario
|
| 31 |
+
chatbot = gr.Chatbot()
|
| 32 |
+
|
| 33 |
+
# crea un nuevo componente Fila, que es un contenedor para otros componentes.
|
| 34 |
+
with gr.Row():
|
| 35 |
+
# crea un cuadro de texto donde los usuarios pueden escribir mensajes.
|
| 36 |
+
txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False)
|
| 37 |
+
|
| 38 |
+
# esta funci贸n procesa la entrada y genera una respuesta del chat
|
| 39 |
+
txt.submit(predict, txt, chatbot) # submit(function, input, output)
|
| 40 |
+
|
| 41 |
+
txt.submit(None, None, txt, _js="() => {''}") # No function, no input to that function, submit action to textbox is a js function that returns empty string, so it clears immediately.
|
| 42 |
+
|
| 43 |
+
demo.launch()
|