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,26 @@ 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 |
-
你只需要 duplicate 本项目,修改 chat 函数,就能造一个能到瀛海威广场注册的机器人了。
|
| 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 = '假设你是以为优秀的Python老师,一步步教我PYTHONE,每次只交一个python的知识点,并且展示一个例子。现在开始。'
|
| 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 |
[对话测试](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)
|
| 37 |
""")
|
| 38 |
iface.launch()
|