| import gradio as gr |
| import openai |
|
|
| openai.api_key = open("key.txt", "r").read().strip("\n") |
|
|
| |
| 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): |
| |
| message_history.append({"role": "user", "content": f"{input}"}) |
|
|
| completion = openai.ChatCompletion.create( |
| model="gpt-3.5-turbo", |
| messages=message_history |
| ) |
| |
| reply_content = completion.choices[0].message.content |
|
|
| message_history.append({"role": "assistant", "content": f"{reply_content}"}) |
| |
| |
| response = [(message_history[i]["content"], message_history[i+1]["content"]) for i in range(2, len(message_history)-1, 2)] |
| return response |
|
|
| |
| with gr.Blocks() as demo: |
|
|
| |
| chatbot = gr.Chatbot() |
|
|
| |
| with gr.Row(): |
| |
| txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False) |
|
|
| |
| txt.submit(predict, txt, chatbot) |
| |
| txt.submit(None, None, txt, _js="() => {''}") |
| |
| demo.launch() |