terry3041 / pyChatGPT

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

Run headless in Docker #111

Open peteh opened 1 year ago

peteh commented 1 year ago

Did anybody succeed in running this headless in docker?

I tried with python:3.10-slim-bullseye and alpine but I'm struggling getting all the dependencies right.

Requirements.txt

pychatgpt
ffmpeg_downloader
PyPasser
pocketsphinx
falcon
PyVirtualDisplay
selenium_profiles

Dockerfile

# syntax=docker/dockerfile:1
#FROM python:3.10-alpine
FROM python:3.10-slim-bullseye
#RUN apk update; apk add build-base swig pulseaudio-dev ffmpeg chromium xvfb
RUN apt-get update; apt-get install -y build-essential swig libpulse-dev ffmpeg chromium chromium-driver xvfb
WORKDIR /app
COPY . .
RUN pip3 install -r requirements.txt
RUN ffdl install -y
RUN python3 bootstrap.py
CMD ["python3", "runIdle.py"]
ENV SERVER_PORT 8000
EXPOSE 8000/tcp

Error in docker:

# python3 run.py
Traceback (most recent call last):
  File "/app/run.py", line 40, in <module>
    chatGPTEndpoint = ChatGPTResource()
  File "/app/run.py", line 9, in __init__
    self._api = ChatGPT(auth_type='openai', email='###', password='###')
  File "/usr/local/lib/python3.10/site-packages/pyChatGPT/pyChatGPT.py", line 126, in __init__
    self.__init_browser()
  File "/usr/local/lib/python3.10/site-packages/pyChatGPT/pyChatGPT.py", line 223, in __init_browser
    self.__ensure_cf()
  File "/usr/local/lib/python3.10/site-packages/pyChatGPT/pyChatGPT.py", line 269, in __ensure_cf
    self.__login()
  File "/usr/local/lib/python3.10/site-packages/pyChatGPT/pyChatGPT.py", line 322, in __login
    WebDriverWait(self.driver, 5).until(
  File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/support/wait.py", line 86, in until
    value = method(self._driver)
  File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/support/expected_conditions.py", line 64, in _predicate
    return driver.find_element(*locator)
  File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 861, in find_element
    return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
  File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 444, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 249, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: disconnected: Unable to receive message from renderer
  (failed to check if window was closed: disconnected: unable to connect to renderer)
  (Session info: chrome=109.0.5414.74)
Stacktrace:
#0 0x56485cd00303 <unknown>
#1 0x56485cad4d37 <unknown>
#2 0x56485cabf549 <unknown>
#3 0x56485cabf285 <unknown>
#4 0x56485cabdc77 <unknown>
#5 0x56485cabe408 <unknown>
#6 0x56485cae07ae <unknown>
#7 0x56485cad7df1 <unknown>
#8 0x56485cad7931 <unknown>
#9 0x56485cad8351 <unknown>
#10 0x56485cad86dc <unknown>
#11 0x56485cb11188 <unknown>
#12 0x56485cb116c1 <unknown>
#13 0x56485cb4bb34 <unknown>
#14 0x56485cb319ad <unknown>
#15 0x56485cb4988c <unknown>
#16 0x56485cb31753 <unknown>
#17 0x56485cb04a14 <unknown>
#18 0x56485cb05b7e <unknown>
#19 0x56485cd4f32e <unknown>
#20 0x56485cd52c0e <unknown>
#21 0x56485cd35610 <unknown>
#22 0x56485cd53c23 <unknown>
#23 0x56485cd27545 <unknown>
#24 0x56485cd746a8 <unknown>
#25 0x56485cd74836 <unknown>
#26 0x56485cd8fd13 <unknown>
#27 0x7efea9da5ea7 start_thread
0xrushi commented 1 year ago

This Dockerfile should work.with these changes in pyChatGPT

FROM python:3.9-slim-buster

RUN apt-get update && \
    apt-get install -y chromium xvfb chromium-driver 

COPY . /app
WORKDIR /app

RUN pip install -U PyVirtualDisplay
RUN pip install -e pyChatGPT/

# CMD ["python", "main.py"]
CMD ["bash"]
peteh commented 1 year ago

Thanks I'll try it when I'm back from the weekend. Will these changes be merged into the lib as well or will this be a manual change I need to do in docker every time?

peteh commented 1 year ago

I tried with your change and I had to switch buster with bullseye but it seems to be working. Thank you

However, I'm currently facing the new cloudflare verify that you are human challenge issue.

peteh commented 1 year ago

Actually I leave this open till the change is merged to the main branch

0xrushi commented 1 year ago

@peteh I don't think this will be merged into main, because then it fails for macos/windows. Lets see if anyone knows a solution for both.

peteh commented 1 year ago

Maybe by checking if we're running on Linux and only then passing that extra parameter?

from sys import platform
if platform == "linux" or platform == "linux2":
    # linux
elif platform == "darwin":
    # OS X
elif platform == "win32":
    # Windows...