File size: 2,206 Bytes
23275e2
 
 
 
 
 
 
 
 
 
d277af4
23275e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10f896a
23275e2
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import gradio as gr
import os
import requests

# 请记得要把 api 的 key 放到 settings 下面的 Repository Secrets 里。
# 目前有个特别奇怪的问题: duplicate 的 key 如果和原来的 key 重名,build 就会失败。不知是否是今天正在 migrating 的原因。
# 作为 workaround,请对 key 使用一个不同的名字,并且记得修改下面这行代码中的 key 的名字。
key = os.getenv("KEY")

# 如果你只打算通过 prompt 来定制机器人的行为,只需要修改这段 prompt 就够了。
prompt = """请你扮演一个高级程序员,能写代码,你写的代码被复制粘贴后直接就能运行。如果有人问你能干什么之类的问题,就回答我是程序员,可以帮你写代码。
########
"""

url = "https://gpt.baixing.com/"

history = {}

# 修改本函数,来实现你自己的 chatbot
# p: 对机器人说话的内容  
# qid: 当前消息的唯一标识。例如 `'bxqid-cManAtRMszw...'`。由平台生成并传递给机器人,以便机器人区分单个问题(写日志、追踪调试、异步回调等)。同步调用可忽略。  
# uid: 用户的唯一标识。例如`'bxuid-Aj8Spso8Xsp...'`。由平台生成并传递给机器人,以便机器人区分用户。可被用于实现多轮对话的功能。  
# 返回值:[type, content]
# 详见 https://huggingface.co/spaces/baixing/hackathon_test/blob/main/bot-api.md
def chat(p, qid, uid): 
    global history
    if uid in history:
        count = history[uid] # 还需要添加复杂一些的逻辑,例如超过10条要重温,超过1小时要重温 etc
    else:
        count = 0
        p = prompt + p #第一次访问,需要加上引导的 prompt

    count = count+1
    print(uid, count)
    history[uid] = count # 统计每个 uid 说过的话的条数

    result = requests.get(url, params={"p": p, "k": key, "session_id": uid}).json()['data']
    return ["text", result]

iface = gr.Interface(fn=chat, 
                     inputs=["text", "text", "text"], 
                     outputs=["text", "text"],
                     description="""能写游戏程序的小助手。)
                     """
                    )
iface.launch()