NexraFluxPro.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from __future__ import annotations
  2. from aiohttp import ClientSession
  3. import json
  4. from ...typing import AsyncResult, Messages
  5. from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin
  6. from ...image import ImageResponse
  7. class NexraFluxPro(AsyncGeneratorProvider, ProviderModelMixin):
  8. label = "Nexra Flux PRO"
  9. url = "https://nexra.aryahcr.cc/documentation/flux-pro/en"
  10. api_endpoint = "https://nexra.aryahcr.cc/api/image/complements"
  11. working = True
  12. default_model = 'flux'
  13. models = [default_model]
  14. model_aliases = {
  15. "flux-pro": "flux",
  16. }
  17. @classmethod
  18. def get_model(cls, model: str) -> str:
  19. if model in cls.models:
  20. return model
  21. elif model in cls.model_aliases:
  22. return cls.model_aliases[model]
  23. else:
  24. return cls.default_model
  25. @classmethod
  26. async def create_async_generator(
  27. cls,
  28. model: str,
  29. messages: Messages,
  30. proxy: str = None,
  31. response: str = "url", # base64 or url
  32. **kwargs
  33. ) -> AsyncResult:
  34. # Retrieve the correct model to use
  35. model = cls.get_model(model)
  36. # Format the prompt from the messages
  37. prompt = messages[0]['content']
  38. headers = {
  39. "Content-Type": "application/json"
  40. }
  41. payload = {
  42. "prompt": prompt,
  43. "model": model,
  44. "response": response
  45. }
  46. async with ClientSession(headers=headers) as session:
  47. async with session.post(cls.api_endpoint, json=payload, proxy=proxy) as response:
  48. response.raise_for_status()
  49. text_data = await response.text()
  50. try:
  51. # Parse the JSON response
  52. json_start = text_data.find('{')
  53. json_data = text_data[json_start:]
  54. data = json.loads(json_data)
  55. # Check if the response contains images
  56. if 'images' in data and len(data['images']) > 0:
  57. image_url = data['images'][0]
  58. yield ImageResponse(image_url, prompt)
  59. else:
  60. yield ImageResponse("No images found in the response.", prompt)
  61. except json.JSONDecodeError:
  62. yield ImageResponse("Failed to parse JSON. Response might not be in JSON format.", prompt)