返回提交历史
Modified
g4f/gui/server/backend_api.py
+31
-1
Modified
g4f/image/__init__.py
+1
-1
XFEstudio/gpt4free
Add thumbnail support
aea736d4
代码差异
2 个文件
+32
-2
@@ -8,6 +8,7 @@ import asyncio
8
8
import shutil
9
9
import random
10
10
import datetime
11
from urllib.parse import quote_plus
11
12
from flask import Flask, Response, redirect, request, jsonify, send_from_directory
12
13
from werkzeug.exceptions import NotFound
13
14
from typing import Generator
@@ -15,6 +16,11 @@ from pathlib import Path
15
16
from urllib.parse import quote_plus
16
17
from hashlib import sha256
17
18
19
try:
20
from PIL import Image
21
has_pillow = True
22
except ImportError:
23
has_pillow = False
18
24
try:
19
25
from ...integration.markitdown import MarkItDown, StreamInfo
20
26
has_markitdown = True
@@ -28,7 +34,7 @@ from ...client.helper import filter_markdown
28
34
from ...tools.files import supports_filename, get_streaming, get_bucket_dir, get_tempfile
29
35
from ...tools.run_tools import iter_run_tools
30
36
from ...errors import ProviderNotFoundError
31
from ...image import is_allowed_extension, MEDIA_TYPE_MAP
37
from ...image import is_allowed_extension, process_image, MEDIA_TYPE_MAP
32
38
from ...cookies import get_cookies_dir
33
39
from ...image.copy_images import secure_filename, get_source_url, get_media_dir, copy_media
34
40
from ... import ChatCompletion
@@ -208,6 +214,10 @@ class Backend_Api(Api):
208
214
'/media/<path:name>': {
209
215
'function': self.serve_images,
210
216
'methods': ['GET']
217
},
218
'/thumbnail/<path:name>': {
219
'function': self.serve_images,
220
'methods': ['GET']
211
221
}
212
222
}
213
223
@@ -368,6 +378,15 @@ class Backend_Api(Api):
368
378
media.append({"name": filename, "text": result})
369
379
else:
370
380
media.append({"name": filename})
381
if has_pillow:
382
try:
383
image = Image.open(copyfile)
384
thumbnail_dir = os.path.join(bucket_dir, "thumbnail")
385
os.makedirs(thumbnail_dir, exist_ok=True)
386
image = process_image(image)
387
image.save(os.path.join(thumbnail_dir, filename))
388
except Exception as e:
389
logger.exception(e)
371
390
elif is_supported:
372
391
newfile = os.path.join(bucket_dir, filename)
373
392
filenames.append(filename)
@@ -395,6 +414,17 @@ class Backend_Api(Api):
395
414
return redirect(source_url)
396
415
raise
397
416
417
@app.route('/files/<bucket_id>/thumbnail/<filename>', methods=['GET'])
418
def get_media(bucket_id, filename, dirname: str = None):
419
media_dir = get_bucket_dir(dirname, bucket_id, "thumbnail")
420
try:
421
return send_from_directory(os.path.abspath(media_dir), filename)
422
except NotFound:
423
original = f'/files/{quote_plus(bucket_id)}/media/{quote_plus(filename)}'
424
if request.query_string:
425
original += f"?{request.query_string.decode()}"
426
return redirect(original)
427
398
428
self.match_files = {}
399
429
400
430
@app.route('/search/<search>', methods=['GET'])
@@ -214,7 +214,7 @@ def get_orientation(image: Image) -> int:
214
214
if orientation is not None:
215
215
return orientation
216
216
217
def process_image(image: Image, new_width: int, new_height: int) -> Image:
217
def process_image(image: Image, new_width: int = 1000, new_height: int = 1000) -> Image:
218
218
"""
219
219
Processes the given image by adjusting its orientation and resizing it.
220
220