Files
gpt4free/g4f/gui/server/website.py
hlohaus 3775c1e06d fix: improve error handling and add type checks in various providers and API
- Updated error handling in g4f/Provider/DDG.py to raise ResponseError instead of yield error strings
- Replaced yield statements with raises in g4f/Provider/DDG.py for HTTP and response errors
- Added response raising in g4f/Provider/DeepInfraChat.py for image upload responses
- Included model alias validation and error raising in g4f/Provider/hf/HuggingFaceMedia.py
- Corrected model alias dictionary key in g4f/Provider/hf_space/StabilityAI_SD35Large.py
- Ensured referrer parameter default value in g4f/Provider/PollinationsImage.py
- Removed duplicate imports and adjusted get_models method in g4f/Provider/har/__init__.py
- Modified g4f/gui/server/api.py to remove unused conversation parameter in _create_response_stream
- Fixed logic to handle single exception in g4f/providers/retry_provider.py
- Added missing import of JsonConversation in g4f/providers/retry_provider.py
- Corrected stream_read_files to replace extension in return string in g4f/tools/files.py
2025-05-17 10:02:13 +02:00

88 lines
3.1 KiB
Python

from __future__ import annotations
import os
import requests
from datetime import datetime
from flask import send_from_directory, redirect
from ...image.copy_images import secure_filename
from ...cookies import get_cookies_dir
from ...errors import VersionNotFoundError
from ... import version
GPT4FREE_URL = "https://gpt4free.github.io"
DIST_DIR = "./gpt4free.github.io/dist"
def redirect_home():
return redirect('/chat')
def render(filename = "chat", add_origion = True):
if os.path.exists(DIST_DIR):
add_origion = False
path = os.path.abspath(os.path.join(os.path.dirname(DIST_DIR), (filename + ("" if "." in filename else ".html"))))
return send_from_directory(os.path.dirname(path), os.path.basename(path))
try:
latest_version = version.utils.latest_version
except VersionNotFoundError:
latest_version = version.utils.current_version
today = datetime.today().strftime('%Y-%m-%d')
cache_dir = os.path.join(get_cookies_dir(), ".gui_cache")
cache_file = os.path.join(cache_dir, f"{today}.{secure_filename(filename)}.{version.utils.current_version}-{latest_version}{'.live' if add_origion else ''}.html")
if not os.path.exists(cache_file):
os.makedirs(cache_dir, exist_ok=True)
html = requests.get(f"{GPT4FREE_URL}/{filename}.html").text
if add_origion:
html = html.replace("../dist/", f"dist/")
html = html.replace("\"dist/", f"\"{GPT4FREE_URL}/dist/")
with open(cache_file, 'w', encoding='utf-8') as f:
f.write(html)
return send_from_directory(os.path.abspath(cache_dir), os.path.basename(cache_file))
class Website:
def __init__(self, app) -> None:
self.app = app
self.routes = {
'/': {
'function': self._index,
'methods': ['GET', 'POST']
},
'/chat/': {
'function': self._chat,
'methods': ['GET', 'POST']
},
'/qrcode.html': {
'function': self._qrcode,
'methods': ['GET', 'POST']
},
'/background.html': {
'function': self._background,
'methods': ['GET', 'POST']
},
'/chat/<conversation_id>': {
'function': self._chat,
'methods': ['GET', 'POST']
},
'/media/': {
'function': redirect_home,
'methods': ['GET', 'POST']
},
'/dist/<path:name>': {
'function': self._dist,
'methods': ['GET']
},
}
def _index(self, filename = "index"):
return render(filename)
def _qrcode(self, filename = "qrcode"):
return render(filename)
def _background(self, filename = "background"):
return render(filename)
def _chat(self, filename = "chat"):
filename = "chat/index" if filename == 'chat' else secure_filename(filename)
return render(filename)
def _dist(self, name: str):
return send_from_directory(os.path.abspath(DIST_DIR), name)