返回提交历史
Added
.vscode/settings.json
+4
-0
Modified
gpt4free/forefront/__init__.py
+23
-55
XFEstudio/gpt4free
Forefront fixed
c60d33c4
代码差异
2 个文件
+27
-55
@@ -0,0 +1,4 @@
1
{
2
"editor.tabCompletion": "on",
3
"diffEditor.codeLens": true
4
}
@@ -1,49 +1,26 @@
1
import os
2
import pickle
3
1
from json import loads
2
from xtempmail import Email
4
3
from re import findall
4
from faker import Faker
5
5
from time import time, sleep
6
from typing import Generator, Optional
7
6
from uuid import uuid4
8
9
7
from fake_useragent import UserAgent
10
from pymailtm import MailTm, Message
11
8
from requests import post
12
9
from tls_client import Session
13
14
10
from .typing import ForeFrontResponse
15
11
16
12
17
13
class Account:
18
COOKIES_FILE_NAME = 'cookies.pickle'
19
20
14
@staticmethod
21
def login(proxy: Optional[str] = None, logging: bool = False) -> str:
22
if not os.path.isfile(Account.COOKIES_FILE_NAME):
23
return Account.create(proxy, logging)
24
25
with open(Account.COOKIES_FILE_NAME, 'rb') as f:
26
cookies = pickle.load(f)
27
proxies = {'http': 'http://' + proxy, 'https': 'http://' + proxy} if proxy else False
28
29
client = Session(client_identifier='chrome110')
30
client.proxies = proxies
31
client.cookies.update(cookies)
32
33
if Account.is_cookie_enabled(client):
34
response = client.get('https://clerk.forefront.ai/v1/client?_clerk_js_version=4.38.4')
35
return response.json()['response']['sessions'][0]['last_active_token']['jwt']
36
else:
37
return Account.create(proxy, logging)
38
39
@staticmethod
40
def create(proxy: Optional[str] = None, logging: bool = False, save_cookies: bool = False) -> str:
15
def create(proxy: Optional[str] = None, logging: bool = False):
41
16
proxies = {'http': 'http://' + proxy, 'https': 'http://' + proxy} if proxy else False
17
faker = Faker()
18
name = (faker.name().replace(' ', '_')).lower()
42
19
43
20
start = time()
44
21
45
mail_client = MailTm().get_account()
46
mail_address = mail_client.address
22
mail_client = Email(name=name)
23
mail_address = mail_client.email
47
24
48
25
client = Session(client_identifier='chrome110')
49
26
client.proxies = proxies
@@ -66,7 +43,10 @@ class Account:
66
43
67
44
response = client.post(
68
45
f'https://clerk.forefront.ai/v1/client/sign_ups/{trace_token}/prepare_verification?_clerk_js_version=4.38.4',
69
data={'strategy': 'email_link', 'redirect_url': 'https://accounts.forefront.ai/sign-up/verify'},
46
data={
47
'strategy': 'email_link',
48
'redirect_url': 'https://accounts.forefront.ai/sign-up/verify'
49
},
70
50
)
71
51
72
52
if logging:
@@ -74,31 +54,23 @@ class Account:
74
54
75
55
if 'sign_up_attempt' not in response.text:
76
56
return 'Failed to create account!'
77
78
while True:
79
sleep(1)
80
new_message: Message = mail_client.wait_for_message()
81
if logging:
82
print(new_message.data['id'])
83
84
verification_url = findall(r'https:\/\/clerk\.forefront\.ai\/v1\/verify\?token=\w.+', new_message.text)[0]
85
57
verification_url = None
58
new_message = mail_client.get_new_message(5)
59
for msg in new_message:
60
verification_url = findall(r'https:\/\/clerk\.forefront\.ai\/v1\/verify\?token=\w.+', msg.text)[0]
86
61
if verification_url:
87
62
break
88
63
64
if verification_url is None or not verification_url:
65
raise RuntimeError('Error while obtaining verfication URL!')
89
66
if logging:
90
67
print(verification_url)
91
92
68
response = client.get(verification_url)
93
69
94
70
response = client.get('https://clerk.forefront.ai/v1/client?_clerk_js_version=4.38.4')
95
71
96
72
token = response.json()['response']['sessions'][0]['last_active_token']['jwt']
97
73
98
if save_cookies:
99
with open(Account.COOKIES_FILE_NAME, 'wb') as f:
100
pickle.dump(client.cookies, f)
101
102
74
with open('accounts.txt', 'a') as f:
103
75
f.write(f'{mail_address}:{token}\n')
104
76
@@ -107,11 +79,6 @@ class Account:
107
79
108
80
return token
109
81
110
@staticmethod
111
def is_cookie_enabled(client: Session) -> bool:
112
response = client.get('https://chat.forefront.ai/')
113
return 'window.startClerk' in response.text
114
115
82
116
83
class StreamingCompletion:
117
84
@staticmethod
@@ -122,14 +89,14 @@ class StreamingCompletion:
122
89
action_type='new',
123
90
default_persona='607e41fe-95be-497e-8e97-010a59b2e2c0', # default
124
91
model='gpt-4',
125
proxy=None,
92
proxy=None
126
93
) -> Generator[ForeFrontResponse, None, None]:
127
94
if not token:
128
95
raise Exception('Token is required!')
129
96
if not chat_id:
130
97
chat_id = str(uuid4())
131
98
132
proxies = {'http': 'http://' + proxy, 'https': 'http://' + proxy} if proxy else None
99
proxies = { 'http': 'http://' + proxy, 'https': 'http://' + proxy } if proxy else None
133
100
134
101
headers = {
135
102
'authority': 'chat-server.tenant-forefront-default.knative.chi.coreweave.com',
@@ -197,7 +164,7 @@ class Completion:
197
164
action_type='new',
198
165
default_persona='607e41fe-95be-497e-8e97-010a59b2e2c0', # default
199
166
model='gpt-4',
200
proxy=None,
167
proxy=None
201
168
) -> ForeFrontResponse:
202
169
text = ''
203
170
final_response = None
@@ -208,7 +175,7 @@ class Completion:
208
175
action_type=action_type,
209
176
default_persona=default_persona,
210
177
model=model,
211
proxy=proxy,
178
proxy=proxy
212
179
):
213
180
if response:
214
181
final_response = response
@@ -220,3 +187,4 @@ class Completion:
220
187
raise Exception('Unable to get the response, Please try again')
221
188
222
189
return final_response
190