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

XFEstudio/gpt4free

feat(g4f/gui/server/backend.py): add route to list generated images

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

代码差异

1 个文件 +22 -2
Modified g4f/gui/server/backend.py +22 -2
@@ -1,5 +1,6 @@
1 1 import json
2 from flask import request, Flask
2 import os
3 from flask import request, Flask, jsonify, send_from_directory
3 4 from g4f.image import is_allowed_extension, to_image
4 5 from .api import Api
5 6
@@ -54,6 +55,10 @@ class Backend_Api(Api):
54 55 '/images/<path:name>': {
55 56 'function': self.serve_images,
56 57 'methods': ['GET']
58 },
59 '/images': {
60 'function': self.get_images,
61 'methods': ['GET']
57 62 }
58 63 }
59 64
@@ -110,4 +115,19 @@ class Backend_Api(Api):
110 115 Returns:
111 116 str: A JSON formatted string.
112 117 """
113 return json.dumps(super()._format_json(response_type, content)) + "\n"
118 return json.dumps(super()._format_json(response_type, content)) + "\n"
119
120 @staticmethod
121 def get_images():
122 images_dir = "./generated_images"
123 try:
124 images = [f for f in os.listdir(images_dir) if os.path.isfile(os.path.join(images_dir, f))]
125 images = [f"/images/{image}" for image in images if image.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.webp'))]
126 return jsonify(images)
127 except Exception as e:
128 return str(e), 500
129
130 @staticmethod
131 def serve_images(name):
132 images_dir = "./generated_images"
133 return send_from_directory(os.path.abspath(images_dir), name)