Spaces:
Sleeping
Sleeping
Commit
·
c071c4c
1
Parent(s):
be62d7f
Update app.py
Browse files
app.py
CHANGED
|
@@ -38,27 +38,106 @@
|
|
| 38 |
#iface.launch()
|
| 39 |
|
| 40 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
def get_key(k):
|
| 45 |
-
global key
|
| 46 |
-
key = k
|
| 47 |
-
return "Key saved successfully!"
|
| 48 |
|
| 49 |
-
def greet(k,name):
|
| 50 |
-
global key
|
| 51 |
-
key = k
|
| 52 |
-
return "Hello " + name + "!!"
|
| 53 |
|
| 54 |
-
key_input = gr.inputs.Textbox(label="Enter a key")
|
| 55 |
-
key_button = gr.Button(label="Save Key", type="submit", onclick=get_key(key_input))
|
| 56 |
|
| 57 |
-
name_input = gr.inputs.Textbox(label="Enter a name")
|
| 58 |
-
greet_button = gr.Button(label="Greet", type="submit", onclick_args={"name": name_input})
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
iface.launch()
|
|
|
|
| 38 |
#iface.launch()
|
| 39 |
|
| 40 |
import gradio as gr
|
| 41 |
+
import openai
|
| 42 |
+
|
| 43 |
+
openai.api_key= = ""
|
| 44 |
+
|
| 45 |
+
def get_completion(prompt, model="gpt-3.5-turbo"):
|
| 46 |
+
messages = [{"role": "user", "content": prompt}]
|
| 47 |
+
response = openai.ChatCompletion.create(
|
| 48 |
+
model=model,
|
| 49 |
+
messages=messages,
|
| 50 |
+
temperature=0, # 控制模型输出的随机程度
|
| 51 |
+
)
|
| 52 |
+
return response.choices[0].message["content"]
|
| 53 |
+
|
| 54 |
+
def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0):
|
| 55 |
+
response = openai.ChatCompletion.create(
|
| 56 |
+
model=model,
|
| 57 |
+
messages=messages,
|
| 58 |
+
temperature=temperature, # 控制模型输出的随机程度
|
| 59 |
+
)
|
| 60 |
+
# print(str(response.choices[0].message))
|
| 61 |
+
return response.choices[0].message["content"]
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
context2 = [{'role':'system', 'content':"""
|
| 65 |
+
你是订餐机器人,为披萨餐厅自动收集订单信息。\n
|
| 66 |
+
注意如果客户来了,发送了空消息,请主动与客户说你好。\n
|
| 67 |
+
你要首先问候顾客,并主动告知客户今天的菜单,询问客户今天要什么食物。\n
|
| 68 |
+
然后等待用户回复收集订单信息。收集完信息需确认顾客是否还需要添加其他内容。\n
|
| 69 |
+
最后需要询问是否自取或外送,如果是外送,你要询问地址。\n
|
| 70 |
+
最后告诉顾客订单总金额,并送上祝福。\n
|
| 71 |
+
\n
|
| 72 |
+
请确保明确所有选项、附加项和尺寸,以便从菜单中识别出该项唯一的内容。\n
|
| 73 |
+
你的回应应该以简短、非常随意和友好的风格呈现。\n
|
| 74 |
+
\n
|
| 75 |
+
菜单包括:\n
|
| 76 |
+
\n
|
| 77 |
+
菜品:\n
|
| 78 |
+
意式辣香肠披萨(大、中、小) 12.95、10.00、7.00\n
|
| 79 |
+
芝士披萨(大、中、小) 10.95、9.25、6.50\n
|
| 80 |
+
茄子披萨(大、中、小) 11.95、9.75、6.75\n
|
| 81 |
+
薯条(大、小) 4.50、3.50\n
|
| 82 |
+
希腊沙拉 7.25\n
|
| 83 |
+
\n
|
| 84 |
+
配料:\n
|
| 85 |
+
奶酪 2.00\n
|
| 86 |
+
蘑菇 1.50\n
|
| 87 |
+
香肠 3.00\n
|
| 88 |
+
加拿大熏肉 3.50\n
|
| 89 |
+
AI酱 1.50\n
|
| 90 |
+
辣椒 1.00\n
|
| 91 |
+
\n
|
| 92 |
+
饮料:\n
|
| 93 |
+
可乐(大、中、小) 3.00、2.00、1.00\n
|
| 94 |
+
雪碧(大、中、小) 3.00、2.00、1.00\n
|
| 95 |
+
瓶装水 5.00\n
|
| 96 |
+
"""} ] # accumulate messages
|
| 97 |
|
| 98 |
+
#def get_key(k):
|
| 99 |
+
# global key
|
| 100 |
+
# key = k
|
| 101 |
+
# return "Key saved successfully!"
|
| 102 |
+
|
| 103 |
+
#def greet(k,name):
|
| 104 |
+
# openai.api_key = k
|
| 105 |
+
# return "Hello " + name + "!!"
|
| 106 |
+
|
| 107 |
+
#key_input = gr.inputs.Textbox(label="Enter a key")
|
| 108 |
+
#key_button = gr.Button(label="Save Key", type="submit", onclick=get_key(key_input))
|
| 109 |
+
|
| 110 |
+
#name_input = gr.inputs.Textbox(label="Enter a name")
|
| 111 |
+
#greet_button = gr.Button(label="Greet", type="submit", onclick_args={"name": name_input})
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
|
|
|
|
|
|
| 115 |
|
|
|
|
|
|
|
| 116 |
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
#iface = gr.Interface(fn=greet, inputs=[key_input, name_input], outputs="text",
|
| 120 |
+
# inputs_layout="vertical", outputs_layout="vertical",
|
| 121 |
+
# title="Greeting App", description="Enter a key and a name to greet")
|
| 122 |
+
|
| 123 |
+
#iface.launch()
|
| 124 |
+
|
| 125 |
+
key_input = gr.inputs.Textbox(label="Enter a key")
|
| 126 |
+
chat_input = gr.inputs.Textbox(label="Enter a chattext")
|
| 127 |
+
|
| 128 |
+
def chatbot_interface_restaurant_chinese(k_text,t_text):
|
| 129 |
+
openai.api_key = k_text
|
| 130 |
+
if not hasattr(chatbot_interface, "chat_history"):
|
| 131 |
+
chatbot_interface.chat_history = ""
|
| 132 |
+
context2.append({'role':'user', 'content':f"{t_text}"})
|
| 133 |
+
chatbot_interface.chat_history += "User: " + t_text + "\n"
|
| 134 |
+
output_text = get_completion_from_messages(context2)
|
| 135 |
+
context2.append({'role':'assistant', 'content':f"{output_text}"})
|
| 136 |
+
chatbot_interface.chat_history += "assistant: " + output_text + "\n\n"
|
| 137 |
+
return chatbot_interface.chat_history
|
| 138 |
+
|
| 139 |
+
iface = gr.Interface(fn=greet, inputs=[key_input, chat_input], outputs="text",
|
| 140 |
+
inputs_layout="vertical", outputs_layout="vertical",
|
| 141 |
+
title="Greeting App", description="Enter a key and a name to greet")
|
| 142 |
|
| 143 |
iface.launch()
|