Files
gpt4free/g4f/requests/__init__.py
Ammar ddcdcef882 update Yupp and LMArena providers (#3291)
* Handle Cloudflare WAF errors in Qwen provider

Added detection and raising of CloudflareError when the response indicates an Aliyun WAF block. This improves error handling for cases where requests are blocked by Cloudflare.

* Improve Qwen and LMArena provider authentication handling

Refactors Qwen provider to better manage authentication cookies and cache, including fallback and refresh logic for Cloudflare errors and rate limits. Adds use of AuthFileMixin to Qwen, improves argument retrieval from cache or nodriver, and ensures cookies are merged after requests. Updates LMArena to prioritize args from kwargs before reading from cache, improving flexibility and reliability in authentication.

* Improve LMArena provider recaptcha handling and error logging

Refactors the LMArena provider to better handle recaptcha token acquisition and error cases, including new async methods for recaptcha retrieval and improved error logging. Updates dependencies and imports, and enhances the raise_for_status utility to detect LMArena-specific recaptcha validation failures.

* Add image upload and caching to LMArena provider

Introduces image upload support with caching in the LMArena provider by implementing a prepare_images method. Images are uploaded, cached by hash, and attached to user messages for models supporting vision. Refactors attachment handling to use the new upload logic and improves code formatting and error handling.

* Update and expand model definitions in LMArena.py

Replaces the previous 'models' list with an updated and expanded set of model definitions, including new fields such as 'name', 'rank', and 'rankByModality'. This change adds new models, updates capabilities, and provides more detailed metadata for each model, improving model selection and feature support.

* Improve reCAPTCHA handling and set default timeout

Refactors the reCAPTCHA execution to use the enterprise.ready callback and adds error handling for token retrieval. Also sets a default timeout of 5 minutes for StreamSession if not provided.

* Update LMArena.py

* StreamSession

* Improve error logging for Qwen Cloudflare errors

Replaces a generic debug log with a more detailed error log that includes the exception message when a CloudflareError is caught in the Qwen provider. This enhances troubleshooting by providing more context in logs.

* generate ssxmod

* Improve error handling for Qwen provider responses

Adds checks for JSON error responses and raises RuntimeError when 'success' is false or a 'code' is present in the response data. Also refines HTML error detection logic in raise_for_status.

* Update fingerprint.py

* Update Yupp.py

for test only

* Update Yupp.py

* Add Qwen bx-ua header generator and update Qwen provider

Introduces g4f/Provider/qwen/generate_ua.py for generating bx-ua headers, including AES encryption and fingerprinting logic. Updates Qwen provider to support dynamic UA/cookie handling and refactors image preparation in LMArena to handle empty media lists. Minor cleanup in cookie_generator.py and preparation for integrating bx-ua header in Qwen requests.

* Update LMArena.py

* Update LMArena.py

* Update LMArena.py

* Add user_info method to Yupp provider

Introduces a new async class method user_info to fetch and parse user details, credits, and model information from Yupp. Updates create_async_generator to yield user_info at the start of the conversation flow. Also fixes a bug in get_last_user_message call by passing a boolean for the prompt argument.

* Update Yupp.py

* Update models.py

* Update Yupp.py

* Enhance LMArena action ID handling and parsing

Refactored LMArena to dynamically extract and update action IDs from HTML/JS, replacing hardcoded values with a class-level dictionary. Added HTML parsing logic to load available actions and models, improving maintainability and adaptability to backend changes. Minor cleanup and improved code structure in Yupp and LMArena providers.

* Update LMArena.py
2025-12-22 13:43:35 +01:00

292 lines
9.7 KiB
Python

from __future__ import annotations
import asyncio
import json
import os
import random
import time
from contextlib import asynccontextmanager
from http.cookies import Morsel
from pathlib import Path
from typing import Iterator, AsyncIterator
from urllib.parse import urlparse
try:
from curl_cffi.requests import Session, Response
from .curl_cffi import StreamResponse, StreamSession, FormData
has_curl_cffi = True
except ImportError:
from typing import Type as Response
from .aiohttp import StreamResponse, StreamSession, FormData
has_curl_cffi = False
try:
import webview
has_webview = True
except ImportError:
has_webview = False
try:
import nodriver
from nodriver.cdp.network import CookieParam
from nodriver.core.config import find_chrome_executable
from nodriver import Browser, Tab, util
has_nodriver = True
except ImportError:
from typing import Type as Browser
from typing import Type as Tab
has_nodriver = False
try:
from platformdirs import user_config_dir
has_platformdirs = True
except ImportError:
has_platformdirs = False
from .. import debug
from .raise_for_status import raise_for_status
from ..errors import MissingRequirementsError
from ..typing import Cookies
from ..cookies import BrowserConfig, get_cookies_dir
from .defaults import DEFAULT_HEADERS, WEBVIEW_HAEDERS
if not has_curl_cffi:
class Session:
def __init__(self, **kwargs):
raise MissingRequirementsError('Install "curl_cffi" package | pip install -U curl_cffi')
async def get_args_from_webview(url: str) -> dict:
if not has_webview:
raise MissingRequirementsError('Install "webview" package')
window = webview.create_window("", url, hidden=True)
await asyncio.sleep(2)
body = None
while body is None:
try:
await asyncio.sleep(1)
body = window.dom.get_element("body:not(.no-js)")
except:
...
headers = {
**WEBVIEW_HAEDERS,
"User-Agent": window.evaluate_js("this.navigator.userAgent"),
"Accept-Language": window.evaluate_js("this.navigator.language"),
"Referer": window.real_url
}
cookies = [list(*cookie.items()) for cookie in window.get_cookies()]
cookies = {name: cookie.value for name, cookie in cookies}
window.destroy()
return {"headers": headers, "cookies": cookies}
def get_cookie_params_from_dict(cookies: Cookies, url: str = None, domain: str = None) -> list[CookieParam]:
[CookieParam.from_json({
"name": key,
"value": value,
"url": url,
"domain": domain
}) for key, value in cookies.items()]
async def get_args_from_nodriver(
url: str,
proxy: str = None,
timeout: int = 120,
wait_for: str = None,
callback: callable = None,
cookies: Cookies = None,
browser: Browser = None,
user_data_dir: str = "nodriver",
browser_args: list = None
) -> dict:
if browser is None:
browser, stop_browser = await get_nodriver(proxy=proxy, timeout=timeout, user_data_dir=user_data_dir, browser_args=browser_args)
else:
def stop_browser():
pass
try:
debug.log(f"Open nodriver with url: {url}")
if cookies is None:
cookies = {}
else:
domain = urlparse(url).netloc
await browser.cookies.set_all(get_cookie_params_from_dict(cookies, url=url, domain=domain))
page = await browser.get(url)
user_agent = await page.evaluate("window.navigator.userAgent", return_by_value=True)
while not await page.evaluate("document.querySelector('body:not(.no-js)')"):
await asyncio.sleep(1)
if wait_for is not None:
await page.wait_for(wait_for, timeout=timeout)
if callback is not None:
await callback(page)
for c in await page.send(nodriver.cdp.network.get_cookies([url])):
cookies[c.name] = c.value
stop_browser()
return {
"impersonate": "chrome",
"cookies": cookies,
"headers": {
**DEFAULT_HEADERS,
"user-agent": user_agent,
"referer": f"{url.rstrip('/')}/",
},
"proxy": proxy,
}
except:
stop_browser()
raise
def merge_cookies(cookies: Iterator[Morsel], response: Response) -> Cookies:
if cookies is None:
cookies = {}
if hasattr(response.cookies, "jar"):
for cookie in response.cookies.jar:
cookies[cookie.name] = cookie.value
else:
for key, value in response.cookies.items():
cookies[key] = value
return cookies
def set_browser_executable_path(browser_executable_path: str):
BrowserConfig.browser_executable_path = browser_executable_path
async def get_nodriver(
proxy: str = None,
user_data_dir="nodriver",
timeout: int = 300,
browser_executable_path: str = None,
**kwargs
) -> tuple[Browser, callable]:
if not has_nodriver:
raise MissingRequirementsError(
'Install "nodriver" and "platformdirs" package | pip install -U nodriver platformdirs')
user_data_dir = user_config_dir(f"g4f-{user_data_dir}") if user_data_dir and has_platformdirs else None
if browser_executable_path is None:
browser_executable_path = BrowserConfig.browser_executable_path
if browser_executable_path is None:
try:
browser_executable_path = find_chrome_executable()
except FileNotFoundError:
# Default to Edge if Chrome is not available.
browser_executable_path = "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"
if not os.path.exists(browser_executable_path):
browser_executable_path = None
debug.log(f"Browser executable path: {browser_executable_path}")
lock_file = Path(get_cookies_dir()) / ".nodriver_is_open"
if user_data_dir:
lock_file.parent.mkdir(exist_ok=True)
# Implement a short delay (milliseconds) to prevent race conditions.
await asyncio.sleep(0.1 * random.randint(0, 50))
if lock_file.exists():
opend_at = float(lock_file.read_text())
time_open = time.time() - opend_at
if timeout * 2 > time_open:
debug.log(f"Nodriver: Browser is already in use since {time_open} secs.")
debug.log("Lock file:", lock_file)
for idx in range(timeout):
if lock_file.exists():
await asyncio.sleep(1)
else:
break
if idx == timeout - 1:
debug.log("Timeout reached, nodriver is still in use.")
raise TimeoutError("Nodriver is already in use, please try again later.")
else:
debug.log(f"Nodriver: Browser was opened {time_open} secs ago, closing it.")
BrowserConfig.stop_browser()
lock_file.unlink(missing_ok=True)
lock_file.write_text(str(time.time()))
debug.log(f"Open nodriver with user_dir: {user_data_dir}")
try:
browser_args = kwargs.pop("browser_args", None) or ["--no-sandbox"]
if BrowserConfig.port:
browser_executable_path = "/bin/google-chrome"
browser = await nodriver.start(
user_data_dir=user_data_dir,
browser_args=[*browser_args, f"--proxy-server={proxy}"] if proxy else browser_args,
browser_executable_path=browser_executable_path,
port=BrowserConfig.port,
host=BrowserConfig.host,
**kwargs
)
except FileNotFoundError as e:
raise MissingRequirementsError(e)
except Exception as e:
if util.get_registered_instances():
debug.error(e)
browser = util.get_registered_instances().pop()
else:
raise
def on_stop():
try:
if BrowserConfig.port is None and browser.connection:
browser.stop()
except:
pass
finally:
if user_data_dir:
lock_file.unlink(missing_ok=True)
BrowserConfig.stop_browser = on_stop
return browser, on_stop
@asynccontextmanager
async def get_nodriver_session(**kwargs):
browser, stop_browser = await get_nodriver(**kwargs)
yield browser
stop_browser()
async def sse_stream(iter_lines: AsyncIterator[bytes]) -> AsyncIterator[dict]:
if hasattr(iter_lines, "content"):
iter_lines = iter_lines.content
elif hasattr(iter_lines, "iter_lines"):
iter_lines = iter_lines.iter_lines()
async for line in iter_lines:
if line.startswith(b"data:"):
rest = line[5:].strip()
if not rest:
continue
if rest.startswith(b"[DONE]"):
break
try:
yield json.loads(rest)
except json.JSONDecodeError:
raise ValueError(f"Invalid JSON data: {rest}")
async def iter_lines(iter_response: AsyncIterator[bytes], delimiter=None):
"""
iterate streaming content line by line, separated by ``\\n``.
Copied from: https://requests.readthedocs.io/en/latest/_modules/requests/models/
which is under the License: Apache 2.0
"""
pending = None
async for chunk in iter_response:
if pending is not None:
chunk = pending + chunk
lines = chunk.split(delimiter) if delimiter else chunk.splitlines()
pending = (
lines.pop()
if lines and lines[-1] and chunk and lines[-1][-1] == chunk[-1]
else None
)
for line in lines:
yield line
if pending is not None:
yield pending