Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +131 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json, requests
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
def request_to_v2(message, cookie, user_id, channel_id,context=[]):
|
| 5 |
+
times = 3
|
| 6 |
+
context = [message]
|
| 7 |
+
headers = {
|
| 8 |
+
# ':Authority': 'claude.ai',
|
| 9 |
+
# ':Method': 'POST',
|
| 10 |
+
# ':Path': '/api/append_message',
|
| 11 |
+
# ':Scheme': 'https',
|
| 12 |
+
'Content-Type': 'application/json',
|
| 13 |
+
'Origin': 'https://claude.ai',
|
| 14 |
+
'Referer': f'https://claude.ai/chat/{channel_id}',
|
| 15 |
+
'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',
|
| 16 |
+
'Cookie': cookie,
|
| 17 |
+
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7,zh-HK;q=0.6',
|
| 18 |
+
# 'Accept-Encoding': 'gzip, deflate, br',
|
| 19 |
+
'Accept': 'text/event-stream, text/event-stream',
|
| 20 |
+
'Sec-Ch-Ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
|
| 21 |
+
'Sec-Ch-Ua-Mobile': '?0',
|
| 22 |
+
'Sec-Ch-Ua-Platform': '"Windows"',
|
| 23 |
+
'Sec-Fetch-Dest': 'empty',
|
| 24 |
+
'Sec-Fetch-Mode': 'cors',
|
| 25 |
+
'Sec-Fetch-Site': 'same-origin',
|
| 26 |
+
'Connection': 'keep-alive',
|
| 27 |
+
}
|
| 28 |
+
post_msg_url = 'https://claude.ai/api/append_message'
|
| 29 |
+
post_msg_data = {
|
| 30 |
+
"completion":{
|
| 31 |
+
"prompt": message,
|
| 32 |
+
"timezone":"Asia/Shanghai",
|
| 33 |
+
"model":"claude-2"
|
| 34 |
+
},
|
| 35 |
+
"organization_uuid": user_id,
|
| 36 |
+
"conversation_uuid": channel_id,
|
| 37 |
+
"text": message,
|
| 38 |
+
"attachments": []
|
| 39 |
+
}
|
| 40 |
+
post_msg_data = json.dumps(post_msg_data)
|
| 41 |
+
print(post_msg_data)
|
| 42 |
+
|
| 43 |
+
try:
|
| 44 |
+
response = requests.post(post_msg_url, headers=headers, data=post_msg_data, verify=False, stream=False)
|
| 45 |
+
print(response)
|
| 46 |
+
|
| 47 |
+
print(response.status_code)
|
| 48 |
+
print(response.request)
|
| 49 |
+
for data in response.iter_lines():
|
| 50 |
+
if data:
|
| 51 |
+
print(data)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
# 将事件数据分割为多行
|
| 55 |
+
# event_data = line.decode('utf-8')
|
| 56 |
+
# print(event_data)
|
| 57 |
+
# encoding = chardet.detect(line)['encoding']
|
| 58 |
+
# print(chardet.detect(line))
|
| 59 |
+
# decoded_string = line.decode('hex')
|
| 60 |
+
# print(decoded_string)
|
| 61 |
+
# event_data = decoded_string.split('\n')
|
| 62 |
+
# for data in event_data:
|
| 63 |
+
# if data:
|
| 64 |
+
# print(data)
|
| 65 |
+
# response.close()
|
| 66 |
+
context += [response]
|
| 67 |
+
responses = [(u, b) for u, b in zip(context[::2], context[1::2])]
|
| 68 |
+
return responses, context
|
| 69 |
+
|
| 70 |
+
except BaseException as e:
|
| 71 |
+
print("查询失败,正在重试")
|
| 72 |
+
print(e)
|
| 73 |
+
for o in range(times):
|
| 74 |
+
try:
|
| 75 |
+
res = requests.post(post_msg_url, headers=headers, data=post_msg_data, verify=False, stream=True, allow_redirects=True)
|
| 76 |
+
if res.status_code == 200:
|
| 77 |
+
break
|
| 78 |
+
except BaseException as e:
|
| 79 |
+
print("再次查询子域名失败", o)
|
| 80 |
+
print(e)
|
| 81 |
+
return False
|
| 82 |
+
|
| 83 |
+
if __name__ == "__main__":
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
with gr.Blocks() as dialog_app:
|
| 87 |
+
with gr.Tab("Cookies"):
|
| 88 |
+
cookies = gr.Textbox(lines=2, label="输入cookies",)
|
| 89 |
+
user_id = gr.Textbox(lines=2, label="输入user_id",)
|
| 90 |
+
channel_id = gr.Textbox(lines=2, label="输入channel_id",)
|
| 91 |
+
with gr.Tab("New Bing Chat GPT4"):
|
| 92 |
+
gr.Markdown("""
|
| 93 |
+
# 连接 new-bing 接口,用的是GPT4接口
|
| 94 |
+
如果回复为 "1" ,说明目前服务比较火爆,建议过段时间再来用;
|
| 95 |
+
如果回复为 "0" , 请刷新网页重来。
|
| 96 |
+
如果回复为 "-1" , 需要重新利用梯子去激活一下聊天功能。
|
| 97 |
+
如果回复为 "-2" , 说明该账号需要登录bing聊天页面进行人机验证。
|
| 98 |
+
|
| 99 |
+
### 由于源码的更新,此时传入 [ choices = None ] 才能返回要求的格式。即不选择特定的choices模式
|
| 100 |
+
""")
|
| 101 |
+
|
| 102 |
+
chatbot = gr.Chatbot()
|
| 103 |
+
state = gr.State([])
|
| 104 |
+
markdown = gr.Markdown(label="Output")
|
| 105 |
+
|
| 106 |
+
with gr.Row():
|
| 107 |
+
inputs = gr.Textbox(
|
| 108 |
+
label="输入问题",
|
| 109 |
+
placeholder="请输入你的文本,确保已经正确填入bing.com中的cookies"
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
inputs.submit(request_to_v2, [inputs, cookies, user_id,channel_id,state], [chatbot, state])
|
| 113 |
+
send = gr.Button("发送请求.....")
|
| 114 |
+
send.click(request_to_v2, [inputs, cookies, user_id,channel_id,state], [chatbot, state], api_name="xiaolvgpt")
|
| 115 |
+
|
| 116 |
+
gr.Markdown("""
|
| 117 |
+
如果输入后没有返回结果,大概率是你的 cookies 账号未申请new-bing的聊天功能;
|
| 118 |
+
步骤:
|
| 119 |
+
1. 在 Edge 浏览器;
|
| 120 |
+
2. 登录 个人账号 // 申请新账号;
|
| 121 |
+
3. 打开“动力”;
|
| 122 |
+
4. https://cn.bing.com/ 网页 或者 https://www.bing.com/new 看是否有聊天功能;
|
| 123 |
+
5. 新版的 Edge 浏览器右上角有个bing的图标,点击后就可以进行聊天;
|
| 124 |
+
6. 说明可以了,再关掉“动力”,使用国内的网络再次复制cookies。
|
| 125 |
+
""")
|
| 126 |
+
|
| 127 |
+
# launches the app in a new local port
|
| 128 |
+
dialog_app.launch(show_error=True,show_api=True,share=True)
|
| 129 |
+
# 为网站设置密码防止滥用
|
| 130 |
+
# dialog_app.launch(auth=("admin", "pass1234"))
|
| 131 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
requests
|