Spaces:
Build error
Build error
| import time | |
| import asyncio | |
| import base64 | |
| from typing import Optional | |
| from fastapi import FastAPI | |
| from pydantic import BaseModel | |
| app = FastAPI() | |
| start_time = time.time() | |
| class GenerateRequest(BaseModel): | |
| prompt: str | |
| height: Optional[int] = 512 | |
| width: Optional[int] = 512 | |
| num_inference_steps: Optional[int] = 50 | |
| guidance_scale: Optional[float] = 7.5 | |
| return_base64: Optional[bool] = False | |
| async def generate(req: GenerateRequest): | |
| # شبیهسازی کار سنگین (میتوانید این بخش را به نمونه واقعی جایگزین کنید) | |
| await asyncio.sleep(0.5) # نصف ثانیه تا یک ثانیه | |
| payload = f"Generated for: {req.prompt}".encode("utf-8") | |
| if req.return_base64: | |
| return {"base64": base64.b64encode(payload).decode()} | |
| return { | |
| "prompt": req.prompt, | |
| "height": req.height, | |
| "width": req.width, | |
| "num_inference_steps": req.num_inference_steps, | |
| "guidance_scale": req.guidance_scale, | |
| "output": payload.decode("utf-8", errors="ignore") | |
| } | |
| async def status(): | |
| uptime = int(time.time() - start_time) | |
| return { | |
| "ready": True, | |
| "uptime_seconds": uptime, | |
| "docs": "/docs", | |
| "openapi": "/openapi.json" | |
| } | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True) |