XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

feat(image-processing): add support for data URI image handlin

7f05d158
kqlio67 <kqlio67@users.noreply.github.com>
提交于

代码差异

1 个文件 +28 -13
Modified g4f/gui/server/api.py +28 -13
@@ -6,6 +6,7 @@ import os.path
6 6 import uuid
7 7 import asyncio
8 8 import time
9 import base64
9 10 from aiohttp import ClientSession
10 11 from typing import Iterator, Optional
11 12 from flask import send_from_directory
@@ -195,18 +196,32 @@ class Api():
195 196 cookies=cookies
196 197 ) as session:
197 198 async def copy_image(image):
198 async with session.get(image) as response:
199 target = os.path.join(images_dir, f"{int(time.time())}_{str(uuid.uuid4())}")
200 with open(target, "wb") as f:
201 async for chunk in response.content.iter_any():
202 f.write(chunk)
203 with open(target, "rb") as f:
204 extension = is_accepted_format(f.read(12)).split("/")[-1]
205 extension = "jpg" if extension == "jpeg" else extension
206 new_target = f"{target}.{extension}"
207 os.rename(target, new_target)
208 return f"/images/{os.path.basename(new_target)}"
209 return await asyncio.gather(*[copy_image(image) for image in images])
199 if image.startswith("data:"):
200 # Обробка URL-адреси даних
201 data_uri_parts = image.split(",")
202 if len(data_uri_parts) == 2:
203 content_type, base64_data = data_uri_parts
204 extension = content_type.split("/")[-1].split(";")[0]
205 target = os.path.join(images_dir, f"{int(time.time())}_{str(uuid.uuid4())}.{extension}")
206 with open(target, "wb") as f:
207 f.write(base64.b64decode(base64_data))
208 return f"/images/{os.path.basename(target)}"
209 else:
210 return None
211 else:
212 # Обробка звичайної URL-адреси
213 async with session.get(image) as response:
214 target = os.path.join(images_dir, f"{int(time.time())}_{str(uuid.uuid4())}")
215 with open(target, "wb") as f:
216 async for chunk in response.content.iter_any():
217 f.write(chunk)
218 with open(target, "rb") as f:
219 extension = is_accepted_format(f.read(12)).split("/")[-1]
220 extension = "jpg" if extension == "jpeg" else extension
221 new_target = f"{target}.{extension}"
222 os.rename(target, new_target)
223 return f"/images/{os.path.basename(new_target)}"
224 return await asyncio.gather(*[copy_image(image) for image in images])
210 225 images = asyncio.run(copy_images(chunk.get_list(), chunk.options.get("cookies")))
211 226 yield self._format_json("content", str(ImageResponse(images, chunk.alt)))
212 227 elif not isinstance(chunk, FinishReason):
@@ -245,4 +260,4 @@ def get_error_message(exception: Exception) -> str:
245 260 provider = get_last_provider()
246 261 if provider is None:
247 262 return message
248 return f"{provider.__name__}: {message}"
263 return f"{provider.__name__}: {message}"