diff options
author | kqlio67 <kqlio67@users.noreply.github.com> | 2024-09-04 19:56:07 +0200 |
---|---|---|
committer | kqlio67 <kqlio67@users.noreply.github.com> | 2024-09-04 19:56:07 +0200 |
commit | 7f05d158556dce53bfc38157a4f1dd5e6453d0fc (patch) | |
tree | dbe230d7ccba56a95b605b9f57be9e2c6d5028db | |
parent | Adding new providers AiChats.py Nexra.py Snova.py (diff) | |
download | gpt4free-7f05d158556dce53bfc38157a4f1dd5e6453d0fc.tar gpt4free-7f05d158556dce53bfc38157a4f1dd5e6453d0fc.tar.gz gpt4free-7f05d158556dce53bfc38157a4f1dd5e6453d0fc.tar.bz2 gpt4free-7f05d158556dce53bfc38157a4f1dd5e6453d0fc.tar.lz gpt4free-7f05d158556dce53bfc38157a4f1dd5e6453d0fc.tar.xz gpt4free-7f05d158556dce53bfc38157a4f1dd5e6453d0fc.tar.zst gpt4free-7f05d158556dce53bfc38157a4f1dd5e6453d0fc.zip |
-rw-r--r-- | g4f/gui/server/api.py | 41 |
1 files changed, 28 insertions, 13 deletions
diff --git a/g4f/gui/server/api.py b/g4f/gui/server/api.py index 3da0fe17..78b41357 100644 --- a/g4f/gui/server/api.py +++ b/g4f/gui/server/api.py @@ -6,6 +6,7 @@ import os.path import uuid import asyncio import time +import base64 from aiohttp import ClientSession from typing import Iterator, Optional from flask import send_from_directory @@ -195,18 +196,32 @@ class Api(): 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())}") - 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]) + if image.startswith("data:"): + # Обробка URL-адреси даних + data_uri_parts = image.split(",") + if len(data_uri_parts) == 2: + content_type, base64_data = data_uri_parts + extension = content_type.split("/")[-1].split(";")[0] + target = os.path.join(images_dir, f"{int(time.time())}_{str(uuid.uuid4())}.{extension}") + with open(target, "wb") as f: + f.write(base64.b64decode(base64_data)) + return f"/images/{os.path.basename(target)}" + else: + return None + else: + # Обробка звичайної URL-адреси + async with session.get(image) as response: + target = os.path.join(images_dir, f"{int(time.time())}_{str(uuid.uuid4())}") + 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]) images = asyncio.run(copy_images(chunk.get_list(), chunk.options.get("cookies"))) yield self._format_json("content", str(ImageResponse(images, chunk.alt))) elif not isinstance(chunk, FinishReason): @@ -245,4 +260,4 @@ def get_error_message(exception: Exception) -> str: provider = get_last_provider() if provider is None: return message - return f"{provider.__name__}: {message}"
\ No newline at end of file + return f"{provider.__name__}: {message}" |