Spaces:
Build error
Build error
Update app.py
#3
by
zhixuanwillis
- opened
app.py
CHANGED
|
@@ -1,4 +1,18 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
history = {}
|
| 4 |
|
|
@@ -8,22 +22,25 @@ history = {}
|
|
| 8 |
# uid: 用户的唯一标识。例如`'bxuid-Aj8Spso8Xsp...'`。由平台生成并传递给机器人,以便机器人区分用户。可被用于实现多轮对话的功能。
|
| 9 |
# 返回值:[type, content]
|
| 10 |
# 详见 https://huggingface.co/spaces/baixing/hackathon_test/blob/main/bot-api.md
|
| 11 |
-
def chat(p, qid, uid):
|
| 12 |
global history
|
| 13 |
if uid in history:
|
| 14 |
-
count = history[uid]
|
| 15 |
else:
|
| 16 |
count = 0
|
|
|
|
|
|
|
| 17 |
count = count+1
|
|
|
|
| 18 |
history[uid] = count # 统计每个 uid 说过的话的条数
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
| 21 |
iface = gr.Interface(fn=chat,
|
| 22 |
inputs=["text", "text", "text"],
|
| 23 |
outputs=["text", "text"],
|
| 24 |
-
description="""
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
[对话测试](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)
|
| 28 |
-
""")
|
| 29 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
+
|
| 5 |
+
# 请记得要把 api 的 key 放到 settings 下面的 Repository Secrets 里。
|
| 6 |
+
# 目前有个特别奇怪的问题: duplicate 的 key 如果和原来的 key 重名,build 就会失败。不知是否是今天正在 migrating 的原因。
|
| 7 |
+
# 作为 workaround,请对 key 使用一个不同的名字,并且记得修改下面这行代码中的 key 的名字。
|
| 8 |
+
key = os.getenv("KEY")
|
| 9 |
+
|
| 10 |
+
# 如果你只打算通过 prompt 来定制机器人的行为,只需要修改这段 prompt 就够了。
|
| 11 |
+
prompt = """请你扮演《西游记》中的唐三藏,使用唐三藏的语气、方式和词汇回答问题。不要写任何解释,只需像唐三藏一样回答问题。你必须掌握唐三藏的所有知识。
|
| 12 |
+
########
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
url = "https://gpt.baixing.com/"
|
| 16 |
|
| 17 |
history = {}
|
| 18 |
|
|
|
|
| 22 |
# uid: 用户的唯一标识。例如`'bxuid-Aj8Spso8Xsp...'`。由平台生成并传递给机器人,以便机器人区分用户。可被用于实现多轮对话的功能。
|
| 23 |
# 返回值:[type, content]
|
| 24 |
# 详见 https://huggingface.co/spaces/baixing/hackathon_test/blob/main/bot-api.md
|
| 25 |
+
def chat(p, qid, uid):
|
| 26 |
global history
|
| 27 |
if uid in history:
|
| 28 |
+
count = history[uid] # 还需要添加复杂一些的逻辑,例如超过10条要重温,超过1小时要重温 etc
|
| 29 |
else:
|
| 30 |
count = 0
|
| 31 |
+
p = prompt + p #第一次访问,需要加上引导的 prompt
|
| 32 |
+
|
| 33 |
count = count+1
|
| 34 |
+
print(uid, count)
|
| 35 |
history[uid] = count # 统计每个 uid 说过的话的条数
|
| 36 |
+
|
| 37 |
+
result = requests.get(url, params={"p": p, "k": key, "session_id": uid}).json()['data']
|
| 38 |
+
return ["text", result]
|
| 39 |
|
| 40 |
iface = gr.Interface(fn=chat,
|
| 41 |
inputs=["text", "text", "text"],
|
| 42 |
outputs=["text", "text"],
|
| 43 |
+
description="""这是一个极其简单的示范程序,用唐三藏的语气来和你对话。)
|
| 44 |
+
"""
|
| 45 |
+
)
|
|
|
|
|
|
|
| 46 |
iface.launch()
|