mirror of
https://github.com/xtekky/gpt4free.git
synced 2025-12-06 02:30:41 -08:00
Fix aarch64 compatibility issue with curl_cffi imports
Co-authored-by: hlohaus <983577+hlohaus@users.noreply.github.com>
This commit is contained in:
parent
0342b13ba0
commit
31f408ed87
1 changed files with 136 additions and 106 deletions
|
|
@ -1,21 +1,38 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from curl_cffi.requests import AsyncSession, Response
|
||||
try:
|
||||
from curl_cffi.requests import AsyncSession, Response
|
||||
has_curl_cffi = True
|
||||
except ImportError:
|
||||
# Fallback for systems where curl_cffi is not available or causes illegal instruction errors
|
||||
from typing import Any
|
||||
class AsyncSession:
|
||||
def __init__(self, *args, **kwargs):
|
||||
raise ImportError("curl_cffi is not available on this platform")
|
||||
class Response:
|
||||
pass
|
||||
has_curl_cffi = False
|
||||
|
||||
if has_curl_cffi:
|
||||
try:
|
||||
from curl_cffi import CurlMime
|
||||
has_curl_mime = True
|
||||
except ImportError:
|
||||
except ImportError:
|
||||
has_curl_mime = False
|
||||
try:
|
||||
try:
|
||||
from curl_cffi import CurlWsFlag
|
||||
has_curl_ws = True
|
||||
except ImportError:
|
||||
except ImportError:
|
||||
has_curl_ws = False
|
||||
else:
|
||||
has_curl_mime = False
|
||||
has_curl_ws = False
|
||||
from typing import AsyncGenerator, Any
|
||||
from functools import partialmethod
|
||||
import json
|
||||
|
||||
class StreamResponse:
|
||||
if has_curl_cffi:
|
||||
class StreamResponse:
|
||||
"""
|
||||
A wrapper class for handling asynchronous streaming responses.
|
||||
|
||||
|
|
@ -77,7 +94,7 @@ class StreamResponse:
|
|||
"""Asynchronously exit the runtime context for the response object."""
|
||||
await self.inner.aclose()
|
||||
|
||||
class StreamSession(AsyncSession):
|
||||
class StreamSession(AsyncSession):
|
||||
"""
|
||||
An asynchronous session class for handling HTTP requests with streaming.
|
||||
|
||||
|
|
@ -87,7 +104,7 @@ class StreamSession(AsyncSession):
|
|||
def request(
|
||||
self, method: str, url: str, ssl = None, **kwargs
|
||||
) -> StreamResponse:
|
||||
if kwargs.get("data") and isinstance(kwargs.get("data"), CurlMime):
|
||||
if has_curl_mime and kwargs.get("data") and isinstance(kwargs.get("data"), CurlMime):
|
||||
kwargs["multipart"] = kwargs.pop("data")
|
||||
"""Create and return a StreamResponse object for the given HTTP request."""
|
||||
return StreamResponse(super().request(method, url, stream=True, verify=ssl, **kwargs))
|
||||
|
|
@ -107,19 +124,28 @@ class StreamSession(AsyncSession):
|
|||
delete = partialmethod(request, "DELETE")
|
||||
options = partialmethod(request, "OPTIONS")
|
||||
|
||||
if not has_curl_mime:
|
||||
class FormData():
|
||||
def __init__(self) -> None:
|
||||
raise RuntimeError("CurlMimi in curl_cffi is missing | pip install -U curl_cffi")
|
||||
else:
|
||||
# Fallback classes when curl_cffi is not available
|
||||
class StreamResponse:
|
||||
def __init__(self, *args, **kwargs):
|
||||
raise ImportError("curl_cffi is not available on this platform")
|
||||
|
||||
class StreamSession:
|
||||
def __init__(self, *args, **kwargs):
|
||||
raise ImportError("curl_cffi is not available on this platform")
|
||||
|
||||
if has_curl_cffi and has_curl_mime:
|
||||
class FormData(CurlMime):
|
||||
def add_field(self, name, data=None, content_type: str = None, filename: str = None) -> None:
|
||||
self.addpart(name, content_type=content_type, filename=filename, data=data)
|
||||
else:
|
||||
class FormData():
|
||||
def __init__(self) -> None:
|
||||
raise RuntimeError("curl_cffi FormData is not available on this platform")
|
||||
|
||||
class WebSocket():
|
||||
if has_curl_cffi and has_curl_ws:
|
||||
class WebSocket():
|
||||
def __init__(self, session, url, **kwargs) -> None:
|
||||
if not has_curl_ws:
|
||||
raise RuntimeError("CurlWsFlag in curl_cffi is missing | pip install -U curl_cffi")
|
||||
self.session: StreamSession = session
|
||||
self.url: str = url
|
||||
del kwargs["autoping"]
|
||||
|
|
@ -140,3 +166,7 @@ class WebSocket():
|
|||
async def send_str(self, data: str):
|
||||
method = self.inner.asend if hasattr(self.inner, "asend") else self.inner.send
|
||||
await method(data.encode(), CurlWsFlag.TEXT)
|
||||
else:
|
||||
class WebSocket():
|
||||
def __init__(self, *args, **kwargs) -> None:
|
||||
raise RuntimeError("curl_cffi WebSocket is not available on this platform")
|
||||
Loading…
Add table
Add a link
Reference in a new issue