mirror of
https://github.com/xtekky/gpt4free.git
synced 2025-12-06 02:30:41 -08:00
Refactor Cloudflare and LMArena providers to enhance authentication handling and improve WebSocket communication
This commit is contained in:
parent
63315df501
commit
da6c00e2a2
6 changed files with 58 additions and 74 deletions
|
|
@ -1,5 +1,6 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import asyncio
|
||||
from typing import Optional
|
||||
from aiohttp import ClientSession, ClientTimeout
|
||||
|
|
@ -10,13 +11,13 @@ from aiohttp import ClientSession
|
|||
try:
|
||||
import nodriver
|
||||
from nodriver.core.connection import ProtocolException
|
||||
has_nodriver = True
|
||||
except:
|
||||
pass
|
||||
has_nodriver = False
|
||||
|
||||
from ...typing import Messages, AsyncResult
|
||||
from ...providers.response import VideoResponse, Reasoning, ContinueResponse, ProviderInfo
|
||||
from ...requests import get_nodriver
|
||||
from ...errors import MissingRequirementsError
|
||||
from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin
|
||||
from ..helper import format_media_prompt
|
||||
from ... import debug
|
||||
|
|
@ -53,7 +54,7 @@ class Video(AsyncGeneratorProvider, ProviderModelMixin):
|
|||
"sora": "https://sora.chatgpt.com/explore",
|
||||
#"veo": "https://aistudio.google.com/generate-video"
|
||||
}
|
||||
api_url = f"{PUBLIC_URL}/backend-api/v2/create?provider=Video&cache=true&prompt="
|
||||
api_path = f"?provider=Video&cache=true&prompt="
|
||||
drive_url = "https://www.googleapis.com/drive/v3/"
|
||||
|
||||
active_by_default = True
|
||||
|
|
@ -62,10 +63,11 @@ class Video(AsyncGeneratorProvider, ProviderModelMixin):
|
|||
video_models = models
|
||||
|
||||
needs_auth = True
|
||||
working = True
|
||||
working = has_nodriver
|
||||
|
||||
browser = None
|
||||
stop_browser = None
|
||||
share_url: Optional[str] = None
|
||||
|
||||
@classmethod
|
||||
async def create_async_generator(
|
||||
|
|
@ -77,6 +79,8 @@ class Video(AsyncGeneratorProvider, ProviderModelMixin):
|
|||
aspect_ratio: str = None,
|
||||
**kwargs
|
||||
) -> AsyncResult:
|
||||
if cls.share_url is None:
|
||||
cls.share_url = os.getenv("G4F_SHARE_URL")
|
||||
if not model:
|
||||
model = cls.default_model
|
||||
if model not in cls.video_models:
|
||||
|
|
@ -94,10 +98,12 @@ class Video(AsyncGeneratorProvider, ProviderModelMixin):
|
|||
yield Reasoning(label="Open browser")
|
||||
browser, stop_browser = await get_nodriver(proxy=proxy)
|
||||
except Exception as e:
|
||||
if cls.share_url is None:
|
||||
raise
|
||||
debug.error(f"Error getting nodriver:", e)
|
||||
async with ClientSession() as session:
|
||||
yield Reasoning(label="Generating")
|
||||
async with session.get(cls.api_url + quote(prompt)) as response:
|
||||
async with session.get(f"{cls.share_url}{cls.api_path + quote(prompt)}") as response:
|
||||
if not response.ok:
|
||||
debug.error(f"Failed to generate Video: {response.status}")
|
||||
else:
|
||||
|
|
@ -108,7 +114,7 @@ class Video(AsyncGeneratorProvider, ProviderModelMixin):
|
|||
return
|
||||
yield VideoResponse(str(response.url), prompt)
|
||||
return
|
||||
raise MissingRequirementsError("Video provider requires a browser to be installed.")
|
||||
raise
|
||||
page = None
|
||||
try:
|
||||
yield ContinueResponse("Timeout waiting for Video URL")
|
||||
|
|
@ -123,7 +129,7 @@ class Video(AsyncGeneratorProvider, ProviderModelMixin):
|
|||
RequestConfig.headers = {}
|
||||
for key, value in event.request.headers.items():
|
||||
RequestConfig.headers[key.lower()] = value
|
||||
for _, urls in RequestConfig.urls.items():
|
||||
for urls in RequestConfig.urls.values():
|
||||
if event.request.url in urls:
|
||||
return
|
||||
debug.log(f"Adding URL: {event.request.url}")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue