summaryrefslogtreecommitdiffstats
path: root/g4f/gui/server
diff options
context:
space:
mode:
authorHeiner Lohaus <hlohaus@users.noreply.github.com>2024-11-17 11:06:37 +0100
committerHeiner Lohaus <hlohaus@users.noreply.github.com>2024-11-17 11:06:37 +0100
commitea1448001df9c66f05a52618eb3e40a88df5efe8 (patch)
tree3f113ea8beb7c43920871019512aeb8de9d1b4f7 /g4f/gui/server
parentFix api streaming, fix AsyncClient (#2357) (diff)
downloadgpt4free-ea1448001df9c66f05a52618eb3e40a88df5efe8.tar
gpt4free-ea1448001df9c66f05a52618eb3e40a88df5efe8.tar.gz
gpt4free-ea1448001df9c66f05a52618eb3e40a88df5efe8.tar.bz2
gpt4free-ea1448001df9c66f05a52618eb3e40a88df5efe8.tar.lz
gpt4free-ea1448001df9c66f05a52618eb3e40a88df5efe8.tar.xz
gpt4free-ea1448001df9c66f05a52618eb3e40a88df5efe8.tar.zst
gpt4free-ea1448001df9c66f05a52618eb3e40a88df5efe8.zip
Diffstat (limited to 'g4f/gui/server')
-rw-r--r--g4f/gui/server/api.py32
1 files changed, 17 insertions, 15 deletions
diff --git a/g4f/gui/server/api.py b/g4f/gui/server/api.py
index dafcb5d4..f03d2048 100644
--- a/g4f/gui/server/api.py
+++ b/g4f/gui/server/api.py
@@ -6,14 +6,14 @@ import uuid
import asyncio
import time
from aiohttp import ClientSession
-from typing import Iterator, Optional, AsyncIterator, Union
+from typing import Iterator, Optional
from flask import send_from_directory
from g4f import version, models
from g4f import get_last_provider, ChatCompletion
from g4f.errors import VersionNotFoundError
from g4f.typing import Cookies
-from g4f.image import ImagePreview, ImageResponse, is_accepted_format
+from g4f.image import ImagePreview, ImageResponse, is_accepted_format, extract_data_uri
from g4f.requests.aiohttp import get_connector
from g4f.Provider import ProviderType, __providers__, __map__
from g4f.providers.base_provider import ProviderModelMixin, FinishReason
@@ -31,7 +31,6 @@ def ensure_images_dir():
conversations: dict[dict[str, BaseConversation]] = {}
-
class Api:
@staticmethod
def get_models() -> list[str]:
@@ -176,18 +175,22 @@ class Api:
connector=get_connector(None, os.environ.get("G4F_PROXY")),
cookies=cookies
) as session:
- async def copy_image(image):
- async with session.get(image) as response:
- target = os.path.join(images_dir, f"{int(time.time())}_{str(uuid.uuid4())}")
+ async def copy_image(image: str) -> str:
+ target = os.path.join(images_dir, f"{int(time.time())}_{str(uuid.uuid4())}")
+ if image.startswith("data:"):
with open(target, "wb") as f:
- async for chunk in response.content.iter_any():
- f.write(chunk)
- with open(target, "rb") as f:
- extension = is_accepted_format(f.read(12)).split("/")[-1]
- extension = "jpg" if extension == "jpeg" else extension
- new_target = f"{target}.{extension}"
- os.rename(target, new_target)
- return f"/images/{os.path.basename(new_target)}"
+ f.write(extract_data_uri(image))
+ else:
+ async with session.get(image) as response:
+ with open(target, "wb") as f:
+ async for chunk in response.content.iter_any():
+ f.write(chunk)
+ with open(target, "rb") as f:
+ extension = is_accepted_format(f.read(12)).split("/")[-1]
+ extension = "jpg" if extension == "jpeg" else extension
+ new_target = f"{target}.{extension}"
+ os.rename(target, new_target)
+ return f"/images/{os.path.basename(new_target)}"
return await asyncio.gather(*[copy_image(image) for image in images])
@@ -197,7 +200,6 @@ class Api:
response_type: content
}
-
def get_error_message(exception: Exception) -> str:
message = f"{type(exception).__name__}: {exception}"
provider = get_last_provider()