from fastapi import FastAPI, UploadFile, File, Form, HTTPException from fastapi.responses import FileResponse, HTMLResponse, RedirectResponse from fastapi.middleware.cors import CORSMiddleware from pathlib import Path import uvicorn import os from renderer import render_logo ROOT = Path(__file__).resolve().parent.parent FRONTEND = ROOT / "frontend" app = FastAPI(title="Manim Logo Renderer") # CORS (Spaces ve lokal için serbest) app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"], ) # ---- Routes ---- @app.get("/", include_in_schema=False) async def root(): # Kök istekleri UI'ye yönlendir return RedirectResponse(url="/web") @app.get("/web", response_class=HTMLResponse) async def web(): index = FRONTEND / "index.html" if not index.exists(): raise HTTPException(status_code=404, detail="frontend/index.html not found") return index.read_text(encoding="utf-8") @app.get("/health") async def health(): return {"ok": True} @app.post("/render") async def render( file: UploadFile = File(...), animation: str = Form(...), # draw | fade | spin | bounce duration: float = Form(4.0), bg_color: str = Form("#111111"), quality: str = Form("m"), # l|m|h|u ): try: out_path = await render_logo(file, animation, duration, bg_color, quality) except ValueError as e: raise HTTPException(status_code=400, detail=str(e)) except Exception as e: raise HTTPException(status_code=500, detail=f"Render failed: {e}") # MP4 döndür return FileResponse( path=out_path, media_type="video/mp4", filename="logo_animation.mp4", ) if __name__ == "__main__": port = int(os.environ.get("PORT", "7860")) uvicorn.run("app:app", host="0.0.0.0", port=port)