diff options
author | kqlio67 <kqlio67@users.noreply.github.com> | 2024-09-06 00:30:21 +0200 |
---|---|---|
committer | kqlio67 <kqlio67@users.noreply.github.com> | 2024-09-06 00:30:21 +0200 |
commit | 5b82cdc25ff90707a95fdf8fcc04fb0cadd6c0fe (patch) | |
tree | 6aefc43192fd71501235300281bcb514aa42d4b5 /g4f/Provider/FluxAirforce.py | |
parent | Updated g4f/Provider/AiChats.py (diff) | |
download | gpt4free-5b82cdc25ff90707a95fdf8fcc04fb0cadd6c0fe.tar gpt4free-5b82cdc25ff90707a95fdf8fcc04fb0cadd6c0fe.tar.gz gpt4free-5b82cdc25ff90707a95fdf8fcc04fb0cadd6c0fe.tar.bz2 gpt4free-5b82cdc25ff90707a95fdf8fcc04fb0cadd6c0fe.tar.lz gpt4free-5b82cdc25ff90707a95fdf8fcc04fb0cadd6c0fe.tar.xz gpt4free-5b82cdc25ff90707a95fdf8fcc04fb0cadd6c0fe.tar.zst gpt4free-5b82cdc25ff90707a95fdf8fcc04fb0cadd6c0fe.zip |
Diffstat (limited to '')
-rw-r--r-- | g4f/Provider/FluxAirforce.py | 43 |
1 files changed, 28 insertions, 15 deletions
diff --git a/g4f/Provider/FluxAirforce.py b/g4f/Provider/FluxAirforce.py index 139e6c5e..fe003a61 100644 --- a/g4f/Provider/FluxAirforce.py +++ b/g4f/Provider/FluxAirforce.py @@ -1,11 +1,12 @@ from __future__ import annotations -from aiohttp import ClientSession +from aiohttp import ClientSession, ClientResponseError from urllib.parse import urlencode +import io from ..typing import AsyncResult, Messages from .base_provider import AsyncGeneratorProvider, ProviderModelMixin -from ..image import ImageResponse +from ..image import ImageResponse, is_accepted_format class FluxAirforce(AsyncGeneratorProvider, ProviderModelMixin): url = "https://flux.api.airforce/" @@ -43,7 +44,6 @@ class FluxAirforce(AsyncGeneratorProvider, ProviderModelMixin): "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36" } - # Assume the last message in the messages list is the prompt prompt = messages[-1]['content'] if messages else "" params = { @@ -53,17 +53,30 @@ class FluxAirforce(AsyncGeneratorProvider, ProviderModelMixin): "model": model } - # Remove None values from params params = {k: v for k, v in params.items() if v is not None} - async with ClientSession(headers=headers) as session: - async with session.get(f"{cls.api_endpoint}", params=params, proxy=proxy) as response: - response.raise_for_status() - - # Get the URL of the generated image - image_url = str(response.url) - - # Create an ImageResponse object - image_response = ImageResponse(image_url, prompt) - - yield image_response + try: + async with ClientSession(headers=headers) as session: + async with session.get(f"{cls.api_endpoint}", params=params, proxy=proxy) as response: + response.raise_for_status() + + content = await response.read() + + if response.content_type.startswith('image/'): + image_url = str(response.url) + yield ImageResponse(image_url, prompt) + else: + try: + text = content.decode('utf-8', errors='ignore') + yield f"Error: {text}" + except Exception as decode_error: + yield f"Error: Unable to decode response - {str(decode_error)}" + + except ClientResponseError as e: + yield f"Error: HTTP {e.status}: {e.message}" + except Exception as e: + yield f"Unexpected error: {str(e)}" + + finally: + if not session.closed: + await session.close() |