Spaces:
Running
Running
import sys | |
import json | |
import datetime | |
import urllib.parse | |
from curl_cffi import requests | |
config = json.loads(sys.argv[1]) | |
prompt = config['messages'][-1]['content'] | |
skill = 'expert' if config['model'] == 'gpt-4' else 'intermediate' | |
json_data = json.dumps({ | |
'question': prompt, | |
'options': { | |
'skill': skill, | |
'date': datetime.datetime.now().strftime('%d/%m/%Y'), | |
'language': 'en', | |
'detailed': True, | |
'creative': True, | |
'customLinks': []}}, separators=(',', ':')) | |
headers = { | |
'Content-Type': 'application/json', | |
'Pragma': 'no-cache', | |
'Accept': '*/*', | |
'Sec-Fetch-Site': 'same-origin', | |
'Accept-Language': 'en-GB,en;q=0.9', | |
'Cache-Control': 'no-cache', | |
'Sec-Fetch-Mode': 'cors', | |
'Content-Length': str(len(json_data)), | |
'Origin': 'https://www.phind.com', | |
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.4 Safari/605.1.15', | |
'Referer': f'https://www.phind.com/search?q={urllib.parse.quote(prompt)}&source=searchbox', | |
'Connection': 'keep-alive', | |
'Host': 'www.phind.com', | |
'Sec-Fetch-Dest': 'empty' | |
} | |
def output(chunk): | |
try: | |
if b'PHIND_METADATA' in chunk: | |
return | |
if chunk == b'data: \r\ndata: \r\ndata: \r\n\r\n': | |
chunk = b'data: \n\r\n\r\n' | |
chunk = chunk.decode() | |
chunk = chunk.replace('data: \r\n\r\ndata: ', 'data: \n') | |
chunk = chunk.replace('\r\ndata: \r\ndata: \r\n\r\n', '\n\r\n\r\n') | |
chunk = chunk.replace('data: ', '').replace('\r\n\r\n', '') | |
print(chunk, flush=True, end = '') | |
except json.decoder.JSONDecodeError: | |
pass | |
while True: | |
try: | |
response = requests.post('https://www.phind.com/api/infer/answer', | |
headers=headers, data=json_data, content_callback=output, timeout=999999, impersonate='safari15_5') | |
exit(0) | |
except Exception as e: | |
print('an error occured, retrying... |', e, flush=True) | |
continue |