Spaces:
Runtime error
Runtime error
File size: 3,777 Bytes
dd05464 269642a dd05464 948174f dd05464 ee6fbcc 948174f dd05464 948174f dd05464 948174f ee6fbcc 948174f dd05464 948174f dd05464 948174f dd05464 ee6fbcc dd05464 ee6fbcc dd05464 ee6fbcc dd05464 ee6fbcc dd05464 ee6fbcc dd05464 ee6fbcc dd05464 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
import gradio as gr
import os
import time
import openai
import json
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']
print(json_data)
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+'Based on the above dialogue and menu, If you were a waiter, what would you answer next?'
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)
question = ""
for each_ques in history:
question = question+ each_ques[0].replace("Customer: ","")+" \n"
response = csv_prompter(question)
response = invitation+ response
history[-1][1] = response
return history
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot, chatbot, chatbot
)
clear.click(lambda: None, None, chatbot, queue=False).then(restart, chatbot, chatbot)
Initialization.click(lambda: None, None, chatbot, queue=False).then(restart, chatbot, chatbot)
if __name__ == "__main__":
demo.launch()
|