Spaces:
Runtime error
Runtime error
| import random | |
| import json | |
| import gradio as gr | |
| import requests | |
| def getResponse(api, p, qid, uid): | |
| if api.strip().endswith(".hf.space/run/predict"): | |
| return getResponseFromHF(api, p, qid, uid) | |
| else: | |
| return getResponseFromDefault(api, p, qid, uid) | |
| return reply | |
| def getResponseFromHF(api, p, qid, uid): | |
| response = requests.post(api, json={ | |
| "data": [ | |
| p, | |
| qid, | |
| uid, | |
| ] | |
| }) | |
| print(response) | |
| print(response.json()) | |
| data = response.json()["data"] | |
| if (len(data) == 2): | |
| return data[1] | |
| def getResponseFromDefault(api, p, qid, uid): | |
| response = requests.post(api, json={ | |
| "p":p, | |
| "qid":qid, | |
| "uid":uid | |
| }) | |
| print(response) | |
| print(response.json()) | |
| return response.json()["data"]["content"] | |
| def chat(api, p, qid, uid, history): | |
| history = history or [] | |
| reply = getResponse(api, p, qid, uid) | |
| history.append((p, reply)) | |
| return history, history | |
| gr.Interface(fn=chat, | |
| theme="default", | |
| css=".footer {display:none !important}", | |
| inputs=["text", "text", "text", "text", "state"], | |
| outputs=["chatbot", "state"], | |
| title="ChatAPI Test", | |
| description="""你可以通过本应用来模拟接入瀛海威广场后的效果。 | |
| #### 左侧:模拟来自瀛海威广场的调用 | |
| * api: 请填写你的机器人的 api 地址 | |
| 当用户在广场找到你的机器人,和它说话,你的 api 将会收到如下参数的调用: | |
| * p: 人们在广场里对你的机器人说话的内容 | |
| * qid: 当前消息的唯一标识。例如 `'bxqid-cManAtRMszw...'`。由平台生成并传递给机器人,以便机器人区分单个问题(写日志、追踪调试、异步回调等)。同步调用可忽略。 | |
| * uid: 用户的唯一标识。例如`'bxuid-Aj8Spso8Xsp...'`。由平台生成并传递给机器人,以便机器人区分用户。可被用于实现多轮对话的功能。 | |
| #### 右侧:对话界面 | |
| 当你的机器人 api 返回给瀛海威广场后,瀛海威广场上的机器人和用户的对话,将会呈现如右侧的效果。 | |
| (瀛海威广场上的对话窗口将有 markdown 格式呈现的功能) | |
| [参考文档](https://huggingface.co/spaces/baixing/hackathon_test/blob/main/bot-api.md) [极简 bot 示例](https://huggingface.co/spaces/baixing/hackathon_chatbot_simple) [Q & A](https://huggingface.co/spaces/baixing/hackathon_test/blob/main/qna.md) | |
| """ | |
| ).launch() |