Create helpers/phind.py
Browse files
g4f/Provider/Providers/helpers
DELETED
|
File without changes
|
g4f/Provider/Providers/helpers/phind.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import json
|
| 3 |
+
import datetime
|
| 4 |
+
import urllib.parse
|
| 5 |
+
|
| 6 |
+
from curl_cffi import requests
|
| 7 |
+
|
| 8 |
+
config = json.loads(sys.argv[1])
|
| 9 |
+
prompt = config['messages'][-1]['content']
|
| 10 |
+
|
| 11 |
+
skill = 'expert' if config['model'] == 'gpt-4' else 'intermediate'
|
| 12 |
+
|
| 13 |
+
json_data = json.dumps({
|
| 14 |
+
'question': prompt,
|
| 15 |
+
'options': {
|
| 16 |
+
'skill': skill,
|
| 17 |
+
'date': datetime.datetime.now().strftime('%d/%m/%Y'),
|
| 18 |
+
'language': 'en',
|
| 19 |
+
'detailed': True,
|
| 20 |
+
'creative': True,
|
| 21 |
+
'customLinks': []}}, separators=(',', ':'))
|
| 22 |
+
|
| 23 |
+
headers = {
|
| 24 |
+
'Content-Type': 'application/json',
|
| 25 |
+
'Pragma': 'no-cache',
|
| 26 |
+
'Accept': '*/*',
|
| 27 |
+
'Sec-Fetch-Site': 'same-origin',
|
| 28 |
+
'Accept-Language': 'en-GB,en;q=0.9',
|
| 29 |
+
'Cache-Control': 'no-cache',
|
| 30 |
+
'Sec-Fetch-Mode': 'cors',
|
| 31 |
+
'Content-Length': str(len(json_data)),
|
| 32 |
+
'Origin': 'https://www.phind.com',
|
| 33 |
+
'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',
|
| 34 |
+
'Referer': f'https://www.phind.com/search?q={urllib.parse.quote(prompt)}&source=searchbox',
|
| 35 |
+
'Connection': 'keep-alive',
|
| 36 |
+
'Host': 'www.phind.com',
|
| 37 |
+
'Sec-Fetch-Dest': 'empty'
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def output(chunk):
|
| 42 |
+
try:
|
| 43 |
+
if b'PHIND_METADATA' in chunk:
|
| 44 |
+
return
|
| 45 |
+
|
| 46 |
+
if chunk == b'data: \r\ndata: \r\ndata: \r\n\r\n':
|
| 47 |
+
chunk = b'data: \n\r\n\r\n'
|
| 48 |
+
|
| 49 |
+
chunk = chunk.decode()
|
| 50 |
+
|
| 51 |
+
chunk = chunk.replace('data: \r\n\r\ndata: ', 'data: \n')
|
| 52 |
+
chunk = chunk.replace('\r\ndata: \r\ndata: \r\n\r\n', '\n\r\n\r\n')
|
| 53 |
+
chunk = chunk.replace('data: ', '').replace('\r\n\r\n', '')
|
| 54 |
+
|
| 55 |
+
print(chunk, flush=True, end = '')
|
| 56 |
+
|
| 57 |
+
except json.decoder.JSONDecodeError:
|
| 58 |
+
pass
|
| 59 |
+
|
| 60 |
+
while True:
|
| 61 |
+
try:
|
| 62 |
+
response = requests.post('https://www.phind.com/api/infer/answer',
|
| 63 |
+
headers=headers, data=json_data, content_callback=output, timeout=999999, impersonate='safari15_5')
|
| 64 |
+
|
| 65 |
+
exit(0)
|
| 66 |
+
|
| 67 |
+
except Exception as e:
|
| 68 |
+
print('an error occured, retrying... |', e, flush=True)
|
| 69 |
+
continue
|