refactor: improve media rendering and response formatting with precise changes

- Modified g4f/providers/response.py to ensure format_images_markdown returns the result directly without additional flags in the 'format_images_markdown' function.
- Updated g4f/gui/server/api.py to add 'tempfiles' parameter with default empty list to '_create_response_stream' method.
- Changed or added code in API response handling to iterate over 'tempfiles' and attempt to remove each file after response completion, with exception handling (try-except block with logger.exception).
- Adjusted g4f/Tools/files.py to fix tempfile creation: corrected the 'suffix' parameter in 'get_tempfile' to use 'suffix' directly instead of splitting.
- In g4f/tools/media.py, changed 'render_part' function to handle 'text' key properly, checking 'part.get("text")' and returning a dictionary with 'type': 'text' and 'text': value, if present.
This commit is contained in:
hlohaus
2025-05-19 08:15:21 +02:00
parent ff0618e25f
commit c0d31c2abb
15 changed files with 516 additions and 61 deletions

View File

@@ -8,8 +8,7 @@ import asyncio
import shutil
import random
import datetime
import tempfile
from flask import Flask, Response, redirect, request, jsonify, render_template, send_from_directory
from flask import Flask, Response, redirect, request, jsonify, send_from_directory
from werkzeug.exceptions import NotFound
from typing import Generator
from pathlib import Path
@@ -17,19 +16,20 @@ from urllib.parse import quote_plus
from hashlib import sha256
try:
from markitdown import MarkItDown
from ...integration.markitdown import MarkItDown, StreamInfo
has_markitdown = True
except ImportError:
except ImportError as e:
print(e)
has_markitdown = False
from ...client.service import convert_to_provider
from ...providers.asyncio import to_sync_generator
from ...providers.response import FinishReason
from ...client.helper import filter_markdown
from ...tools.files import supports_filename, get_streaming, get_bucket_dir, get_buckets
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
from ...image import is_allowed_extension, MEDIA_TYPE_MAP
from ...cookies import get_cookies_dir
from ...image.copy_images import secure_filename, get_source_url, get_media_dir
from ... import ChatCompletion
@@ -79,9 +79,7 @@ class Backend_Api(Api):
@app.route('/backend-api/v2/providers', methods=['GET'])
def jsonify_providers(**kwargs):
response = self.get_providers(**kwargs)
if isinstance(response, list):
return jsonify(response)
return response
return jsonify(response)
def get_demo_models():
return [{
@@ -91,7 +89,7 @@ class Backend_Api(Api):
"audio": isinstance(model, models.AudioModel),
"video": isinstance(model, models.VideoModel),
"providers": [
getattr(provider, "parent", provider.__name__)
provider.get_parent()
for provider in providers
],
"demo": True
@@ -109,13 +107,14 @@ class Backend_Api(Api):
json_data = json.loads(request.form['json'])
else:
json_data = request.json
tempfiles = []
if "files" in request.files:
media = []
for file in request.files.getlist('files'):
if file.filename != '' and is_allowed_extension(file.filename):
newfile = tempfile.TemporaryFile()
shutil.copyfileobj(file.stream, newfile)
media.append((newfile, file.filename))
newfile = get_tempfile(file)
tempfiles.append(newfile)
media.append((Path(newfile), file.filename))
json_data['media'] = media
if app.demo and not json_data.get("provider"):
@@ -130,6 +129,7 @@ class Backend_Api(Api):
kwargs,
json_data.get("provider"),
json_data.get("download_media", True),
tempfiles
),
mimetype='text/event-stream'
)
@@ -306,41 +306,46 @@ class Backend_Api(Api):
filenames = []
media = []
for file in request.files.getlist('files'):
# Copy the file to a temporary location
filename = secure_filename(file.filename)
copyfile = tempfile.NamedTemporaryFile(suffix=filename, delete=False)
shutil.copyfileobj(file.stream, copyfile)
copyfile.close()
file.stream.close()
mimetype = file.mimetype.split(";")[0]
if (not filename or filename == "blob") and mimetype in MEDIA_TYPE_MAP:
filename = f"file.{MEDIA_TYPE_MAP[mimetype]}"
suffix = os.path.splitext(filename)[1].lower()
copyfile = get_tempfile(file, suffix)
result = None
if has_markitdown:
try:
language = request.headers.get("x-recognition-language")
md = MarkItDown()
result = md.convert(copyfile.name).text_content
with open(os.path.join(bucket_dir, f"{filename}.md"), 'w') as f:
f.write(f"{result}\n")
filenames.append(f"{filename}.md")
result = md.convert(copyfile, stream_info=StreamInfo(
extension=suffix,
mimetype=file.mimetype,
), language=language).text_content
except Exception as e:
logger.exception(e)
if not result:
if is_allowed_extension(filename):
os.makedirs(media_dir, exist_ok=True)
newfile = os.path.join(media_dir, filename)
media.append(filename)
elif supports_filename(filename):
newfile = os.path.join(bucket_dir, filename)
filenames.append(filename)
else:
os.remove(copyfile.name)
continue
try:
os.rename(copyfile.name, newfile)
except OSError:
shutil.copyfile(copyfile.name, newfile)
os.remove(copyfile.name)
is_media = is_allowed_extension(filename)
is_supported = supports_filename(filename)
if not is_media and not is_supported:
os.remove(copyfile)
continue
if not is_media and result:
with open(os.path.join(bucket_dir, f"{filename}.md"), 'w') as f:
f.write(f"{result}\n")
filenames.append(f"{filename}.md")
if is_media:
os.makedirs(media_dir, exist_ok=True)
newfile = os.path.join(media_dir, filename)
media.append({"name": filename, "text": result})
elif not result and supports_filename(filename):
newfile = os.path.join(bucket_dir, filename)
filenames.append(filename)
try:
os.rename(copyfile, newfile)
except OSError:
shutil.copyfile(copyfile, newfile)
os.remove(copyfile)
with open(os.path.join(bucket_dir, "files.txt"), 'w') as f:
[f.write(f"{filename}\n") for filename in filenames]
f.write("".join([f"{filename}\n" for filename in filenames]))
return {"bucket_id": bucket_id, "files": filenames, "media": media}
@app.route('/files/<bucket_id>/media/<filename>', methods=['GET'])