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

XFEstudio/gpt4free

Add support thumbnail for generated images

70a0c9ae
hlohaus <983577+hlohaus@users.noreply.github.com>
提交于

代码差异

3 个文件 +30 -3
Modified g4f/api/__init__.py +27 -2
@@ -35,6 +35,11 @@ try:
35 35 has_a2wsgi = True
36 36 except ImportError:
37 37 has_a2wsgi = False
38 try:
39 from PIL import Image
40 has_pillow = True
41 except ImportError:
42 has_pillow = False
38 43 from types import SimpleNamespace
39 44 from typing import Union, Optional, List
40 45
@@ -49,7 +54,7 @@ import g4f.debug
49 54 from g4f.client import AsyncClient, ChatCompletion, ImagesResponse, ClientResponse
50 55 from g4f.providers.response import BaseConversation, JsonConversation
51 56 from g4f.client.helper import filter_none
52 from g4f.image import is_data_an_media, EXTENSIONS_MAP
57 from g4f.image import is_data_an_media, process_image, EXTENSIONS_MAP
53 58 from g4f.image.copy_images import get_media_dir, copy_media, get_source_url
54 59 from g4f.errors import ProviderNotFoundError, ModelNotFoundError, MissingAuthError, NoValidHarFileError, MissingRequirementsError
55 60 from g4f.cookies import read_cookie_files, get_cookies_dir
@@ -670,12 +675,25 @@ class Api:
670 675 HTTP_200_OK: {"content": {"image/*": {}, "audio/*": {}}, "video/*": {}},
671 676 HTTP_404_NOT_FOUND: {}
672 677 })
673 async def get_media(filename, request: Request):
678 async def get_media(filename, request: Request, thumbnail: bool = False):
674 679 target = os.path.join(get_media_dir(), os.path.basename(filename))
675 680 if not os.path.isfile(target):
676 681 other_name = os.path.join(get_media_dir(), os.path.basename(quote_plus(filename)))
677 682 if os.path.isfile(other_name):
678 683 target = other_name
684 if thumbnail and has_pillow:
685 thumbnail_dir = os.path.join(get_media_dir(), "thumbnails")
686 thumbnail = os.path.join(thumbnail_dir, filename)
687 try:
688 if not os.path.isfile(thumbnail):
689 image = Image.open(target)
690 os.makedirs(thumbnail_dir, exist_ok=True)
691 image = process_image(image)
692 image.save(os.path.join(thumbnail_dir, filename))
693 except Exception as e:
694 logger.exception(e)
695 if os.path.isfile(thumbnail):
696 target = thumbnail
679 697 ext = os.path.splitext(filename)[1][1:]
680 698 mime_type = EXTENSIONS_MAP.get(ext)
681 699 stat_result = SimpleNamespace()
@@ -735,6 +753,13 @@ class Api:
735 753 yield chunk
736 754 return StreamingResponse(stream(), headers=headers)
737 755
756 @self.app.get("/thumbnail/{filename}", responses={
757 HTTP_200_OK: {"content": {"image/*": {}, "audio/*": {}}, "video/*": {}},
758 HTTP_404_NOT_FOUND: {}
759 })
760 async def get_media_thumbnail(filename: str, request: Request):
761 return await get_media(filename, request, True)
762
738 763 def format_exception(e: Union[Exception, str], config: Union[ChatCompletionsConfig, ImageGenerationConfig] = None, image: bool = False) -> str:
739 764 last_provider = {} if not image else g4f.get_last_provider(True)
740 765 provider = (AppConfig.media_provider if image else AppConfig.provider)
Modified g4f/image/__init__.py +1 -1
@@ -216,7 +216,7 @@ def get_orientation(image: Image) -> int:
216 216 if orientation is not None:
217 217 return orientation
218 218
219 def process_image(image: Image, new_width: int = 1000, new_height: int = 1000) -> Image:
219 def process_image(image: Image, new_width: int = 800, new_height: int = 800) -> Image:
220 220 """
221 221 Processes the given image by adjusting its orientation and resizing it.
222 222
Modified g4f/providers/response.py +2 -0
@@ -84,6 +84,8 @@ def format_image(image: str, alt: str, preview: Optional[str] = None) -> str:
84 84 str: The formatted markdown string.
85 85 """
86 86 preview_url = preview.replace('{image}', image) if preview else image
87 if preview_url.startswith("/media/"):
88 preview_url = "/thumbnail" + preview_url[6:]
87 89 return f"[![{quote_title(alt)}]({quote_url(preview_url)})]({quote_url(image)})"
88 90
89 91 def format_images_markdown(images: Union[str, List[str]], alt: str,