Spaces:
Runtime error
Runtime error
File size: 5,393 Bytes
48d64c3 c391acc 48d64c3 de12296 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 |
import json, requests
import gradio as gr
def request_to_v2(message, cookie, user_id, channel_id,context=[]):
times = 3
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,zh-TW;q=0.7,zh-HK;q=0.6',
# '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":{
"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, stream=False)
print(response)
print(response.status_code)
print(response.request)
for data in response.iter_lines():
if data:
print(data)
# 将事件数据分割为多行
# event_data = line.decode('utf-8')
# print(event_data)
# encoding = chardet.detect(line)['encoding']
# print(chardet.detect(line))
# decoded_string = line.decode('hex')
# print(decoded_string)
# event_data = decoded_string.split('\n')
# for data in event_data:
# if data:
# print(data)
# response.close()
context += [response]
responses = [(u, b) for u, b in zip(context[::2], context[1::2])]
return responses, context
except BaseException as e:
print("查询失败,正在重试")
print(e)
for o in range(times):
try:
res = requests.post(post_msg_url, headers=headers, data=post_msg_data, verify=False, stream=True, allow_redirects=True)
if res.status_code == 200:
break
except BaseException as e:
print("再次查询子域名失败", o)
print(e)
return False
if __name__ == "__main__":
with gr.Blocks() as dialog_app:
with gr.Tab("Cookies"):
cookies = gr.Textbox(lines=2, label="输入cookies",)
user_id = gr.Textbox(lines=2, label="输入user_id",)
channel_id = gr.Textbox(lines=2, label="输入channel_id",)
with gr.Tab("New Bing Chat GPT4"):
gr.Markdown("""
# 连接 new-bing 接口,用的是GPT4接口
如果回复为 "1" ,说明目前服务比较火爆,建议过段时间再来用;
如果回复为 "0" , 请刷新网页重来。
如果回复为 "-1" , 需要重新利用梯子去激活一下聊天功能。
如果回复为 "-2" , 说明该账号需要登录bing聊天页面进行人机验证。
### 由于源码的更新,此时传入 [ choices = None ] 才能返回要求的格式。即不选择特定的choices模式
""")
chatbot = gr.Chatbot()
state = gr.State([])
markdown = gr.Markdown(label="Output")
with gr.Row():
inputs = gr.Textbox(
label="输入问题",
placeholder="请输入你的文本,确保已经正确填入bing.com中的cookies"
)
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")
gr.Markdown("""
如果输入后没有返回结果,大概率是你的 cookies 账号未申请new-bing的聊天功能;
步骤:
1. 在 Edge 浏览器;
2. 登录 个人账号 // 申请新账号;
3. 打开“动力”;
4. https://cn.bing.com/ 网页 或者 https://www.bing.com/new 看是否有聊天功能;
5. 新版的 Edge 浏览器右上角有个bing的图标,点击后就可以进行聊天;
6. 说明可以了,再关掉“动力”,使用国内的网络再次复制cookies。
""")
# launches the app in a new local port
dialog_app.launch(show_error=True,share=True,show_api=True)
# 为网站设置密码防止滥用
# dialog_app.launch(auth=("admin", "pass1234"))
|