Spaces:
Runtime error
Runtime error
File size: 5,794 Bytes
bc73251 48d64c3 bc73251 e813911 48d64c3 c391acc 4fc48ee 48d64c3 4d98764 a86f939 48d64c3 dd82a48 9ffe8f1 48d64c3 dd82a48 48d64c3 c55c175 bc73251 d6bbdb0 5e77b4d ec58988 48d64c3 d6bbdb0 48d64c3 5e77b4d 48d64c3 5e77b4d ec58988 48d64c3 216fd1b 46843f3 216fd1b 806d462 515a179 ce740f4 515a179 216fd1b ce740f4 e0f4c76 ce740f4 fa4def4 216fd1b 48d64c3 216fd1b fa4def4 216fd1b 48d64c3 216fd1b bcd7341 216fd1b 330e184 216fd1b fa4def4 48d64c3 |
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
import json
import gradio as gr
from curl_cffi import requests
import re
def request_to_v2(message, cookie, user_id, channel_id,context=[]):
timeout = 5*60 #5分钟不回复,显示超时
context = [message]
headers = {
# ':Authority': 'claude.ai',
# ':Method': 'POST',
# ':Path': '/api/append_message',
# ':Scheme': 'https',
'Content-Type': 'application/json',
'Origin': 'https://claude.ai',
'Referer': f'https://claude.ai/chat/{channel_id}',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
'Cookie': cookie,
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
# 'Accept-Encoding': 'gzip, deflate, br',
'Accept': 'text/event-stream, text/event-stream',
'Sec-Ch-Ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
'Sec-Ch-Ua-Mobile': '?0',
'Sec-Ch-Ua-Platform': '"Windows"',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'Connection': 'keep-alive',
}
post_msg_url = 'https://claude.ai/api/append_message'
post_msg_data = {
"completion":{
"incremental":True,
"prompt": message,
"timezone":"Asia/Shanghai",
"model":"claude-2"
},
"organization_uuid": user_id,
"conversation_uuid": channel_id,
"text": message,
"attachments": []
}
post_msg_data = json.dumps(post_msg_data)
print(post_msg_data)
try:
response = requests.post(post_msg_url, headers=headers, data=post_msg_data, verify=False,timeout = timeout,impersonate="chrome110")
decoded_data = response.content.decode("utf-8")
decoded_data = re.sub('\n+', '\n', decoded_data).strip()
data_strings = decoded_data.split('\n')
completions = []
for data_string in data_strings:
json_str = data_string[6:].strip()
data = json.loads(json_str)
if 'completion' in data:
completions.append(data['completion'])
bots = ''.join(completions)
print(bots)
context += [bots]
responses = [(u, b) for u, b in zip(context[::2], context[1::2])]
print("a"*100)
return responses, context
except Exception as e:
print(">>>>>> 查询失败")
print(e)
response =">>>>>> 查询失败\n报错信息为:"+ str(e)
context += [response]
responses = [(u, b) for u, b in zip(context[::2], context[1::2])]
return responses, context
with gr.Blocks() as dialog_app:
gr.HTML("""<h1 align="center">Claude2-API-xiaolv-第1版-多轮对话</h1>""")
with gr.Tab("Claude2 API xiaolv"):
gr.Markdown("""
## 需要传入的参数有:
1.cookie:intercom-device-id-lupk8zyo=8888
2.user_id:对应 organization_uuid
3.channel_id:对应 conversation_uuid
## 特点:
1. 会根据传入的channel_id在同一个频道下进行对话,模型会记住上一次的对话;
2. 此时如果不需要模型记住历史记录的话,需要传```/resrt```命令,
```/resrt```:a. 虽然这样可以使模型忘记之前的记录,但同时模型很多前置的信息也会忘记,比如问他今天几号;
b. 虽然模型把之前的记录忘记了,但历史的对话依旧存在,对话过长时,模型回复同样很慢。
""")
gr.HTML(
"""<p>3.如果只想回复一次的话,可以参考以下这个地址:<a href="https://huggingface.co/spaces/xiaolv/claude2_xiaolv_api_updata">Claude2-API-xiaolv-第2版-单轮对话</a></p>"""
"""<p>4.如果只想回复一次且可以支持文件知识库回答的话,可以参考以下这个地址:<a href="https://huggingface.co/spaces/xiaolv/claude2_xiaolv_api_file_chat">Claude2-API-xiaolv-第3版-单轮对话-支持文件知识库</a></p>"""
"""<p>如果喜欢请给个 💖 </p>"""
)
with gr.Row():
with gr.Column(scale=0.4):
cookies = gr.Textbox(lines=2, label="输入cookies")
user_id = gr.Textbox(lines=2, label="输入user_id/organization_uuid")
channel_id = gr.Textbox(lines=2, label="输入channel_id/conversation_uuid")
# chatbot = gr.Chatbot()
with gr.Column(scale=0.6):
chatbot = gr.Chatbot([])
state = gr.State([])
with gr.Row():
inputs = gr.Textbox(
label="输入问题",
placeholder="请输入你的文本,确保已经正确填入cookies、user_id、channel_id"
)
inputs.submit(request_to_v2, [inputs, cookies, user_id,channel_id,state], [chatbot, state])
send = gr.Button("发送请求.....")
send.click(request_to_v2, [inputs, cookies, user_id,channel_id,state], [chatbot, state], api_name="xiaolvgpt", show_progress=True)
gr.Markdown("""
清除历史记录是,请输入:```/resrt```
>>> 2023-08-21:修复“We are unable to serve your request ”问题,替换成curl_cffi 模拟浏览器模式。
** 参考github资料 **
<a href="https://github.com/wwwzhouhui/Claude2-PyAPI">1. Claude2-PyAPI源码魔改地址</a>
""")
# 为网站设置密码防止滥用
# dialog_app.launch(auth=("admin", "2240560729"),show_error=True,show_api=True,share=True)
# dialog_app.launch(auth=("admin", "2240560729"),show_error=True)
dialog_app.launch(show_error=True)
# dialog_app.launch(show_error=True,show_api=True,share=True)
|