Skip to content

Commit

Permalink
Merge pull request #1309 from hlohaus/go
Browse files Browse the repository at this point in the history
Add selenium to dockerfile
  • Loading branch information
hlohaus authored Dec 6, 2023
2 parents 546ea6e + c667f1c commit f962993
Show file tree
Hide file tree
Showing 19 changed files with 255 additions and 245 deletions.
33 changes: 0 additions & 33 deletions Dockerfile

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![248433934-7886223b-c1d1-4260-82aa-da5741f303bb](https://github.com/xtekky/gpt4free/assets/98614666/ea012c87-76e0-496a-8ac4-e2de090cc6c9)
![g4f](g4f.png)

<a href='https://ko-fi.com/xtekky' target='_blank'><img height='35' style='border:0px;height:46px;' src='https://az743702.vo.msecnd.net/cdn/kofi3.png?v=0' border='0' alt='Buy Me a Coffee at ko-fi.com' />
<div id="top"></div>
Expand Down
19 changes: 8 additions & 11 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
version: '3'

services:
gpt4free-api: &gpt4free
image: gpt4free:latest
gpt4free:
image: ghcr.io/xtekky/gpt4free:latest
shm_size: 2gb
build:
context: .
dockerfile: Dockerfile
cache_from:
- gpt4free:latest
ports:
- '1337:1337'
command: api
gpt4free-gui:
<<: *gpt4free
dockerfile: docker/Dockerfile
volumes:
- .:/app
ports:
- '8080:80'
command: gui
- '1337:1337'
- '7900:7900'
43 changes: 43 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
FROM selenium/node-chrome

ENV SE_SCREEN_WIDTH 1920
ENV G4F_LOGIN_URL http://localhost:7900/?autoconnect=1&resize=scale&password=secret
ENV PYTHONUNBUFFERED 1

USER root

# Python packages
RUN apt-get -qqy update \
&& apt-get -qqy install \
python3 \
python-is-python3 \
pip

# Cleanup
RUN rm -rf /var/lib/apt/lists/* /var/cache/apt/* \
&& apt-get -qyy autoremove \
&& apt-get -qyy clean

# Update entrypoint
COPY docker/supervisor.conf /etc/supervisor/conf.d/selenium.conf

# Change background image
COPY g4f.png /usr/share/images/fluxbox/ubuntu-light.png

# Switch user
USER 1200

# Set the working directory in the container.
WORKDIR /app

# Copy the project's requirements file into the container.
COPY requirements.txt /app/

# Upgrade pip for the latest features and install the project's Python dependencies.
RUN pip install --upgrade pip && pip install -r requirements.txt

# Copy the entire package into the container.
COPY g4f /app/g4f

# Expose ports
EXPOSE 80 1337
76 changes: 76 additions & 0 deletions docker/supervisor.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
[program:xvfb]
priority=0
command=/opt/bin/start-xvfb.sh
autostart=true
autorestart=true

;Logs
redirect_stderr=false
stdout_logfile=/var/log/supervisor/xvfb-stdout.log
stderr_logfile=/var/log/supervisor/xvfb-stderr.log
stdout_logfile_maxbytes=50MB
stderr_logfile_maxbytes=50MB
stdout_logfile_backups=5
stderr_logfile_backups=5
stdout_capture_maxbytes=50MB
stderr_capture_maxbytes=50MB

[program:vnc]
priority=5
command=/opt/bin/start-vnc.sh
autostart=true
autorestart=true

;Logs
redirect_stderr=false
stdout_logfile=/var/log/supervisor/vnc-stdout.log
stderr_logfile=/var/log/supervisor/vnc-stderr.log
stdout_logfile_maxbytes=50MB
stderr_logfile_maxbytes=50MB
stdout_logfile_backups=5
stderr_logfile_backups=5
stdout_capture_maxbytes=50MB
stderr_capture_maxbytes=50MB

[program:novnc]
priority=10
command=/opt/bin/start-novnc.sh
autostart=true
autorestart=true

;Logs
redirect_stderr=false
stdout_logfile=/var/log/supervisor/novnc-stdout.log
stderr_logfile=/var/log/supervisor/novnc-stderr.log
stdout_logfile_maxbytes=50MB
stderr_logfile_maxbytes=50MB
stdout_logfile_backups=5
stderr_logfile_backups=5
stdout_capture_maxbytes=50MB
stderr_capture_maxbytes=50MB

[program:g4f-cli]
priority=15
command=python -m g4f.cli api
directory=/app
stopasgroup=true
autostart=true
autorestart=true

;Logs (all Hub activity redirected to stdout so it can be seen through "docker logs"
redirect_stderr=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0

[program:g4f-gui]
priority=15
command=python -m g4f.cli gui
directory=/app
stopasgroup=true
autostart=true
autorestart=true

;Logs (all Hub activity redirected to stdout so it can be seen through "docker logs"
redirect_stderr=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
Binary file added g4f.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 5 additions & 2 deletions g4f/Provider/Bing.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,11 @@ async def delete_conversation(session: ClientSession, conversation: Conversation
"optionsSets": ["autosave"]
}
async with session.post(url, json=json, proxy=proxy) as response:
response = await response.json()
return response["result"]["value"] == "Success"
try:
response = await response.json()
return response["result"]["value"] == "Success"
except:
return False

class Defaults:
delimiter = "\x1e"
Expand Down
9 changes: 4 additions & 5 deletions g4f/Provider/PerplexityAi.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from __future__ import annotations

import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

from ..typing import CreateResult, Messages
from .base_provider import BaseProvider
Expand All @@ -27,11 +31,6 @@ def create_completion(
**kwargs
) -> CreateResult:
with WebDriverSession(webdriver, "", virtual_display=virtual_display, proxy=proxy) as driver:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

prompt = format_prompt(messages)

driver.get(f"{cls.url}/")
Expand Down
9 changes: 7 additions & 2 deletions g4f/Provider/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import random
import string
import secrets
import os
from os import path
from asyncio import AbstractEventLoop
from platformdirs import user_config_dir
Expand All @@ -18,7 +19,7 @@
edge,
vivaldi,
firefox,
BrowserCookieError
_LinuxPasswordManager
)

from ..typing import Dict, Messages
Expand Down Expand Up @@ -81,6 +82,10 @@ def open_urls_in_browser(browser):
except webbrowser.Error:
continue

# Check for broken dbus address in docker image
if os.environ.get('DBUS_SESSION_BUS_ADDRESS') == "/dev/null":
_LinuxPasswordManager.get_password = lambda a, b: b"secret"

# Load cookies for a domain from all supported browsers.
# Cache the results in the "_cookies" variable.
def get_cookies(domain_name=''):
Expand All @@ -100,7 +105,7 @@ def g4f(domain_name):
for cookie in cookie_jar:
if cookie.name not in cookies:
cookies[cookie.name] = cookie.value
except BrowserCookieError as e:
except:
pass
_cookies[domain_name] = cookies
return _cookies[domain_name]
Expand Down
16 changes: 10 additions & 6 deletions g4f/Provider/needs_auth/Bard.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from __future__ import annotations

import time
import os
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

from ...typing import CreateResult, Messages
from ..base_provider import BaseProvider
Expand All @@ -27,10 +32,6 @@ def create_completion(
prompt = format_prompt(messages)
session = WebDriverSession(webdriver, user_data_dir, headless, proxy=proxy)
with session as driver:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

try:
driver.get(f"{cls.url}/chat")
wait = WebDriverWait(driver, 10 if headless else 240)
Expand All @@ -40,6 +41,9 @@ def create_completion(
if not webdriver:
driver = session.reopen()
driver.get(f"{cls.url}/chat")
login_url = os.environ.get("G4F_LOGIN_URL")
if login_url:
yield f"Please login: [Google Bard]({login_url})\n\n"
wait = WebDriverWait(driver, 240)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.ql-editor.textarea")))
else:
Expand All @@ -61,8 +65,8 @@ def create_completion(
driver.execute_script(script)

# Submit prompt
driver.find_element(By.CSS_SELECTOR, "div.ql-editor.ql-blank.textarea").send_keys(prompt)
driver.find_element(By.CSS_SELECTOR, "button.send-button").click()
driver.find_element(By.CSS_SELECTOR, "div.ql-editor.textarea").send_keys(prompt)
driver.find_element(By.CSS_SELECTOR, "div.ql-editor.textarea").send_keys(Keys.ENTER)

# Yield response
while True:
Expand Down
6 changes: 2 additions & 4 deletions g4f/Provider/needs_auth/HuggingChat.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

class HuggingChat(AsyncGeneratorProvider):
url = "https://huggingface.co/chat"
needs_auth = True
working = True
model = "meta-llama/Llama-2-70b-chat-hf"

Expand All @@ -22,12 +21,11 @@ async def create_async_generator(
messages: Messages,
stream: bool = True,
proxy: str = None,
web_search: bool = False,
cookies: dict = None,
**kwargs
) -> AsyncResult:
model = model if model else cls.model
if proxy and "://" not in proxy:
proxy = f"http://{proxy}"
if not cookies:
cookies = get_cookies(".huggingface.co")

Expand All @@ -46,7 +44,7 @@ async def create_async_generator(
"inputs": format_prompt(messages),
"is_retry": False,
"response_id": str(uuid.uuid4()),
"web_search": False
"web_search": web_search
}
async with session.post(f"{cls.url}/conversation/{conversation_id}", json=send, proxy=proxy) as response:
async for line in response.content:
Expand Down
Loading

0 comments on commit f962993

Please sign in to comment.