Spaces:
Sleeping
Sleeping
Upload patch_image_gen.py
Browse files- patch_image_gen.py +93 -72
patch_image_gen.py
CHANGED
|
@@ -10,31 +10,31 @@ the chat endpoint:
|
|
| 10 |
endpoint instead."
|
| 11 |
|
| 12 |
The agent loop only ever calls /chat/completions, so selecting this model in the
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
ENDPOINT SHAPE (verified against GET https://openrouter.ai/api/v1/images/models)
|
| 17 |
-------------------------------------------------------------------------------
|
| 18 |
-
POST https://openrouter.ai/api/v1/images
|
| 19 |
Headers: Authorization: Bearer $OPENROUTER_API_KEY
|
| 20 |
HTTP-Referer / X-Title optional but recommended
|
| 21 |
Body: {"model": ..., "prompt": ..., "n": 1, "output_format": "png"}
|
| 22 |
Resp: {"data": [{"b64_json": "<base64>", "media_type": "image/png"}],
|
| 23 |
"usage": {"cost": ...}}
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
(png/jpeg/webp). There is NO aspect_ratio / size / resolution parameter
|
| 29 |
-
sending one makes OpenRouter answer 400.
|
| 30 |
-
|
| 31 |
|
| 32 |
-
LiteLLM cannot
|
| 33 |
-
|
| 34 |
|
| 35 |
-
Runs LAST among the backend patches (after patch_none_guard.py)
|
| 36 |
-
appends to backend/routes/agent.py, which no other backend patch rewrites
|
| 37 |
-
|
| 38 |
"""
|
| 39 |
|
| 40 |
import ast
|
|
@@ -48,14 +48,19 @@ if "/image/generate" in c:
|
|
| 48 |
print("OK: image route already present (idempotent)")
|
| 49 |
raise SystemExit(0)
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
# --- imports -------------------------------------------------------------
|
| 52 |
-
# `json`
|
|
|
|
|
|
|
| 53 |
if "\nimport httpx" not in c:
|
| 54 |
imp_anchor = "import asyncio\nimport json\nimport logging\n"
|
| 55 |
if imp_anchor not in c:
|
| 56 |
-
|
| 57 |
-
"FAIL: could not find import anchor (asyncio/json/logging) in agent.py"
|
| 58 |
-
)
|
| 59 |
c = c.replace(
|
| 60 |
imp_anchor,
|
| 61 |
"import asyncio\nimport json\nimport logging\nimport os\n\nimport httpx\n",
|
|
@@ -63,41 +68,56 @@ if "\nimport httpx" not in c:
|
|
| 63 |
)
|
| 64 |
print("OK: added os + httpx imports")
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
# --- route ---------------------------------------------------------------
|
| 67 |
-
# Appended as
|
| 68 |
-
#
|
|
|
|
|
|
|
| 69 |
ROUTE = '''
|
| 70 |
|
| 71 |
# === PATCH: image generation ================================================
|
| 72 |
-
# OpenRouter serves image models on POST /api/v1/images (NOT
|
| 73 |
-
# /images/generations, and never /chat/completions). See
|
| 74 |
-
# patch_frontend_image_gen.py for the matching frontend wiring.
|
| 75 |
_IMAGE_ROUTER = APIRouter(prefix="/image", tags=["image"])
|
| 76 |
|
| 77 |
_IMAGE_OUTPUT_FORMATS = {"png", "jpeg", "webp"}
|
|
|
|
| 78 |
|
| 79 |
|
| 80 |
class ImageGenerateRequest(BaseModel):
|
| 81 |
prompt: str
|
| 82 |
-
model: str =
|
| 83 |
output_format: str = "png"
|
| 84 |
|
| 85 |
|
| 86 |
-
def
|
| 87 |
-
"""
|
| 88 |
-
normalized = model
|
| 89 |
for prefix in ("openai/", "huggingface/"):
|
| 90 |
if normalized.startswith(prefix):
|
| 91 |
normalized = normalized[len(prefix):]
|
| 92 |
-
|
|
|
|
| 93 |
|
| 94 |
|
| 95 |
@_IMAGE_ROUTER.post("/generate")
|
| 96 |
async def generate_image(request: ImageGenerateRequest) -> dict:
|
| 97 |
"""Generate an image via OpenRouter and return it as a data: URL.
|
| 98 |
|
| 99 |
-
OpenRouter returns base64 (b64_json), never a URL, so the
|
| 100 |
-
|
| 101 |
"""
|
| 102 |
api_key = os.environ.get("OPENROUTER_API_KEY")
|
| 103 |
if not api_key:
|
|
@@ -122,9 +142,9 @@ async def generate_image(request: ImageGenerateRequest) -> dict:
|
|
| 122 |
if not prompt:
|
| 123 |
raise HTTPException(status_code=400, detail="Prompt must not be empty.")
|
| 124 |
|
| 125 |
-
|
| 126 |
payload = {
|
| 127 |
-
"model":
|
| 128 |
"prompt": prompt,
|
| 129 |
# ming-image-0.1-design accepts n == 1 only.
|
| 130 |
"n": 1,
|
|
@@ -152,14 +172,15 @@ async def generate_image(request: ImageGenerateRequest) -> dict:
|
|
| 152 |
)
|
| 153 |
|
| 154 |
if resp.status_code >= 400:
|
| 155 |
-
body = resp.text[:500]
|
| 156 |
logger.warning(
|
| 157 |
-
"Image generation failed: status=%s body=%s",
|
|
|
|
|
|
|
| 158 |
)
|
| 159 |
-
# Surface the provider's own message; it
|
| 160 |
-
detail =
|
| 161 |
try:
|
| 162 |
-
detail = resp.json().get("error", {}).get("message"
|
| 163 |
except Exception:
|
| 164 |
pass
|
| 165 |
raise HTTPException(
|
|
@@ -181,65 +202,65 @@ async def generate_image(request: ImageGenerateRequest) -> dict:
|
|
| 181 |
status_code=502, detail="Image provider returned no image data."
|
| 182 |
)
|
| 183 |
|
| 184 |
-
|
| 185 |
-
b64 = first.get("b64_json")
|
| 186 |
if not b64:
|
| 187 |
raise HTTPException(
|
| 188 |
status_code=502,
|
| 189 |
-
detail="Image provider returned
|
| 190 |
)
|
| 191 |
|
| 192 |
-
media_type =
|
| 193 |
-
cost = (data.get("usage") or {}).get("cost")
|
| 194 |
|
| 195 |
return {
|
| 196 |
"images": [
|
| 197 |
-
{
|
| 198 |
-
"url": f"data:{media_type};base64,{b64}",
|
| 199 |
-
"media_type": media_type,
|
| 200 |
-
}
|
| 201 |
],
|
| 202 |
-
"model":
|
| 203 |
-
"cost_usd": cost,
|
| 204 |
}
|
| 205 |
|
| 206 |
|
| 207 |
-
# Mount
|
|
|
|
| 208 |
router.include_router(_IMAGE_ROUTER)
|
| 209 |
# === END PATCH: image generation ============================================
|
| 210 |
'''
|
| 211 |
|
| 212 |
-
|
| 213 |
-
if marker not in c:
|
| 214 |
c = c.rstrip("\n") + ROUTE
|
| 215 |
print("OK: appended image generation route")
|
| 216 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
try:
|
| 218 |
-
ast.parse(c)
|
| 219 |
except SyntaxError as e:
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
with open(AGENT_FILE, "w") as f:
|
| 226 |
f.write(c)
|
| 227 |
-
print("OK: agent.py written & syntax valid")
|
| 228 |
-
print("DONE: image generation route added (POST /api/image/generate)")
|
| 229 |
-
'''
|
| 230 |
|
| 231 |
-
PY
|
| 232 |
-
print("DONE: image generation route added (POST /api/image/generate)")
|
| 233 |
-
'''
|
| 234 |
-
|
| 235 |
-
# `router.include_router(...)` at import time requires `router` to exist; it
|
| 236 |
-
# does (defined above in the module). Verify the expected symbols are present.
|
| 237 |
-
if "router = APIRouter(prefix=\"/api\", tags=[\"agent\"])" not in c:
|
| 238 |
-
raise SystemExit("FAIL: could not find the module-level `router` in agent.py")
|
| 239 |
-
if "\nimport os" not in c:
|
| 240 |
-
raise SystemExit("FAIL: os import not present after patching")
|
| 241 |
-
|
| 242 |
-
with open(AGENT_FILE, "w") as f:
|
| 243 |
-
f.write(c)
|
| 244 |
print("OK: agent.py written & syntax valid")
|
|
|
|
| 245 |
print("DONE: image generation route added (POST /api/image/generate)")
|
|
|
|
| 10 |
endpoint instead."
|
| 11 |
|
| 12 |
The agent loop only ever calls /chat/completions, so selecting this model in the
|
| 13 |
+
picker produced a runtime error and nothing else. This adds a dedicated route
|
| 14 |
+
that the frontend calls instead, bypassing the chat transport entirely.
|
| 15 |
|
| 16 |
ENDPOINT SHAPE (verified against GET https://openrouter.ai/api/v1/images/models)
|
| 17 |
-------------------------------------------------------------------------------
|
| 18 |
+
POST https://openrouter.ai/api/v1/images <-- NOT /images/generations
|
| 19 |
Headers: Authorization: Bearer $OPENROUTER_API_KEY
|
| 20 |
HTTP-Referer / X-Title optional but recommended
|
| 21 |
Body: {"model": ..., "prompt": ..., "n": 1, "output_format": "png"}
|
| 22 |
Resp: {"data": [{"b64_json": "<base64>", "media_type": "image/png"}],
|
| 23 |
"usage": {"cost": ...}}
|
| 24 |
|
| 25 |
+
* data[i].b64_json is ALWAYS base64 - there is never a URL to link to, so
|
| 26 |
+
this route returns a data: URL and the frontend renders it in an <img>.
|
| 27 |
+
* For ming-image-0.1-design the catalog advertises ONLY output_format
|
| 28 |
+
(png/jpeg/webp). There is NO aspect_ratio / size / resolution parameter and
|
| 29 |
+
sending one makes OpenRouter answer 400. n is fixed at 1.
|
| 30 |
+
* supports_streaming is false for this model, so this route never streams.
|
| 31 |
|
| 32 |
+
LiteLLM cannot do this: its OpenRouter image path targets /chat/completions,
|
| 33 |
+
not /api/v1/images. The request goes out over httpx directly.
|
| 34 |
|
| 35 |
+
Runs LAST among the backend patches (after patch_none_guard.py): it only
|
| 36 |
+
appends to backend/routes/agent.py, which no other backend patch rewrites
|
| 37 |
+
afterwards. Idempotent, fail-loud, atomic (validated before the single write).
|
| 38 |
"""
|
| 39 |
|
| 40 |
import ast
|
|
|
|
| 48 |
print("OK: image route already present (idempotent)")
|
| 49 |
raise SystemExit(0)
|
| 50 |
|
| 51 |
+
|
| 52 |
+
def fail(msg: str) -> None:
|
| 53 |
+
raise SystemExit(f"FAIL: {msg}\n File left untouched.")
|
| 54 |
+
|
| 55 |
+
|
| 56 |
# --- imports -------------------------------------------------------------
|
| 57 |
+
# `json`/`logger` already exist upstream; `os` and `httpx` are new.
|
| 58 |
+
# httpx arrives via FastAPI/Starlette's dependency chain, so it needs no
|
| 59 |
+
# separate install step.
|
| 60 |
if "\nimport httpx" not in c:
|
| 61 |
imp_anchor = "import asyncio\nimport json\nimport logging\n"
|
| 62 |
if imp_anchor not in c:
|
| 63 |
+
fail("could not find import anchor (asyncio/json/logging) in agent.py")
|
|
|
|
|
|
|
| 64 |
c = c.replace(
|
| 65 |
imp_anchor,
|
| 66 |
"import asyncio\nimport json\nimport logging\nimport os\n\nimport httpx\n",
|
|
|
|
| 68 |
)
|
| 69 |
print("OK: added os + httpx imports")
|
| 70 |
|
| 71 |
+
# The request model needs pydantic.BaseModel. agent.py currently imports only
|
| 72 |
+
# ValidationError, so extend that line rather than adding a second pydantic
|
| 73 |
+
# import further down the file.
|
| 74 |
+
if "from pydantic import BaseModel" not in c:
|
| 75 |
+
pyd_anchor = "from pydantic import ValidationError\n"
|
| 76 |
+
if pyd_anchor not in c:
|
| 77 |
+
fail("could not find 'from pydantic import ValidationError' in agent.py")
|
| 78 |
+
c = c.replace(
|
| 79 |
+
pyd_anchor,
|
| 80 |
+
"from pydantic import BaseModel, ValidationError\n",
|
| 81 |
+
1,
|
| 82 |
+
)
|
| 83 |
+
print("OK: added BaseModel to pydantic imports")
|
| 84 |
+
|
| 85 |
# --- route ---------------------------------------------------------------
|
| 86 |
+
# Appended as its own router so it cannot disturb, and cannot be disturbed by,
|
| 87 |
+
# the edits other patches make higher up in this file. It mounts onto the
|
| 88 |
+
# existing module-level `router`, which already carries prefix="/api", so the
|
| 89 |
+
# final path is /api/image/generate - matching the frontend's apiFetch call.
|
| 90 |
ROUTE = '''
|
| 91 |
|
| 92 |
# === PATCH: image generation ================================================
|
|
|
|
|
|
|
|
|
|
| 93 |
_IMAGE_ROUTER = APIRouter(prefix="/image", tags=["image"])
|
| 94 |
|
| 95 |
_IMAGE_OUTPUT_FORMATS = {"png", "jpeg", "webp"}
|
| 96 |
+
_DEFAULT_IMAGE_MODEL = "openai/inclusionai/ming-image-0.1-design"
|
| 97 |
|
| 98 |
|
| 99 |
class ImageGenerateRequest(BaseModel):
|
| 100 |
prompt: str
|
| 101 |
+
model: str = _DEFAULT_IMAGE_MODEL
|
| 102 |
output_format: str = "png"
|
| 103 |
|
| 104 |
|
| 105 |
+
def _openrouter_image_model(model: str) -> str:
|
| 106 |
+
"""Strip our internal prefixes; OpenRouter ids carry neither."""
|
| 107 |
+
normalized = model.strip()
|
| 108 |
for prefix in ("openai/", "huggingface/"):
|
| 109 |
if normalized.startswith(prefix):
|
| 110 |
normalized = normalized[len(prefix):]
|
| 111 |
+
break
|
| 112 |
+
return normalized
|
| 113 |
|
| 114 |
|
| 115 |
@_IMAGE_ROUTER.post("/generate")
|
| 116 |
async def generate_image(request: ImageGenerateRequest) -> dict:
|
| 117 |
"""Generate an image via OpenRouter and return it as a data: URL.
|
| 118 |
|
| 119 |
+
OpenRouter returns base64 (b64_json), never a URL, so the bytes are inlined
|
| 120 |
+
as a data: URL for the frontend to render directly.
|
| 121 |
"""
|
| 122 |
api_key = os.environ.get("OPENROUTER_API_KEY")
|
| 123 |
if not api_key:
|
|
|
|
| 142 |
if not prompt:
|
| 143 |
raise HTTPException(status_code=400, detail="Prompt must not be empty.")
|
| 144 |
|
| 145 |
+
model = _openrouter_image_model(request.model)
|
| 146 |
payload = {
|
| 147 |
+
"model": model,
|
| 148 |
"prompt": prompt,
|
| 149 |
# ming-image-0.1-design accepts n == 1 only.
|
| 150 |
"n": 1,
|
|
|
|
| 172 |
)
|
| 173 |
|
| 174 |
if resp.status_code >= 400:
|
|
|
|
| 175 |
logger.warning(
|
| 176 |
+
"Image generation failed: status=%s body=%s",
|
| 177 |
+
resp.status_code,
|
| 178 |
+
resp.text[:500],
|
| 179 |
)
|
| 180 |
+
# Surface the provider's own message; it beats a bare 500.
|
| 181 |
+
detail = resp.text[:500]
|
| 182 |
try:
|
| 183 |
+
detail = resp.json().get("error", {}).get("message") or detail
|
| 184 |
except Exception:
|
| 185 |
pass
|
| 186 |
raise HTTPException(
|
|
|
|
| 202 |
status_code=502, detail="Image provider returned no image data."
|
| 203 |
)
|
| 204 |
|
| 205 |
+
b64 = entries[0].get("b64_json")
|
|
|
|
| 206 |
if not b64:
|
| 207 |
raise HTTPException(
|
| 208 |
status_code=502,
|
| 209 |
+
detail="Image provider returned no b64_json payload.",
|
| 210 |
)
|
| 211 |
|
| 212 |
+
media_type = entries[0].get("media_type") or f"image/{fmt}"
|
|
|
|
| 213 |
|
| 214 |
return {
|
| 215 |
"images": [
|
| 216 |
+
{"url": f"data:{media_type};base64,{b64}", "media_type": media_type}
|
|
|
|
|
|
|
|
|
|
| 217 |
],
|
| 218 |
+
"model": model,
|
| 219 |
+
"cost_usd": (data.get("usage") or {}).get("cost"),
|
| 220 |
}
|
| 221 |
|
| 222 |
|
| 223 |
+
# Mount under the same /api prefix as the rest of the app:
|
| 224 |
+
# _IMAGE_ROUTER prefix "/image" + router prefix "/api" -> /api/image/generate
|
| 225 |
router.include_router(_IMAGE_ROUTER)
|
| 226 |
# === END PATCH: image generation ============================================
|
| 227 |
'''
|
| 228 |
|
| 229 |
+
if '_IMAGE_ROUTER = APIRouter(prefix="/image"' not in c:
|
|
|
|
| 230 |
c = c.rstrip("\n") + ROUTE
|
| 231 |
print("OK: appended image generation route")
|
| 232 |
|
| 233 |
+
# --- pre-write sanity checks --------------------------------------------
|
| 234 |
+
if 'router = APIRouter(prefix="/api", tags=["agent"])' not in c:
|
| 235 |
+
fail("module-level `router` not found; the appended router cannot mount")
|
| 236 |
+
|
| 237 |
try:
|
| 238 |
+
tree = ast.parse(c)
|
| 239 |
except SyntaxError as e:
|
| 240 |
+
fail(f"patched agent.py has a syntax error at line {e.lineno}: {e.msg}")
|
| 241 |
+
|
| 242 |
+
# Every name the appended block relies on must resolve in the patched module.
|
| 243 |
+
defined = set()
|
| 244 |
+
for node in ast.walk(tree):
|
| 245 |
+
if isinstance(node, (ast.Import, ast.ImportFrom)):
|
| 246 |
+
for a in node.names:
|
| 247 |
+
defined.add((a.asname or a.name).split(".")[0])
|
| 248 |
+
elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)):
|
| 249 |
+
defined.add(node.name)
|
| 250 |
+
elif isinstance(node, ast.Name) and isinstance(node.ctx, ast.Store):
|
| 251 |
+
defined.add(node.id)
|
| 252 |
+
|
| 253 |
+
required = {
|
| 254 |
+
"APIRouter", "BaseModel", "HTTPException", "httpx", "os", "logger",
|
| 255 |
+
"_IMAGE_ROUTER", "router",
|
| 256 |
+
}
|
| 257 |
+
missing = sorted(required - defined)
|
| 258 |
+
if missing:
|
| 259 |
+
fail(f"appended code references names not present in agent.py: {missing}")
|
| 260 |
|
| 261 |
with open(AGENT_FILE, "w") as f:
|
| 262 |
f.write(c)
|
|
|
|
|
|
|
|
|
|
| 263 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
print("OK: agent.py written & syntax valid")
|
| 265 |
+
print(f"OK: import graph resolves ({len(required)} required names present)")
|
| 266 |
print("DONE: image generation route added (POST /api/image/generate)")
|