Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,10 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
# 修改本函数,来实现你自己的 chatbot
|
4 |
# p: 对机器人说话的内容
|
@@ -7,13 +13,27 @@ import gradio as gr
|
|
7 |
# 返回值:[type, content]
|
8 |
# 详见 https://huggingface.co/spaces/baixing/hackathon_test/blob/main/bot-api.md
|
9 |
def chat(p, qid, uid):
|
10 |
-
return ["text",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
iface = gr.Interface(fn=chat,
|
13 |
inputs=["text", "text", "text"],
|
14 |
outputs=["text", "text"],
|
15 |
-
description="""
|
16 |
-
|
17 |
[对话测试](https://huggingface.co/spaces/BaixingAI/hackathon_test) [参考文档](https://huggingface.co/spaces/baixing/hackathon_test/blob/main/bot-api.md) [Q & A](https://huggingface.co/spaces/baixing/hackathon_test/blob/main/qna.md)
|
18 |
""")
|
19 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
+
import openai
|
4 |
+
|
5 |
+
|
6 |
+
# 如果你只打算通过 prompt 来定制机器人的行为,只需要修改这段 prompt 就够了。
|
7 |
+
prompt = '请你扮演《西游记》中的唐三藏,使用唐三藏的语气、方式和词汇回答问题。不要写任何解释,只需像唐三藏一样回答问题。你必须掌握唐三藏的所有知识。'
|
8 |
|
9 |
# 修改本函数,来实现你自己的 chatbot
|
10 |
# p: 对机器人说话的内容
|
|
|
13 |
# 返回值:[type, content]
|
14 |
# 详见 https://huggingface.co/spaces/baixing/hackathon_test/blob/main/bot-api.md
|
15 |
def chat(p, qid, uid):
|
16 |
+
return ["text", callapi(p)]
|
17 |
+
|
18 |
+
openai.api_key = os.getenv("openai_key")
|
19 |
+
def callapi(p):
|
20 |
+
response = openai.ChatCompletion.create(
|
21 |
+
model="gpt-3.5-turbo",
|
22 |
+
messages= [{"role":"system", "content":prompt},
|
23 |
+
{"role":"user", "content":p}
|
24 |
+
]
|
25 |
+
)
|
26 |
+
print(response)
|
27 |
+
response = response["choices"][0]["message"]["content"]
|
28 |
+
while response.startswith("\n"):
|
29 |
+
response = response[1:]
|
30 |
+
return response
|
31 |
|
32 |
iface = gr.Interface(fn=chat,
|
33 |
inputs=["text", "text", "text"],
|
34 |
outputs=["text", "text"],
|
35 |
+
description="""这是一个极其简单的示范程序,用唐三藏的语气来和你对话。
|
36 |
+
注意:duplicate 本项目后,需要将你自己的 openai apikey 设置到 settings 的 Repository Secrets 里,否则运行会报错。[了解详情](https://huggingface.co/spaces/baixing/hackathon_chatbot_openai_api/blob/main/%E6%B7%BB%E5%8A%A0%20secret%20%E7%9A%84%E6%96%B9%E6%B3%95.jpg)
|
37 |
[对话测试](https://huggingface.co/spaces/BaixingAI/hackathon_test) [参考文档](https://huggingface.co/spaces/baixing/hackathon_test/blob/main/bot-api.md) [Q & A](https://huggingface.co/spaces/baixing/hackathon_test/blob/main/qna.md)
|
38 |
""")
|
39 |
+
iface.launch()
|