order_demo / app.py
kenton-li's picture
Update app.py
6de1c2c
import gradio as gr
import os
import time
import openai
import json
import itertools
openai_api_key_textbox = ""
model = None
tokenizer = None
generator = None
openai.api_key = "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"
def csv_prompter(question):
json_file = open('order.json')
json_data = json.load(json_file)
json_data = json_data['records']
fulltext = []
#print all nm in json file
for i in json_data:
# identify if opts in this item
opt_list = []
if 'opt' in i:
for opt1 in i['opt']:
for opt2 in opt1['opts']:
opt_list.append(opt2['nm'])
if len(opt_list) > 100:
print(str(i['pid'])+" "+i['nm']+" Options:"+str(opt_list))
each = i['nm']+" Options:"+str(opt_list)
fulltext.append(each)
else:
print(str(i['pid'])+" "+i['nm'])
each = i['nm']
fulltext.append(each)
fulltext = '\n'.join(fulltext)
#fulltext = fulltext + "\n 00:00 - Customer: Hey, dear, can I please take out? 00:03 - Waiter: I'm up here. Okay go ahead. 00:06 - Customer: Can I get two sesame chicken dinners? 00:10 - Customer: One with lo mein and one with fried rice? 00:13 - Customer: Can I get, I know spring rolls come with each of those, but can I get just as long 00:17 - Customer: as I've got a total of two spring rolls and two egg rolls? 00:21 - Waiter: Okay. Anything? 00:22 - Customer: And a small bowl of hot and sour soup. 00:25 - Customer: And that's it. 00:26 - Waiter: Alright. Two sesame chickens, small one with fried rice, small one with lo mein, and both of those with spring rolls, two egg rolls and a small hot and sour soup. 00:30 - Customer: It's 205-473-1750. 00:36 - Waiter: What's the fun number please? 00:43 - Waiter: Just got around 20 minutes. Thank you. 00:43 - Customer: Okay, dear, thank you so much."
fulltext = fulltext+'\n'
fulltext = fulltext+'\n\n'+question
print(fulltext)
messages = [
{"role": "system", "content": ""},
]
messages.append(
{"role": "user", "content": f"{fulltext}"}
)
rsp = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages
)
response = rsp.get("choices")[0]["message"]["content"]
print()
print(response)
return response
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.Button("Clear")
Initialization = gr.Button("Initialization")
def restart(history):
invitation = "Waiter: "
human_invitation = "Customer: "
return [["\n",invitation+"Taste of China, may I help you?"]]
def user(user_message, history):
invitation = "Waiter: "
human_invitation = "Customer: "
return "", history +[[human_invitation+user_message, None]]
def bot(history):
invitation = "Waiter: "
human_invitation = "Customer: "
print(history)
alist = list(itertools.chain.from_iterable(history))
alist=filter(None, alist)
fulltext = "Based on the above menu and following dialogue, If you were a waiter, what would you say next?\n"+ "\n".join(alist) + "\n" + invitation
response = csv_prompter(fulltext)
response = invitation+ response
history[-1][1] = response
return history
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot, chatbot, chatbot
)
Initialization.click(lambda: None, None, chatbot, queue=False).then(restart, chatbot, chatbot)
clear.click(lambda: None, None, chatbot, queue=False).then(restart, chatbot, chatbot)
if __name__ == "__main__":
demo.launch()