diff --git a/g4f/gui/server/backend_api.py b/g4f/gui/server/backend_api.py index c7438e66..4f13a73d 100644 --- a/g4f/gui/server/backend_api.py +++ b/g4f/gui/server/backend_api.py @@ -8,6 +8,7 @@ import asyncio import shutil import random import datetime +from urllib.parse import quote_plus from flask import Flask, Response, redirect, request, jsonify, send_from_directory from werkzeug.exceptions import NotFound from typing import Generator @@ -15,6 +16,11 @@ from pathlib import Path from urllib.parse import quote_plus from hashlib import sha256 +try: + from PIL import Image + has_pillow = True +except ImportError: + has_pillow = False try: from ...integration.markitdown import MarkItDown, StreamInfo has_markitdown = True @@ -28,7 +34,7 @@ from ...client.helper import filter_markdown from ...tools.files import supports_filename, get_streaming, get_bucket_dir, get_tempfile from ...tools.run_tools import iter_run_tools from ...errors import ProviderNotFoundError -from ...image import is_allowed_extension, MEDIA_TYPE_MAP +from ...image import is_allowed_extension, process_image, MEDIA_TYPE_MAP from ...cookies import get_cookies_dir from ...image.copy_images import secure_filename, get_source_url, get_media_dir, copy_media from ... import ChatCompletion @@ -208,6 +214,10 @@ class Backend_Api(Api): '/media/': { 'function': self.serve_images, 'methods': ['GET'] + }, + '/thumbnail/': { + 'function': self.serve_images, + 'methods': ['GET'] } } @@ -368,6 +378,15 @@ class Backend_Api(Api): media.append({"name": filename, "text": result}) else: media.append({"name": filename}) + if has_pillow: + try: + image = Image.open(copyfile) + thumbnail_dir = os.path.join(bucket_dir, "thumbnail") + os.makedirs(thumbnail_dir, exist_ok=True) + image = process_image(image) + image.save(os.path.join(thumbnail_dir, filename)) + except Exception as e: + logger.exception(e) elif is_supported: newfile = os.path.join(bucket_dir, filename) filenames.append(filename) @@ -395,6 +414,17 @@ class Backend_Api(Api): return redirect(source_url) raise + @app.route('/files//thumbnail/', methods=['GET']) + def get_media(bucket_id, filename, dirname: str = None): + media_dir = get_bucket_dir(dirname, bucket_id, "thumbnail") + try: + return send_from_directory(os.path.abspath(media_dir), filename) + except NotFound: + original = f'/files/{quote_plus(bucket_id)}/media/{quote_plus(filename)}' + if request.query_string: + original += f"?{request.query_string.decode()}" + return redirect(original) + self.match_files = {} @app.route('/search/', methods=['GET']) diff --git a/g4f/image/__init__.py b/g4f/image/__init__.py index 8a40ff52..2a18c160 100644 --- a/g4f/image/__init__.py +++ b/g4f/image/__init__.py @@ -214,7 +214,7 @@ def get_orientation(image: Image) -> int: if orientation is not None: return orientation -def process_image(image: Image, new_width: int, new_height: int) -> Image: +def process_image(image: Image, new_width: int = 1000, new_height: int = 1000) -> Image: """ Processes the given image by adjusting its orientation and resizing it.