Elfe commited on
Commit
b8345f6
1 Parent(s): af386a4
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ import json
3
+ import gradio as gr
4
+ import requests
5
+
6
+ def getResponse(api, p, qid, uid):
7
+ if api.strip().endswith(".hf.space/run/predict"):
8
+ return getResponseFromHF(api, p, qid, uid)
9
+ else:
10
+ return getResponseFromDefault(api, p, qid, uid)
11
+ return reply
12
+
13
+
14
+ def getResponseFromHF(api, p, qid, uid):
15
+ response = requests.post(api, json={
16
+ "data": [
17
+ p,
18
+ qid,
19
+ uid,
20
+ ]
21
+ }).json()
22
+ print(response)
23
+ data = response["data"]
24
+ if (len(data) == 2):
25
+ return data[1]
26
+
27
+ def getResponseFromDefault(api, p, qid, uid):
28
+ response = requests.post(api, json={
29
+ "p":p,
30
+ "qid":qid,
31
+ "uid":uid
32
+ }).json()
33
+ print(response)
34
+ data = response["data"]
35
+ return data["content"]
36
+
37
+ def chat(api, p, qid, uid, history):
38
+ history = history or []
39
+ reply = getResponse(api, p, qid, uid)
40
+ history.append((p, reply))
41
+ return history, history
42
+
43
+ gr.Interface(fn=chat,
44
+ theme="default",
45
+ css=".footer {display:none !important}",
46
+ inputs=["text", "text", "text", "text", "state"],
47
+ outputs=["chatbot", "state"],
48
+ title="ChatAPI Test",
49
+ description="""你可以通过本应用来模拟接入瀛海威广场后的效果。
50
+ #### 左侧:模拟来自瀛海威广场的调用
51
+ * api: 请填写你的机器人的 api 地址
52
+ 当用户在广场找到你的机器人,和它说话,你的 api 将会收到如下参数的调用:
53
+ * p: 人们在广场里对你的机器人说话的内容
54
+ * qid: 当前消息的唯一标识。例如 `'bxqid-cManAtRMszw...'`。由平台生成并传递给机器人,以便机器人区分单个问题(写日志、追踪调试、异步回调等)。同步调用可忽略。
55
+ * uid: 用户的唯一标识。例如`'bxuid-Aj8Spso8Xsp...'`。由平台生成并传递给机器人,以便机器人区分用户。可被用于实现多轮对话的功能。
56
+
57
+ #### 右侧:对话界面
58
+ 当你的机器人 api 返回给瀛海威广场后,瀛海威广场上的机器人和用户的对话,将会呈现如右侧的效果。
59
+ (瀛海威广场上的对话窗口将有 markdown 格式呈现的功能)
60
+ """
61
+ ).launch()