返回提交历史
Added
unfinished/chatpdf/__init__.py
+75
-0
XFEstudio/gpt4free
initial chatpdf reverse api
5b0fa351
代码差异
1 个文件
+75
-0
@@ -0,0 +1,75 @@
1
import requests
2
import json
3
4
class Completion:
5
6
def request(prompt: str):
7
'''TODO: some sort of authentication + upload PDF from URL or local file
8
Then you should get the atoken and chat ID
9
'''
10
11
token = "your_token_here"
12
chat_id = "your_chat_id_here"
13
14
url = "https://chat-pr4yueoqha-ue.a.run.app/"
15
16
payload = json.dumps({
17
"v": 2,
18
"chatSession": {
19
"type": "join",
20
"chatId": chat_id
21
},
22
"history": [
23
{
24
"id": "VNsSyJIq_0",
25
"author": "p_if2GPSfyN8hjDoA7unYe",
26
"msg": "<start>",
27
"time": 1682672009270
28
},
29
{
30
"id": "Zk8DRUtx_6",
31
"author": "uplaceholder",
32
"msg": prompt,
33
"time": 1682672181339
34
}
35
]
36
})
37
38
# TODO: fix headers, use random user-agent, streaming response, etc
39
headers = {
40
'authority': 'chat-pr4yueoqha-ue.a.run.app',
41
'accept': '*/*',
42
'accept-language': 'en-US,en;q=0.9',
43
'atoken': token,
44
'content-type': 'application/json',
45
'origin': 'https://www.chatpdf.com',
46
'referer': 'https://www.chatpdf.com/',
47
'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"',
48
'sec-ch-ua-mobile': '?0',
49
'sec-ch-ua-platform': '"Windows"',
50
'sec-fetch-dest': 'empty',
51
'sec-fetch-mode': 'cors',
52
'sec-fetch-site': 'cross-site',
53
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'
54
}
55
56
response = requests.request("POST", url, headers=headers, data=payload).text
57
Completion.stream_completed = True
58
return {'response': response}
59
60
@staticmethod
61
def create(prompt: str):
62
Thread(target=Completion.request, args=[prompt]).start()
63
64
while Completion.stream_completed != True or not Completion.message_queue.empty():
65
try:
66
message = Completion.message_queue.get(timeout=0.01)
67
for message in findall(Completion.regex, message):
68
yield loads(Completion.part1 + message + Completion.part2)['delta']
69
70
except Empty:
71
pass
72
73
@staticmethod
74
def handle_stream_response(response):
75
Completion.message_queue.put(response.decode())