terry3041 / pyChatGPT

An unofficial Python wrapper for OpenAI's ChatGPT API
GNU General Public License v3.0
1.35k stars 255 forks source link

Authorization fails on Linux server (headless) #115

Closed jose-a11ysolutions closed 1 year ago

jose-a11ysolutions commented 1 year ago

I'm trying to run the following script on a Linux server. Specifically an Ubuntu EC2 instance on AWS:

from pyChatGPT import ChatGPT

session_token = 'my_session_token'

api = ChatGPT(session_token, verbose=True)

resp = api.send_message('Hello, world!')
print(resp['message'])

It is the same example as in the README, I just added the verbose option and, of course, my session token. I get the following output when I run it in the specified server.

python3 example.py 
/usr/lib/python3/dist-packages/requests/__init__.py:89: RequestsDependencyWarning: urllib3 (1.26.14) or chardet (3.0.4) doesn't match a supported version!
  warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported "
[__init_browser] Starting virtual display...
[__init_browser] Initializing browser...
[__init_browser] Restoring session_token...
[__init_browser] Ensuring Cloudflare cookies...
[__ensure_cf] Opening new tab...
[__ensure_cf] Getting Cloudflare challenge...
[__ensure_cf] Cloudflare challenge passed
[__ensure_cf] Validating authorization...
Traceback (most recent call last):
  File "example.py", line 5, in <module>
    api = ChatGPT(session_token, verbose=True)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/pyChatGPT/pyChatGPT.py", line 126, in __init__
    self.__init_browser()
  File "/home/ubuntu/.local/lib/python3.8/site-packages/pyChatGPT/pyChatGPT.py", line 223, in __init_browser
    self.__ensure_cf()
  File "/home/ubuntu/.local/lib/python3.8/site-packages/pyChatGPT/pyChatGPT.py", line 261, in __ensure_cf
    response = self.driver.find_element(By.TAG_NAME, 'pre').text
  File "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 830, in find_element
    return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
  File "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 440, in execute
    self.error_handler.check_response(response)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"tag name","selector":"pre"}
ubuntu@ip-172-31-40-189:~/gpt-interface$ python3 example.py 
/usr/lib/python3/dist-packages/requests/__init__.py:89: RequestsDependencyWarning: urllib3 (1.26.14) or chardet (3.0.4) doesn't match a supported version!
  warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported "
[__init_browser] Starting virtual display...
[__init_browser] Initializing browser...
[__init_browser] Restoring session_token...
[__init_browser] Ensuring Cloudflare cookies...
[__ensure_cf] Opening new tab...
[__ensure_cf] Getting Cloudflare challenge...
[__ensure_cf] Cloudflare challenge passed
[__ensure_cf] Validating authorization...
Traceback (most recent call last):
  File "example.py", line 5, in <module>
    api = ChatGPT(session_token, verbose=True)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/pyChatGPT/pyChatGPT.py", line 126, in __init__
    self.__init_browser()
  File "/home/ubuntu/.local/lib/python3.8/site-packages/pyChatGPT/pyChatGPT.py", line 223, in __init_browser
    self.__ensure_cf()
  File "/home/ubuntu/.local/lib/python3.8/site-packages/pyChatGPT/pyChatGPT.py", line 261, in __ensure_cf
    response = self.driver.find_element(By.TAG_NAME, 'pre').text
  File "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 830, in find_element
    return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
  File "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 440, in execute
    self.error_handler.check_response(response)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"tag name","selector":"pre"}
  (Session info: chrome=109.0.5414.119)
Stacktrace:
#0 0x564a95949303 <unknown>
#1 0x564a9571dd37 <unknown>
#2 0x564a9575a5b2 <unknown>
#3 0x564a9575a6c1 <unknown>
#4 0x564a95794b34 <unknown>
#5 0x564a9577a9ad <unknown>
#6 0x564a9579288c <unknown>
#7 0x564a9577a753 <unknown>
#8 0x564a9574da14 <unknown>
#9 0x564a9574eb7e <unknown>
#10 0x564a9599832e <unknown>
#11 0x564a9599bc0e <unknown>
#12 0x564a9597e610 <unknown>
#13 0x564a9599cc23 <unknown>
#14 0x564a95970545 <unknown>
#15 0x564a959bd6a8 <unknown>
#16 0x564a959bd836 <unknown>
#17 0x564a959d8d13 <unknown>
#18 0x7f229e78f609 start_thread

[__del__] Closing browser...
[__del__] Closing display...

I followed the instructions in the README and ran the following command to use chrome headlessly:

sudo apt install chromium-browser xvfb

The previous script runs perfectly in Windows in a headful fashion.

Any help would be highly appreciated. Thanks!

terry3041 commented 1 year ago

Please use a proxy on an AWS EC2 machine as its IP is blocked by OpenAI.