Create providers/prexzy_image.py
Browse files- providers/prexzy_image.py +63 -0
providers/prexzy_image.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app/providers/prexzy_image.py
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
import asyncio, logging
|
| 4 |
+
from typing import Any
|
| 5 |
+
import httpx
|
| 6 |
+
from app.core.config import settings
|
| 7 |
+
|
| 8 |
+
LOG = logging.getLogger("genisi.prexzy")
|
| 9 |
+
|
| 10 |
+
class PrexzyImageProvider:
|
| 11 |
+
"""توليد وتعديل الصور عبر prexzyapis.com (بدون مفتاح)."""
|
| 12 |
+
|
| 13 |
+
def __init__(self, base: str | None = None, timeout: int | None = None):
|
| 14 |
+
self.base = (base or settings.prexzy_base).rstrip("/")
|
| 15 |
+
self.timeout = timeout or settings.prexzy_timeout
|
| 16 |
+
|
| 17 |
+
async def generate(self, prompt: str, model_id: str = "DALL-E") -> dict[str, Any]:
|
| 18 |
+
"""توليد صورة. يعيد {'image_url': ..., 'raw': ...}."""
|
| 19 |
+
url = f"{self.base}/ai/aiappgen"
|
| 20 |
+
params = {"prompt": prompt, "modelId": model_id}
|
| 21 |
+
async with httpx.AsyncClient(timeout=self.timeout) as client:
|
| 22 |
+
r = await client.get(url, params=params)
|
| 23 |
+
r.raise_for_status()
|
| 24 |
+
data = r.json()
|
| 25 |
+
image_url = self._extract_image_url(data)
|
| 26 |
+
if not image_url:
|
| 27 |
+
raise RuntimeError(f"Prexzy generation returned no image_url: {data}")
|
| 28 |
+
LOG.info("prexzy_image_ok model=%s url=%s", model_id, image_url[:80])
|
| 29 |
+
return {"image_url": image_url, "raw": data, "engine": f"prexzy/{model_id}"}
|
| 30 |
+
|
| 31 |
+
async def edit(self, prompt: str, image_url: str) -> dict[str, Any]:
|
| 32 |
+
"""تعديل صورة موجودة. يعيد {'image_url': ...}."""
|
| 33 |
+
url = f"{self.base}/ai/imageedit"
|
| 34 |
+
params = {"prompt": prompt, "image": image_url}
|
| 35 |
+
async with httpx.AsyncClient(timeout=self.timeout) as client:
|
| 36 |
+
r = await client.get(url, params=params)
|
| 37 |
+
r.raise_for_status()
|
| 38 |
+
data = r.json()
|
| 39 |
+
image_url_out = self._extract_image_url(data)
|
| 40 |
+
if not image_url_out:
|
| 41 |
+
raise RuntimeError(f"Prexzy edit returned no image_url: {data}")
|
| 42 |
+
LOG.info("prexzy_edit_ok url=%s", image_url_out[:80])
|
| 43 |
+
return {"image_url": image_url_out, "raw": data, "engine": "prexzy/imageedit"}
|
| 44 |
+
|
| 45 |
+
@staticmethod
|
| 46 |
+
def _extract_image_url(data: Any) -> str | None:
|
| 47 |
+
if not isinstance(data, dict):
|
| 48 |
+
return None
|
| 49 |
+
# حسب الاستجابة الموثقة: image_url (أحيانًا داخل images[])
|
| 50 |
+
for key in ("image_url", "url", "output"):
|
| 51 |
+
v = data.get(key)
|
| 52 |
+
if isinstance(v, str) and v.startswith("http"):
|
| 53 |
+
return v
|
| 54 |
+
images = data.get("images") or data.get("data")
|
| 55 |
+
if isinstance(images, list) and images:
|
| 56 |
+
first = images[0]
|
| 57 |
+
if isinstance(first, str):
|
| 58 |
+
return first
|
| 59 |
+
if isinstance(first, dict):
|
| 60 |
+
for k in ("url", "image_url"):
|
| 61 |
+
if isinstance(first.get(k), str):
|
| 62 |
+
return first[k]
|
| 63 |
+
return None
|