Bandrik0 commited on
Commit
c244342
·
1 Parent(s): 6f05768

app.py change

Browse files
Files changed (1) hide show
  1. backend/app.py +37 -41
backend/app.py CHANGED
@@ -1,68 +1,64 @@
1
- \
2
- import os
3
- import uuid
4
- import shutil
5
  from fastapi import FastAPI, UploadFile, File, Form, HTTPException
 
6
  from fastapi.middleware.cors import CORSMiddleware
7
- from fastapi.responses import FileResponse, JSONResponse
 
 
8
  from renderer import render_logo
9
 
 
 
 
10
  app = FastAPI(title="Manim Logo Renderer")
11
 
12
- # Allow local dev frontends; tighten in production
13
  app.add_middleware(
14
  CORSMiddleware,
15
  allow_origins=["*"],
16
- allow_credentials=True,
17
  allow_methods=["*"],
18
  allow_headers=["*"],
19
  )
20
 
21
- UPLOAD_DIR = "/tmp/logo_uploads"
22
- RENDER_DIR = "/tmp/logo_renders"
23
- os.makedirs(UPLOAD_DIR, exist_ok=True)
24
- os.makedirs(RENDER_DIR, exist_ok=True)
 
 
25
 
26
- VALID_ANIMS = {"draw", "fade", "spin", "bounce"}
 
 
 
 
 
27
 
28
  @app.get("/health")
29
- def health():
30
  return {"ok": True}
31
 
32
  @app.post("/render")
33
  async def render(
34
  file: UploadFile = File(...),
35
- animation: str = Form("draw"),
36
  duration: float = Form(4.0),
37
  bg_color: str = Form("#111111"),
38
- quality: str = Form("m"), # l,m,h,u
39
  ):
40
- ext = os.path.splitext(file.filename or "")[1].lower()
41
- if ext not in {".svg", ".png", ".jpg", ".jpeg", ".webp"}:
42
- raise HTTPException(status_code=400, detail="Bitte SVG/PNG/JPG/WEBP hochladen.")
43
-
44
- if animation not in VALID_ANIMS:
45
- raise HTTPException(status_code=400, detail=f"Unknown animation: {animation}")
46
-
47
- # Save upload
48
- uid = str(uuid.uuid4())
49
- upload_path = os.path.join(UPLOAD_DIR, f"{uid}{ext}")
50
- with open(upload_path, "wb") as out:
51
- shutil.copyfileobj(file.file, out)
52
-
53
- # Render output
54
- out_path = os.path.join(RENDER_DIR, f"{uid}.mp4")
55
  try:
56
- render_logo(
57
- input_path=upload_path,
58
- output_path=out_path,
59
- animation=animation,
60
- duration=float(duration),
61
- bg_color=bg_color,
62
- quality=quality,
63
- )
64
  except Exception as e:
65
- # surface manim errors
66
- return JSONResponse(status_code=500, content={"error": str(e)})
 
 
 
 
 
 
67
 
68
- return FileResponse(out_path, media_type="video/mp4", filename="logo_animation.mp4")
 
 
 
 
 
 
 
1
  from fastapi import FastAPI, UploadFile, File, Form, HTTPException
2
+ from fastapi.responses import FileResponse, HTMLResponse, RedirectResponse
3
  from fastapi.middleware.cors import CORSMiddleware
4
+ from pathlib import Path
5
+ import uvicorn
6
+ import os
7
  from renderer import render_logo
8
 
9
+ ROOT = Path(__file__).resolve().parent.parent
10
+ FRONTEND = ROOT / "frontend"
11
+
12
  app = FastAPI(title="Manim Logo Renderer")
13
 
14
+ # CORS (Spaces ve lokal için serbest)
15
  app.add_middleware(
16
  CORSMiddleware,
17
  allow_origins=["*"],
 
18
  allow_methods=["*"],
19
  allow_headers=["*"],
20
  )
21
 
22
+ # ---- Routes ----
23
+
24
+ @app.get("/", include_in_schema=False)
25
+ async def root():
26
+ # Kök istekleri UI'ye yönlendir
27
+ return RedirectResponse(url="/web")
28
 
29
+ @app.get("/web", response_class=HTMLResponse)
30
+ async def web():
31
+ index = FRONTEND / "index.html"
32
+ if not index.exists():
33
+ raise HTTPException(status_code=404, detail="frontend/index.html not found")
34
+ return index.read_text(encoding="utf-8")
35
 
36
  @app.get("/health")
37
+ async def health():
38
  return {"ok": True}
39
 
40
  @app.post("/render")
41
  async def render(
42
  file: UploadFile = File(...),
43
+ animation: str = Form(...), # draw | fade | spin | bounce
44
  duration: float = Form(4.0),
45
  bg_color: str = Form("#111111"),
46
+ quality: str = Form("m"), # l|m|h|u
47
  ):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  try:
49
+ out_path = await render_logo(file, animation, duration, bg_color, quality)
50
+ except ValueError as e:
51
+ raise HTTPException(status_code=400, detail=str(e))
 
 
 
 
 
52
  except Exception as e:
53
+ raise HTTPException(status_code=500, detail=f"Render failed: {e}")
54
+
55
+ # MP4 döndür
56
+ return FileResponse(
57
+ path=out_path,
58
+ media_type="video/mp4",
59
+ filename="logo_animation.mp4",
60
+ )
61
 
62
+ if __name__ == "__main__":
63
+ port = int(os.environ.get("PORT", "7860"))
64
+ uvicorn.run("app:app", host="0.0.0.0", port=port)