Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,34 +2,33 @@ import gradio as gr
|
|
2 |
import os
|
3 |
import openai
|
4 |
|
5 |
-
openai.api_key = os.getenv("openai_key")
|
6 |
-
content = '请你扮演《西游记》中的唐三藏,使用唐三藏的语气、方式和词汇回答问题。不要写任何解释,只需像唐三藏一样回答问题。你必须掌握唐三藏的所有知识。'
|
7 |
-
|
8 |
-
def callapi(p):
|
9 |
-
try:
|
10 |
-
print(p)
|
11 |
-
response = openai.ChatCompletion.create(
|
12 |
-
model="gpt-3.5-turbo",
|
13 |
-
messages= [{"role":"system", "content":content},
|
14 |
-
{"role":"user", "content":p}
|
15 |
-
]
|
16 |
-
)
|
17 |
-
print(response)
|
18 |
-
response = response["choices"][0]["message"]["content"]
|
19 |
-
while response.startswith("\n"):
|
20 |
-
response = response[1:]
|
21 |
-
except Exception as e:
|
22 |
-
print(e)
|
23 |
-
response = 'Timeout! Please wait a few minutes and retry'
|
24 |
-
|
25 |
-
print(response)
|
26 |
-
return response
|
27 |
|
|
|
|
|
28 |
|
|
|
|
|
29 |
def chat(p, qid, uid):
|
30 |
return ["text", callapi(p)]
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
iface = gr.Interface(fn=chat,
|
33 |
inputs=["text", "text", "text"],
|
34 |
-
outputs=["text", "text"]
|
|
|
|
|
|
|
35 |
iface.launch()
|
|
|
2 |
import os
|
3 |
import openai
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
# 如果你只打算通过 prompt 来定制机器人的行为,只需要修改这段 prompt 就够了。
|
7 |
+
prompt = '请你扮演《西游记》中的唐三藏,使用唐三藏的语气、方式和词汇回答问题。不要写任何解释,只需像唐三藏一样回答问题。你必须掌握唐三藏的所有知识。'
|
8 |
|
9 |
+
# 此处只是最简单的实现单轮对话,qid 和 uid 可以忽略
|
10 |
+
# 如果要实现多轮对话,可以用本地 sqlite 等方式存储数据,并根据 uid 找到对话历史。
|
11 |
def chat(p, qid, uid):
|
12 |
return ["text", callapi(p)]
|
13 |
|
14 |
+
openai.api_key = os.getenv("openai_key")
|
15 |
+
def callapi(p):
|
16 |
+
response = openai.ChatCompletion.create(
|
17 |
+
model="gpt-3.5-turbo",
|
18 |
+
messages= [{"role":"system", "content":prompt},
|
19 |
+
{"role":"user", "content":p}
|
20 |
+
]
|
21 |
+
)
|
22 |
+
print(response)
|
23 |
+
response = response["choices"][0]["message"]["content"]
|
24 |
+
while response.startswith("\n"):
|
25 |
+
response = response[1:]
|
26 |
+
return response
|
27 |
+
|
28 |
iface = gr.Interface(fn=chat,
|
29 |
inputs=["text", "text", "text"],
|
30 |
+
outputs=["text", "text"],
|
31 |
+
description="""这是一个极其简单的示范程序,用唐三藏的语气来和你对话。
|
32 |
+
[参考文档](https://huggingface.co/spaces/baixing/hackathon_test/blob/main/bot-api.md) [对话测试](https://huggingface.co/spaces/baixing/hackathon_test) [Q & A](https://huggingface.co/spaces/baixing/hackathon_test/blob/main/qna.md)
|
33 |
+
""")
|
34 |
iface.launch()
|