diff options
author | kqlio67 <kqlio67@users.noreply.github.com> | 2024-10-25 19:05:37 +0200 |
---|---|---|
committer | kqlio67 <kqlio67@users.noreply.github.com> | 2024-10-25 19:05:37 +0200 |
commit | 7ecc5962e413ca5cb4c13a99a07f3e14bcf73b15 (patch) | |
tree | ab447a1064a981375966f6a72f70cac1f84cab9b /g4f/gui | |
parent | feat(g4f/gui/server/api.py): enhance image handling and directory management (diff) | |
download | gpt4free-7ecc5962e413ca5cb4c13a99a07f3e14bcf73b15.tar gpt4free-7ecc5962e413ca5cb4c13a99a07f3e14bcf73b15.tar.gz gpt4free-7ecc5962e413ca5cb4c13a99a07f3e14bcf73b15.tar.bz2 gpt4free-7ecc5962e413ca5cb4c13a99a07f3e14bcf73b15.tar.lz gpt4free-7ecc5962e413ca5cb4c13a99a07f3e14bcf73b15.tar.xz gpt4free-7ecc5962e413ca5cb4c13a99a07f3e14bcf73b15.tar.zst gpt4free-7ecc5962e413ca5cb4c13a99a07f3e14bcf73b15.zip |
Diffstat (limited to 'g4f/gui')
-rw-r--r-- | g4f/gui/server/backend.py | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/g4f/gui/server/backend.py b/g4f/gui/server/backend.py index dc1b1080..e24d4da2 100644 --- a/g4f/gui/server/backend.py +++ b/g4f/gui/server/backend.py @@ -1,5 +1,6 @@ import json -from flask import request, Flask +import os +from flask import request, Flask, jsonify, send_from_directory from g4f.image import is_allowed_extension, to_image from .api import Api @@ -54,6 +55,10 @@ class Backend_Api(Api): '/images/<path:name>': { 'function': self.serve_images, 'methods': ['GET'] + }, + '/images': { + 'function': self.get_images, + 'methods': ['GET'] } } @@ -110,4 +115,19 @@ class Backend_Api(Api): Returns: str: A JSON formatted string. """ - return json.dumps(super()._format_json(response_type, content)) + "\n"
\ No newline at end of file + return json.dumps(super()._format_json(response_type, content)) + "\n" + + @staticmethod + def get_images(): + images_dir = "./generated_images" + try: + images = [f for f in os.listdir(images_dir) if os.path.isfile(os.path.join(images_dir, f))] + images = [f"/images/{image}" for image in images if image.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.webp'))] + return jsonify(images) + except Exception as e: + return str(e), 500 + + @staticmethod + def serve_images(name): + images_dir = "./generated_images" + return send_from_directory(os.path.abspath(images_dir), name) |