diff options
author | kqlio67 <kqlio67@users.noreply.github.com> | 2024-10-22 14:27:01 +0200 |
---|---|---|
committer | kqlio67 <kqlio67@users.noreply.github.com> | 2024-10-22 14:27:01 +0200 |
commit | 5a79d8cbd7d99510c9f7f504e876f5197a64927b (patch) | |
tree | bea226fcabb4a11c03b7223a921bedfab20b5d8a /g4f/Provider | |
parent | Restored provider (g4f/Provider/nexra/NexraSDTurbo.py) (diff) | |
download | gpt4free-5a79d8cbd7d99510c9f7f504e876f5197a64927b.tar gpt4free-5a79d8cbd7d99510c9f7f504e876f5197a64927b.tar.gz gpt4free-5a79d8cbd7d99510c9f7f504e876f5197a64927b.tar.bz2 gpt4free-5a79d8cbd7d99510c9f7f504e876f5197a64927b.tar.lz gpt4free-5a79d8cbd7d99510c9f7f504e876f5197a64927b.tar.xz gpt4free-5a79d8cbd7d99510c9f7f504e876f5197a64927b.tar.zst gpt4free-5a79d8cbd7d99510c9f7f504e876f5197a64927b.zip |
Diffstat (limited to 'g4f/Provider')
-rw-r--r-- | g4f/Provider/nexra/NexraSDLora.py | 81 |
1 files changed, 41 insertions, 40 deletions
diff --git a/g4f/Provider/nexra/NexraSDLora.py b/g4f/Provider/nexra/NexraSDLora.py index a33afa04..a12bff1a 100644 --- a/g4f/Provider/nexra/NexraSDLora.py +++ b/g4f/Provider/nexra/NexraSDLora.py @@ -1,28 +1,26 @@ from __future__ import annotations -from aiohttp import ClientSession import json - -from ...typing import AsyncResult, Messages -from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin +import requests +from ...typing import CreateResult, Messages +from ..base_provider import ProviderModelMixin, AbstractProvider from ...image import ImageResponse - -class NexraSDLora(AsyncGeneratorProvider, ProviderModelMixin): +class NexraSDLora(AbstractProvider, ProviderModelMixin): label = "Nexra Stable Diffusion Lora" url = "https://nexra.aryahcr.cc/documentation/stable-diffusion/en" api_endpoint = "https://nexra.aryahcr.cc/api/image/complements" - working = False + working = True - default_model = 'sdxl-lora' + default_model = "sdxl-lora" models = [default_model] @classmethod def get_model(cls, model: str) -> str: return cls.default_model - + @classmethod - async def create_async_generator( + def create_completion( cls, model: str, messages: Messages, @@ -31,38 +29,41 @@ class NexraSDLora(AsyncGeneratorProvider, ProviderModelMixin): guidance: str = 0.3, # Min: 0, Max: 5 steps: str = 2, # Min: 2, Max: 10 **kwargs - ) -> AsyncResult: + ) -> CreateResult: model = cls.get_model(model) - + headers = { - "Content-Type": "application/json" + 'Content-Type': 'application/json' } - async with ClientSession(headers=headers) as session: - prompt = messages[0]['content'] - data = { - "prompt": prompt, - "model": model, - "response": response, - "data": { - "guidance": guidance, - "steps": steps - } + + data = { + "prompt": messages[-1]["content"], + "model": model, + "response": response, + "data": { + "guidance": guidance, + "steps": steps } - async with session.post(cls.api_endpoint, json=data, proxy=proxy) as response: - text_data = await response.text() - - if response.status == 200: - try: - json_start = text_data.find('{') - json_data = text_data[json_start:] - - data = json.loads(json_data) - if 'images' in data and len(data['images']) > 0: - image_url = data['images'][-1] - yield ImageResponse(image_url, prompt) - else: - yield ImageResponse("No images found in the response.", prompt) - except json.JSONDecodeError: - yield ImageResponse("Failed to parse JSON. Response might not be in JSON format.", prompt) + } + + response = requests.post(cls.api_endpoint, headers=headers, json=data) + + result = cls.process_response(response) + yield result + + @classmethod + def process_response(cls, response): + if response.status_code == 200: + try: + content = response.text.strip() + content = content.lstrip('_') + data = json.loads(content) + if data.get('status') and data.get('images'): + image_url = data['images'][0] + return ImageResponse(images=[image_url], alt="Generated Image") else: - yield ImageResponse(f"Request failed with status: {response.status}", prompt) + return "Error: No image URL found in the response" + except json.JSONDecodeError as e: + return f"Error: Unable to decode JSON response. Details: {str(e)}" + else: + return f"Error: {response.status_code}, Response: {response.text}" |