mirror of
https://github.com/xtekky/gpt4free.git
synced 2026-01-02 23:33:43 -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,6 +1,19 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
try:
|
||||||
from curl_cffi.requests import AsyncSession, Response
|
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:
|
try:
|
||||||
from curl_cffi import CurlMime
|
from curl_cffi import CurlMime
|
||||||
has_curl_mime = True
|
has_curl_mime = True
|
||||||
|
|
@ -11,10 +24,14 @@ try:
|
||||||
has_curl_ws = True
|
has_curl_ws = True
|
||||||
except ImportError:
|
except ImportError:
|
||||||
has_curl_ws = False
|
has_curl_ws = False
|
||||||
|
else:
|
||||||
|
has_curl_mime = False
|
||||||
|
has_curl_ws = False
|
||||||
from typing import AsyncGenerator, Any
|
from typing import AsyncGenerator, Any
|
||||||
from functools import partialmethod
|
from functools import partialmethod
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
if has_curl_cffi:
|
||||||
class StreamResponse:
|
class StreamResponse:
|
||||||
"""
|
"""
|
||||||
A wrapper class for handling asynchronous streaming responses.
|
A wrapper class for handling asynchronous streaming responses.
|
||||||
|
|
@ -87,7 +104,7 @@ class StreamSession(AsyncSession):
|
||||||
def request(
|
def request(
|
||||||
self, method: str, url: str, ssl = None, **kwargs
|
self, method: str, url: str, ssl = None, **kwargs
|
||||||
) -> StreamResponse:
|
) -> 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")
|
kwargs["multipart"] = kwargs.pop("data")
|
||||||
"""Create and return a StreamResponse object for the given HTTP request."""
|
"""Create and return a StreamResponse object for the given HTTP request."""
|
||||||
return StreamResponse(super().request(method, url, stream=True, verify=ssl, **kwargs))
|
return StreamResponse(super().request(method, url, stream=True, verify=ssl, **kwargs))
|
||||||
|
|
@ -107,19 +124,28 @@ class StreamSession(AsyncSession):
|
||||||
delete = partialmethod(request, "DELETE")
|
delete = partialmethod(request, "DELETE")
|
||||||
options = partialmethod(request, "OPTIONS")
|
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:
|
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):
|
class FormData(CurlMime):
|
||||||
def add_field(self, name, data=None, content_type: str = None, filename: str = None) -> None:
|
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)
|
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")
|
||||||
|
|
||||||
|
if has_curl_cffi and has_curl_ws:
|
||||||
class WebSocket():
|
class WebSocket():
|
||||||
def __init__(self, session, url, **kwargs) -> None:
|
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.session: StreamSession = session
|
||||||
self.url: str = url
|
self.url: str = url
|
||||||
del kwargs["autoping"]
|
del kwargs["autoping"]
|
||||||
|
|
@ -140,3 +166,7 @@ class WebSocket():
|
||||||
async def send_str(self, data: str):
|
async def send_str(self, data: str):
|
||||||
method = self.inner.asend if hasattr(self.inner, "asend") else self.inner.send
|
method = self.inner.asend if hasattr(self.inner, "asend") else self.inner.send
|
||||||
await method(data.encode(), CurlWsFlag.TEXT)
|
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