summaryrefslogtreecommitdiffstats
path: root/g4f/gui/server/backend.py
diff options
context:
space:
mode:
authorH Lohaus <hlohaus@users.noreply.github.com>2024-11-22 01:50:48 +0100
committerGitHub <noreply@github.com>2024-11-22 01:50:48 +0100
commite8bd24a25bb8737c4f6ef8ba656e74a58e74336a (patch)
treeaa45d15f59adaf85c8f4154f44ccbc17afb0a0bd /g4f/gui/server/backend.py
parentImprove slim docker image example, clean up OpenaiChat provider (#2397) (diff)
downloadgpt4free-e8bd24a25bb8737c4f6ef8ba656e74a58e74336a.tar
gpt4free-e8bd24a25bb8737c4f6ef8ba656e74a58e74336a.tar.gz
gpt4free-e8bd24a25bb8737c4f6ef8ba656e74a58e74336a.tar.bz2
gpt4free-e8bd24a25bb8737c4f6ef8ba656e74a58e74336a.tar.lz
gpt4free-e8bd24a25bb8737c4f6ef8ba656e74a58e74336a.tar.xz
gpt4free-e8bd24a25bb8737c4f6ef8ba656e74a58e74336a.tar.zst
gpt4free-e8bd24a25bb8737c4f6ef8ba656e74a58e74336a.zip
Diffstat (limited to '')
-rw-r--r--g4f/gui/server/backend.py24
1 files changed, 15 insertions, 9 deletions
diff --git a/g4f/gui/server/backend.py b/g4f/gui/server/backend.py
index 102c5685..3dcae546 100644
--- a/g4f/gui/server/backend.py
+++ b/g4f/gui/server/backend.py
@@ -1,6 +1,8 @@
import json
import flask
import os
+import logging
+import asyncio
from flask import request, Flask
from typing import Generator
from werkzeug.utils import secure_filename
@@ -12,6 +14,8 @@ from g4f.errors import ProviderNotFoundError
from g4f.cookies import get_cookies_dir
from .api import Api
+logger = logging.getLogger(__name__)
+
def safe_iter_generator(generator: Generator) -> Generator:
start = next(generator)
def iter_generator():
@@ -127,15 +131,17 @@ class Backend_Api(Api):
return "Provider not found", 404
if not hasattr(provider_handler, "synthesize"):
return "Provider doesn't support synthesize", 500
- try:
- response_generator = provider_handler.synthesize({**request.args})
- if hasattr(response_generator, "__aiter__"):
- response_generator = to_sync_generator(response_generator)
- response = flask.Response(safe_iter_generator(response_generator), content_type="audio/mpeg")
- response.headers['Cache-Control'] = "max-age=604800"
- return response
- except Exception as e:
- return f"{e.__class__.__name__}: {e}", 500
+ response_data = provider_handler.synthesize({**request.args})
+ if asyncio.iscoroutinefunction(provider_handler.synthesize):
+ response_data = asyncio.run(response_data)
+ else:
+ if hasattr(response_data, "__aiter__"):
+ response_data = to_sync_generator(response_data)
+ response_data = safe_iter_generator(response_data)
+ content_type = getattr(provider_handler, "synthesize_content_type", "application/octet-stream")
+ response = flask.Response(response_data, content_type=content_type)
+ response.headers['Cache-Control'] = "max-age=604800"
+ return response
def get_provider_models(self, provider: str):
api_key = None if request.authorization is None else request.authorization.token