Skip to content

Commit

Permalink
Update __init__.py
Browse files Browse the repository at this point in the history
Server responds with forbidden when using requests. rewrited with urllib
  • Loading branch information
noes14155 authored Jun 3, 2023
1 parent ebc10fa commit 1629b8a
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions gpt4free/aiassist/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import urllib.request
import json
import requests


class Completion:
Expand All @@ -20,16 +20,17 @@ def create(
}

url = "http://43.153.7.56:8080/api/chat-process"
request = requests.post(url, json=json_data)
request.encoding = request.apparent_encoding
content = request.content
headers = {"Content-type": "application/json"}

response = Completion.__load_json(content)
return response
data = json.dumps(json_data).encode("utf-8")
req = urllib.request.Request(url, data=data, headers=headers)
response = urllib.request.urlopen(req)
content = response.read().decode()

return Completion.__load_json(content)

@classmethod
def __load_json(cls, content) -> dict:
decode_content = str(content.decode("utf-8"))
split = decode_content.rsplit("\n", 1)[1]
split = content.rsplit("\n", 1)[1]
to_json = json.loads(split)
return to_json

0 comments on commit 1629b8a

Please sign in to comment.