返回提交历史
Modified
g4f/gui/server/api.py
+13
-28
XFEstudio/gpt4free
Updated(g4f/gui/server/api.py)
3e8dcaf1
代码差异
1 个文件
+13
-28
@@ -6,7 +6,6 @@ import os.path
6
6
import uuid
7
7
import asyncio
8
8
import time
9
import base64
10
9
from aiohttp import ClientSession
11
10
from typing import Iterator, Optional
12
11
from flask import send_from_directory
@@ -196,32 +195,18 @@ class Api():
196
195
cookies=cookies
197
196
) as session:
198
197
async def copy_image(image):
199
if image.startswith("data:"):
200
# Processing the data 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
# Processing a regular 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])
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])
225
210
images = asyncio.run(copy_images(chunk.get_list(), chunk.options.get("cookies")))
226
211
yield self._format_json("content", str(ImageResponse(images, chunk.alt)))
227
212
elif not isinstance(chunk, FinishReason):
@@ -260,4 +245,4 @@ def get_error_message(exception: Exception) -> str:
260
245
provider = get_last_provider()
261
246
if provider is None:
262
247
return message
263
return f"{provider.__name__}: {message}"
248
return f"{provider.__name__}: {message}"