1
0
Fork 0
GPT4FREE/forefront/__init__.py

155 lines
5.3 KiB
Python
Raw Normal View History

2023-04-27 14:43:59 +00:00
from json import loads
2023-04-27 09:34:05 +00:00
from re import match
2023-04-27 14:43:59 +00:00
from time import time, sleep
2023-04-27 09:34:05 +00:00
from uuid import uuid4
2023-04-27 14:43:59 +00:00
2023-04-27 09:34:05 +00:00
from requests import post
2023-04-27 14:43:59 +00:00
from tls_client import Session
from forefront.mail import Mail
from forefront.typing import ForeFrontResponse
2023-04-27 09:34:05 +00:00
class Account:
2023-04-27 13:43:58 +00:00
@staticmethod
2023-04-27 14:43:59 +00:00
def create(proxy=None, logging=False):
2023-04-27 09:34:05 +00:00
proxies = {
'http': 'http://' + proxy,
2023-04-27 14:43:59 +00:00
'https': 'http://' + proxy} if proxy else False
2023-04-27 09:34:05 +00:00
start = time()
2023-04-27 14:43:59 +00:00
mail = Mail(proxies)
mail_token = None
mail_adress = mail.get_mail()
# print(mail_adress)
client = Session(client_identifier='chrome110')
2023-04-27 09:34:05 +00:00
client.proxies = proxies
client.headers = {
"origin": "https://accounts.forefront.ai",
2023-04-27 14:43:59 +00:00
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36",
2023-04-27 09:34:05 +00:00
}
2023-04-27 14:43:59 +00:00
response = client.post('https://clerk.forefront.ai/v1/client/sign_ups?_clerk_js_version=4.32.6',
data={
"email_address": mail_adress
}
)
2023-04-28 10:33:51 +00:00
try:
trace_token = response.json()['response']['id']
if logging: print(trace_token)
except KeyError:
return 'Failed to create account!'
2023-04-27 09:34:05 +00:00
2023-04-27 14:43:59 +00:00
response = client.post(
f"https://clerk.forefront.ai/v1/client/sign_ups/{trace_token}/prepare_verification?_clerk_js_version=4.32.6",
data={
"strategy": "email_code",
2023-04-27 09:34:05 +00:00
}
2023-04-27 14:43:59 +00:00
)
2023-04-27 09:34:05 +00:00
if logging: print(response.text)
if not 'sign_up_attempt' in response.text:
return 'Failed to create account!'
while True:
sleep(1)
for _ in mail.fetch_inbox():
if logging: print(mail.get_message_content(_["id"]))
2023-04-27 09:34:05 +00:00
mail_token = match(r"(\d){5,6}", mail.get_message_content(_["id"])).group(0)
if mail_token:
break
2023-04-27 14:43:59 +00:00
2023-04-27 09:34:05 +00:00
if logging: print(mail_token)
2023-04-27 14:43:59 +00:00
response = client.post(
f'https://clerk.forefront.ai/v1/client/sign_ups/{trace_token}/attempt_verification?_clerk_js_version=4.38.4',
data={
'code': mail_token,
'strategy': 'email_code'
})
2023-04-27 09:34:05 +00:00
if logging: print(response.json())
2023-04-27 14:43:59 +00:00
token = response.json()['client']['sessions'][0]['last_active_token']['jwt']
2023-04-27 09:34:05 +00:00
with open('accounts.txt', 'a') as f:
f.write(f'{mail_adress}:{token}\n')
2023-04-27 14:43:59 +00:00
2023-04-27 09:34:05 +00:00
if logging: print(time() - start)
2023-04-27 14:43:59 +00:00
2023-04-27 09:34:05 +00:00
return token
class StreamingCompletion:
2023-04-27 13:43:58 +00:00
@staticmethod
2023-04-27 09:34:05 +00:00
def create(
2023-04-27 14:43:59 +00:00
token=None,
chatId=None,
prompt='',
actionType='new',
defaultPersona='607e41fe-95be-497e-8e97-010a59b2e2c0', # default
model='gpt-4') -> ForeFrontResponse:
2023-04-27 09:34:05 +00:00
if not token: raise Exception('Token is required!')
if not chatId: chatId = str(uuid4())
2023-04-27 14:43:59 +00:00
2023-04-27 09:34:05 +00:00
headers = {
2023-04-27 14:43:59 +00:00
'authority': 'chat-server.tenant-forefront-default.knative.chi.coreweave.com',
'accept': '*/*',
'accept-language': 'en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3',
'authorization': 'Bearer ' + token,
'cache-control': 'no-cache',
'content-type': 'application/json',
'origin': 'https://chat.forefront.ai',
'pragma': 'no-cache',
'referer': 'https://chat.forefront.ai/',
'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"',
'sec-ch-ua-mobile': '?0',
2023-04-27 09:34:05 +00:00
'sec-ch-ua-platform': '"macOS"',
2023-04-27 14:43:59 +00:00
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'cross-site',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
2023-04-27 09:34:05 +00:00
}
json_data = {
2023-04-27 14:43:59 +00:00
'text': prompt,
'action': actionType,
'parentId': chatId,
'workspaceId': chatId,
'messagePersona': defaultPersona,
'model': model
2023-04-27 09:34:05 +00:00
}
for chunk in post('https://chat-server.tenant-forefront-default.knative.chi.coreweave.com/chat',
2023-04-27 14:43:59 +00:00
headers=headers, json=json_data, stream=True).iter_lines():
2023-04-27 09:34:05 +00:00
if b'finish_reason":null' in chunk:
2023-04-27 14:43:59 +00:00
data = loads(chunk.decode('utf-8').split('data: ')[1])
2023-04-27 09:34:05 +00:00
token = data['choices'][0]['delta'].get('content')
2023-04-27 14:43:59 +00:00
2023-04-27 09:34:05 +00:00
if token != None:
yield ForeFrontResponse({
2023-04-27 14:43:59 +00:00
'id': chatId,
'object': 'text_completion',
'created': int(time()),
'model': model,
'choices': [{
'text': token,
'index': 0,
'logprobs': None,
'finish_reason': 'stop'
}],
'usage': {
'prompt_tokens': len(prompt),
'completion_tokens': len(token),
'total_tokens': len(prompt) + len(token)
}
})