|
| 1 | +import asyncio |
| 2 | +import base64 |
| 3 | +import re |
| 4 | +import time |
| 5 | +import uuid |
| 6 | +from pathlib import Path |
| 7 | +from typing import Literal, Optional |
| 8 | + |
| 9 | +from fastapi import APIRouter, File, Form, HTTPException, Request, UploadFile |
| 10 | +from loguru import logger |
| 11 | +from pydantic import BaseModel, Field |
| 12 | + |
| 13 | +from ..schema import ImageTaskRequest |
| 14 | +from ..task_manager import TaskStatus, task_manager |
| 15 | +from .deps import get_services |
| 16 | + |
| 17 | +router = APIRouter() |
| 18 | + |
| 19 | +_SIZE_PATTERN = re.compile(r"^\s*(\d+)\s*x\s*(\d+)\s*$", re.IGNORECASE) |
| 20 | + |
| 21 | + |
| 22 | +class OpenAIImageGenerationRequest(BaseModel): |
| 23 | + prompt: str = Field(..., description="Text prompt") |
| 24 | + model: Optional[str] = Field(default=None, description="Ignored for compatibility") |
| 25 | + n: int = Field(default=1, description="Number of images, currently only supports 1") |
| 26 | + size: Optional[str] = Field(default=None, description="Image size, e.g. 1024x1024") |
| 27 | + response_format: Literal["b64_json"] = Field(default="b64_json") |
| 28 | + user: Optional[str] = Field(default=None, description="Ignored for compatibility") |
| 29 | + seed: Optional[int] = Field(default=None, description="Optional random seed") |
| 30 | + |
| 31 | + |
| 32 | +class OpenAIImageResponse(BaseModel): |
| 33 | + created: int |
| 34 | + data: list[dict[str, str]] |
| 35 | + |
| 36 | + |
| 37 | +def _write_file_sync(file_path: Path, content: bytes) -> None: |
| 38 | + with open(file_path, "wb") as buffer: |
| 39 | + buffer.write(content) |
| 40 | + |
| 41 | + |
| 42 | +def _shape_from_size(size: str) -> tuple[int, int]: |
| 43 | + match = _SIZE_PATTERN.match(size) |
| 44 | + if not match: |
| 45 | + raise ValueError("size must be in WxH format, e.g. 1024x1024") |
| 46 | + width = int(match.group(1)) |
| 47 | + height = int(match.group(2)) |
| 48 | + if width <= 0 or height <= 0: |
| 49 | + raise ValueError("size width and height must be positive") |
| 50 | + return width, height |
| 51 | + |
| 52 | + |
| 53 | +async def _wait_task_result_png(task_id: str, timeout_seconds: int, poll_interval_seconds: float) -> bytes: |
| 54 | + start_time = time.monotonic() |
| 55 | + while True: |
| 56 | + task_status = task_manager.get_task_status(task_id) |
| 57 | + if not task_status: |
| 58 | + raise HTTPException(status_code=500, detail=f"Task status not found: {task_id}") |
| 59 | + |
| 60 | + status = task_status.get("status") |
| 61 | + if status == TaskStatus.COMPLETED.value: |
| 62 | + result_png = task_manager.get_task_result_png(task_id) |
| 63 | + if result_png: |
| 64 | + return result_png |
| 65 | + raise HTTPException(status_code=500, detail=f"Task completed but no in-memory image found: {task_id}") |
| 66 | + |
| 67 | + if status == TaskStatus.FAILED.value: |
| 68 | + raise HTTPException(status_code=500, detail=task_status.get("error", "Task failed")) |
| 69 | + |
| 70 | + if status == TaskStatus.CANCELLED.value: |
| 71 | + raise HTTPException(status_code=409, detail=task_status.get("error", "Task cancelled")) |
| 72 | + |
| 73 | + if (time.monotonic() - start_time) > timeout_seconds: |
| 74 | + task_manager.cancel_task(task_id) |
| 75 | + raise HTTPException(status_code=504, detail=f"Task {task_id} timed out after {timeout_seconds} seconds") |
| 76 | + |
| 77 | + await asyncio.sleep(poll_interval_seconds) |
| 78 | + |
| 79 | + |
| 80 | +async def _watch_client_disconnect(request: Request, task_id: str, poll_interval_seconds: float = 0.2) -> bool: |
| 81 | + while True: |
| 82 | + if await request.is_disconnected(): |
| 83 | + task_manager.cancel_task(task_id) |
| 84 | + logger.info(f"Client disconnected, task {task_id} cancelled") |
| 85 | + return True |
| 86 | + await asyncio.sleep(poll_interval_seconds) |
| 87 | + |
| 88 | + |
| 89 | +async def _run_sync_image_task(request: Request, message: ImageTaskRequest) -> bytes: |
| 90 | + task_id = None |
| 91 | + timeout_seconds = 600 |
| 92 | + poll_interval_seconds = 0.5 |
| 93 | + |
| 94 | + try: |
| 95 | + message.prefer_memory_result = True |
| 96 | + task_id = task_manager.create_task(message) |
| 97 | + message.task_id = task_id |
| 98 | + |
| 99 | + wait_task = asyncio.create_task(_wait_task_result_png(task_id, timeout_seconds, poll_interval_seconds)) |
| 100 | + disconnect_task = asyncio.create_task(_watch_client_disconnect(request, task_id)) |
| 101 | + |
| 102 | + done, pending = await asyncio.wait({wait_task, disconnect_task}, return_when=asyncio.FIRST_COMPLETED) |
| 103 | + for pending_task in pending: |
| 104 | + pending_task.cancel() |
| 105 | + await asyncio.gather(*pending, return_exceptions=True) |
| 106 | + |
| 107 | + if disconnect_task in done and disconnect_task.result(): |
| 108 | + if not wait_task.done(): |
| 109 | + wait_task.cancel() |
| 110 | + await asyncio.gather(wait_task, return_exceptions=True) |
| 111 | + raise HTTPException(status_code=499, detail=f"Client disconnected, task {task_id} cancelled") |
| 112 | + |
| 113 | + return wait_task.result() |
| 114 | + except RuntimeError as e: |
| 115 | + raise HTTPException(status_code=503, detail=str(e)) |
| 116 | + except HTTPException: |
| 117 | + raise |
| 118 | + except asyncio.CancelledError: |
| 119 | + if task_id: |
| 120 | + task_manager.cancel_task(task_id) |
| 121 | + raise |
| 122 | + except Exception as e: |
| 123 | + logger.error(f"Failed to run OpenAI-compatible image task: {e}") |
| 124 | + raise HTTPException(status_code=500, detail=str(e)) |
| 125 | + |
| 126 | + |
| 127 | +def _build_url_response(request: Request, task_id: str, image_bytes: bytes) -> str: |
| 128 | + services = get_services() |
| 129 | + assert services.file_service is not None, "File service is not initialized" |
| 130 | + |
| 131 | + file_name = f"{task_id}.png" |
| 132 | + output_path = services.file_service.output_video_dir / file_name |
| 133 | + output_path.parent.mkdir(parents=True, exist_ok=True) |
| 134 | + _write_file_sync(output_path, image_bytes) |
| 135 | + |
| 136 | + base = str(request.base_url).rstrip("/") |
| 137 | + return f"{base}/v1/files/download/{file_name}" |
| 138 | + |
| 139 | + |
| 140 | +def _build_openai_response(request: Request, task_id: str, image_bytes: bytes, response_format: Literal["url", "b64_json"]): |
| 141 | + if response_format == "b64_json": |
| 142 | + return OpenAIImageResponse(created=int(time.time()), data=[{"b64_json": base64.b64encode(image_bytes).decode("utf-8")}]) |
| 143 | + |
| 144 | + return OpenAIImageResponse(created=int(time.time()), data=[{"url": _build_url_response(request, task_id, image_bytes)}]) |
| 145 | + |
| 146 | + |
| 147 | +def _build_image_task_request( |
| 148 | + prompt: str, |
| 149 | + *, |
| 150 | + negative_prompt: str = "", |
| 151 | + seed: Optional[int] = None, |
| 152 | + target_shape: Optional[list[int]] = None, |
| 153 | + image_path: str = "", |
| 154 | + image_mask_path: str = "", |
| 155 | +) -> ImageTaskRequest: |
| 156 | + payload = { |
| 157 | + "prompt": prompt, |
| 158 | + "negative_prompt": negative_prompt, |
| 159 | + "image_path": image_path, |
| 160 | + "image_mask_path": image_mask_path, |
| 161 | + } |
| 162 | + if target_shape: |
| 163 | + payload["target_shape"] = target_shape |
| 164 | + if seed is not None: |
| 165 | + payload["seed"] = seed |
| 166 | + return ImageTaskRequest(**payload) |
| 167 | + |
| 168 | + |
| 169 | +@router.post("/generations", response_model=OpenAIImageResponse) |
| 170 | +async def create_openai_image_generation(request: Request, body: OpenAIImageGenerationRequest): |
| 171 | + if body.n != 1: |
| 172 | + raise HTTPException(status_code=400, detail="Only n=1 is currently supported") |
| 173 | + if not body.prompt.strip(): |
| 174 | + raise HTTPException(status_code=400, detail="prompt is required") |
| 175 | + |
| 176 | + target_shape = None |
| 177 | + if body.size: |
| 178 | + try: |
| 179 | + width, height = _shape_from_size(body.size) |
| 180 | + target_shape = [height, width] |
| 181 | + except ValueError as e: |
| 182 | + raise HTTPException(status_code=400, detail=str(e)) |
| 183 | + |
| 184 | + message = _build_image_task_request( |
| 185 | + prompt=body.prompt, |
| 186 | + seed=body.seed, |
| 187 | + target_shape=target_shape, |
| 188 | + ) |
| 189 | + |
| 190 | + result_png = await _run_sync_image_task(request, message) |
| 191 | + return _build_openai_response(request, message.task_id, result_png, body.response_format) |
| 192 | + |
| 193 | + |
| 194 | +async def _save_upload_file(file: UploadFile, target_dir: Path) -> str: |
| 195 | + if not file.filename: |
| 196 | + raise HTTPException(status_code=400, detail="Uploaded file has no filename") |
| 197 | + |
| 198 | + file_extension = Path(file.filename).suffix or ".png" |
| 199 | + unique_filename = f"{uuid.uuid4()}{file_extension}" |
| 200 | + file_path = target_dir / unique_filename |
| 201 | + |
| 202 | + content = await file.read() |
| 203 | + if not content: |
| 204 | + raise HTTPException(status_code=400, detail=f"Uploaded file is empty: {file.filename}") |
| 205 | + await asyncio.to_thread(_write_file_sync, file_path, content) |
| 206 | + return str(file_path) |
| 207 | + |
| 208 | + |
| 209 | +@router.post("/edits", response_model=OpenAIImageResponse) |
| 210 | +async def create_openai_image_edit( |
| 211 | + request: Request, |
| 212 | + image: UploadFile = File(...), |
| 213 | + prompt: str = Form(...), |
| 214 | + mask: UploadFile | None = File(default=None), |
| 215 | + model: str | None = Form(default=None), |
| 216 | + n: int = Form(default=1), |
| 217 | + size: str | None = Form(default=None), |
| 218 | + response_format: Literal["url", "b64_json"] = Form(default="url"), |
| 219 | + user: str | None = Form(default=None), |
| 220 | + negative_prompt: str = Form(default=""), |
| 221 | + seed: int | None = Form(default=None), |
| 222 | +): |
| 223 | + _ = model, user |
| 224 | + if n != 1: |
| 225 | + raise HTTPException(status_code=400, detail="Only n=1 is currently supported") |
| 226 | + if not prompt.strip(): |
| 227 | + raise HTTPException(status_code=400, detail="prompt is required") |
| 228 | + |
| 229 | + services = get_services() |
| 230 | + assert services.file_service is not None, "File service is not initialized" |
| 231 | + |
| 232 | + target_shape = None |
| 233 | + if size: |
| 234 | + try: |
| 235 | + width, height = _shape_from_size(size) |
| 236 | + target_shape = [height, width] |
| 237 | + except ValueError as e: |
| 238 | + raise HTTPException(status_code=400, detail=str(e)) |
| 239 | + |
| 240 | + image_path = await _save_upload_file(image, services.file_service.input_image_dir) |
| 241 | + image_mask_path = "" |
| 242 | + if mask is not None: |
| 243 | + image_mask_path = await _save_upload_file(mask, services.file_service.input_image_dir) |
| 244 | + |
| 245 | + message = _build_image_task_request( |
| 246 | + prompt=prompt, |
| 247 | + negative_prompt=negative_prompt, |
| 248 | + seed=seed, |
| 249 | + target_shape=target_shape, |
| 250 | + image_path=image_path, |
| 251 | + image_mask_path=image_mask_path, |
| 252 | + ) |
| 253 | + |
| 254 | + result_png = await _run_sync_image_task(request, message) |
| 255 | + return _build_openai_response(request, message.task_id, result_png, response_format) |
0 commit comments