2024-09-04 02:09:29 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-09-29 23:38:25 +03:00
|
|
|
from aiohttp import ClientSession
|
2024-10-11 09:33:30 +03:00
|
|
|
import json
|
|
|
|
|
|
|
|
from ..typing import AsyncResult, Messages
|
2024-09-04 02:09:29 +03:00
|
|
|
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
|
2024-10-11 09:33:30 +03:00
|
|
|
from ..image import ImageResponse
|
|
|
|
|
2024-09-04 02:09:29 +03:00
|
|
|
|
|
|
|
class Nexra(AsyncGeneratorProvider, ProviderModelMixin):
|
2024-10-11 09:33:30 +03:00
|
|
|
label = "Nexra Animagine XL"
|
|
|
|
url = "https://nexra.aryahcr.cc/documentation/midjourney/en"
|
|
|
|
api_endpoint = "https://nexra.aryahcr.cc/api/image/complements"
|
2024-09-04 02:09:29 +03:00
|
|
|
working = True
|
2024-09-29 23:38:25 +03:00
|
|
|
|
2024-10-11 09:33:30 +03:00
|
|
|
default_model = 'animagine-xl'
|
|
|
|
models = [default_model]
|
2024-09-29 23:38:25 +03:00
|
|
|
|
2024-09-04 02:09:29 +03:00
|
|
|
@classmethod
|
|
|
|
def get_model(cls, model: str) -> str:
|
2024-10-11 09:33:30 +03:00
|
|
|
return cls.default_model
|
2024-09-29 23:38:25 +03:00
|
|
|
|
2024-09-04 02:09:29 +03:00
|
|
|
@classmethod
|
|
|
|
async def create_async_generator(
|
|
|
|
cls,
|
|
|
|
model: str,
|
|
|
|
messages: Messages,
|
|
|
|
proxy: str = None,
|
2024-10-11 09:33:30 +03:00
|
|
|
response: str = "url", # base64 or url
|
2024-09-04 02:09:29 +03:00
|
|
|
**kwargs
|
2024-09-11 16:16:22 +03:00
|
|
|
) -> AsyncResult:
|
2024-10-11 09:33:30 +03:00
|
|
|
# Retrieve the correct model to use
|
2024-09-04 02:09:29 +03:00
|
|
|
model = cls.get_model(model)
|
2024-09-29 23:38:25 +03:00
|
|
|
|
2024-10-11 09:33:30 +03:00
|
|
|
# Format the prompt from the messages
|
|
|
|
prompt = messages[0]['content']
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
}
|
|
|
|
payload = {
|
|
|
|
"prompt": prompt,
|
|
|
|
"model": model,
|
|
|
|
"response": response
|
|
|
|
}
|
|
|
|
|
|
|
|
async with ClientSession(headers=headers) as session:
|
|
|
|
async with session.post(cls.api_endpoint, json=payload, proxy=proxy) as response:
|
|
|
|
response.raise_for_status()
|
|
|
|
text_data = await response.text()
|
2024-09-29 23:38:25 +03:00
|
|
|
|
2024-10-11 09:33:30 +03:00
|
|
|
try:
|
|
|
|
# Parse the JSON response
|
|
|
|
json_start = text_data.find('{')
|
|
|
|
json_data = text_data[json_start:]
|
|
|
|
data = json.loads(json_data)
|
|
|
|
|
|
|
|
# Check if the response contains images
|
|
|
|
if 'images' in data and len(data['images']) > 0:
|
|
|
|
image_url = data['images'][0]
|
|
|
|
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)
|