Download app.py from Edxaidys/BUKZ: direct link, hf CLI and curl.
- Browser
- Download file 2.17 kB
-
https://huggingface.co/spaces/Edxaidys/BUKZ/resolve/main/app.py
- Command line
-
hf download hf://spaces/Edxaidys/BUKZ/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Edxaidys/BUKZ/resolve/main/app.py
2.17 kB
| import gradio as gr | |
| import openai | |
| openai.api_key = open("key.txt", "r").read().strip("\n") | |
| # Se almacena el historial de mensajes entre el usuario y el chat. | |
| 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."}, | |
| {"role": "assistant", "content": f"OK"}] | |
| def predict(input): | |
| # tokenizar la nueva oración de entrada | |
| message_history.append({"role": "user", "content": f"{input}"}) | |
| completion = openai.ChatCompletion.create( | |
| model="gpt-3.5-turbo", # $0.002 por 1k tokens | |
| messages=message_history | |
| ) | |
| # Respuesta: | |
| reply_content = completion.choices[0].message.content | |
| message_history.append({"role": "assistant", "content": f"{reply_content}"}) | |
| #Almacenamos la preguntas y respuestas en una lista | |
| response = [(message_history[i]["content"], message_history[i+1]["content"]) for i in range(2, len(message_history)-1, 2)] | |
| return response | |
| # Creamos una interfaz para el caht con Gradio | |
| with gr.Blocks() as demo: | |
| # crea la instancia de un chatbot que se utilizará para procesar las entradas de texto del usuario | |
| chatbot = gr.Chatbot() | |
| # crea un nuevo componente Fila, que es un contenedor para otros componentes. | |
| with gr.Row(): | |
| # crea un cuadro de texto donde los usuarios pueden escribir mensajes. | |
| txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False) | |
| # esta función procesa la entrada y genera una respuesta del chat | |
| txt.submit(predict, txt, chatbot) # submit(function, input, output) | |
| 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. | |
| demo.launch() |