File size: 9,160 Bytes
f020d6c
 
 
 
 
 
 
 
 
 
 
 
 
 
4e88441
f020d6c
12c4b57
 
 
 
 
 
 
 
4e88441
 
f020d6c
 
 
 
 
 
 
 
 
 
62b7f0a
f020d6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34c09b7
f020d6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87a48cb
f020d6c
87a48cb
 
 
 
 
 
 
f020d6c
 
 
 
87a48cb
f020d6c
 
 
 
87a48cb
 
 
 
 
f020d6c
87a48cb
f020d6c
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
"""
app_hf.py β€” Simplified FastAPI backend for Hugging Face Spaces.

Differences from main.py:
  - No Celery / Redis required.
  - In-memory job registry (jobs dict).
  - ThreadPoolExecutor runs inference in background thread.
  - Serves Next.js static export from ../frontend/out/ on all non-API routes.
"""

import os
import uuid
import asyncio
import logging
import sys
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor
from typing import Any, Dict

from fastapi import FastAPI, UploadFile, File, HTTPException, WebSocket, WebSocketDisconnect
from fastapi.responses import FileResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware

# Add current directory to path so relative imports work without package structure
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from inference import process_video, get_model, VOC_CLASSES

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# ─── Paths ────────────────────────────────────────────────────────────────────

UPLOAD_DIR = Path(os.getenv("UPLOAD_DIR", "/tmp/video_seg/uploads"))
OUTPUT_DIR = Path(os.getenv("OUTPUT_DIR", "/tmp/video_seg/outputs"))
# In Docker: /app/backend/../frontend/out = /app/frontend/out
STATIC_DIR = Path(__file__).parent.parent / "frontend" / "out"

UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)

ALLOWED_EXTENSIONS = {".mp4", ".avi", ".mov", ".mkv", ".webm"}
MAX_FILE_SIZE_MB   = int(os.getenv("MAX_FILE_SIZE_MB", "200"))

# ─── In-memory job registry ───────────────────────────────────────────────────

jobs: Dict[str, Dict[str, Any]] = {}
executor = ThreadPoolExecutor(max_workers=2)

# ─── App ─────────────────────────────────────────────────────────────────────

app = FastAPI(title="SegVision HF API", version="1.0.0")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)


@app.on_event("startup")
async def startup():
    logger.info("Loading segmentation model…")
    loop = asyncio.get_event_loop()
    await loop.run_in_executor(executor, get_model)
    logger.info("Model ready.")


# ─── Background inference runner ─────────────────────────────────────────────

def _run_inference(job_id: str, input_path: str, output_path: str):
    """Run video segmentation synchronously (called in thread pool)."""
    jobs[job_id]["status"] = "processing"

    def on_progress(pct: float, detected_names: list):
        jobs[job_id].update({"pct": pct, "detected": detected_names})

    try:
        detected_ids = process_video(
            input_path, output_path, progress_callback=on_progress
        )
        detected_names = [
            VOC_CLASSES[c] for c in sorted(detected_ids) if c < len(VOC_CLASSES)
        ]
        jobs[job_id].update({
            "status":   "done",
            "pct":      100.0,
            "detected": detected_names,
        })
        logger.info(f"[{job_id}] Done β€” detected: {detected_names}")
    except Exception as exc:
        logger.exception(f"[{job_id}] Inference failed")
        jobs[job_id].update({"status": "error", "error": str(exc)})


# ─── API Endpoints ────────────────────────────────────────────────────────────

@app.post("/api/upload")
async def upload_video(file: UploadFile = File(...)):
    logger.info(f"Incoming upload request: filename='{file.filename}', content_type='{file.content_type}'")
    ext = Path(file.filename or "x.mp4").suffix.lower()
    if ext not in ALLOWED_EXTENSIONS:
        raise HTTPException(400, f"Unsupported format '{ext}'.")

    content = await file.read()
    size_mb = len(content) / (1024 * 1024)
    if size_mb > MAX_FILE_SIZE_MB:
        raise HTTPException(413, f"File too large ({size_mb:.1f} MB). Max {MAX_FILE_SIZE_MB} MB.")

    job_id      = str(uuid.uuid4())
    input_path  = UPLOAD_DIR / f"{job_id}{ext}"
    output_path = OUTPUT_DIR / f"{job_id}_output.mp4"

    with open(input_path, "wb") as f:
        f.write(content)

    jobs[job_id] = {"status": "queued", "pct": 0.0, "detected": []}

    loop = asyncio.get_event_loop()
    loop.run_in_executor(executor, _run_inference, job_id, str(input_path), str(output_path))

    logger.info(f"[{job_id}] Queued: {file.filename} ({size_mb:.1f} MB)")
    return {"job_id": job_id, "status": "queued"}


@app.get("/api/status/{job_id}")
async def get_status(job_id: str):
    if job_id in jobs:
        return {"job_id": job_id, **jobs[job_id]}

    # Fallback: check if the output file exists (handles server restart)
    out = OUTPUT_DIR / f"{job_id}_output.mp4"
    if out.exists():
        return {"job_id": job_id, "status": "done", "pct": 100.0, "detected": []}

    raise HTTPException(404, "Job not found")


@app.head("/api/video/{job_id}")
@app.get("/api/video/{job_id}")
async def get_video(job_id: str):
    output_path = OUTPUT_DIR / f"{job_id}_output.mp4"
    if not output_path.exists():
        raise HTTPException(404, "Result not ready yet")
    return FileResponse(
        str(output_path),
        media_type="video/mp4",
        filename=f"segmented_{job_id[:8]}.mp4",
    )


@app.delete("/api/job/{job_id}")
async def delete_job(job_id: str):
    jobs.pop(job_id, None)
    for path in UPLOAD_DIR.glob(f"{job_id}*"):
        path.unlink(missing_ok=True)
    for path in OUTPUT_DIR.glob(f"{job_id}*"):
        path.unlink(missing_ok=True)
    return {"job_id": job_id, "status": "deleted"}


@app.get("/api/health")
async def health():
    import torch
    return {"status": "ok", "device": "cuda" if torch.cuda.is_available() else "cpu"}


# ─── WebSocket progress ───────────────────────────────────────────────────────

@app.websocket("/ws/{job_id}")
async def websocket_progress(ws: WebSocket, job_id: str):
    await ws.accept()
    try:
        while True:
            if job_id in jobs:
                job = jobs[job_id]
                await ws.send_json({"job_id": job_id, **job})
                if job["status"] in ("done", "error"):
                    break
            else:
                out = OUTPUT_DIR / f"{job_id}_output.mp4"
                if out.exists():
                    await ws.send_json({"status": "done", "pct": 100.0, "detected": []})
                    break
                await ws.send_json({"status": "queued", "pct": 0.0, "detected": []})
            await asyncio.sleep(0.8)
    except WebSocketDisconnect:
        pass


# ─── Serve Next.js static export ─────────────────────────────────────────────

if STATIC_DIR.exists():
    # Serve Next.js _next/ assets (JS, CSS, images)
    _next_dir = STATIC_DIR / "_next"
    if _next_dir.exists():
        app.mount("/_next", StaticFiles(directory=str(_next_dir)), name="nextjs-assets")

    @app.get("/{full_path:path}")
    async def serve_spa(full_path: str):
        """
        SPA catch-all: try to serve the exact static file, then .html,
        then index.html in the folder (trailingSlash support).
        """
        # Handle root specially
        if not full_path or full_path == "/":
            index = STATIC_DIR / "index.html"
            if index.is_file(): return FileResponse(str(index))
            return JSONResponse({"error": "frontend index.html not found"}, status_code=404)

        # 1. Exact file match (images, JS, CSS)
        candidate = STATIC_DIR / full_path
        if candidate.is_file():
            return FileResponse(str(candidate))

        # 2. Next.js route: try path.html (e.g., /upload -> upload.html)
        html_candidate = STATIC_DIR / f"{full_path}.html"
        if html_candidate.is_file():
            return FileResponse(str(html_candidate))

        # 3. Next.js route with trailingSlash: path/index.html
        # (e.g., /processing/ -> processing/index.html)
        index_candidate = STATIC_DIR / full_path / "index.html"
        if index_candidate.is_file():
            return FileResponse(str(index_candidate))

        # Final fallback: root index.html (client-side routing)
        index = STATIC_DIR / "index.html"
        if index.is_file():
            return FileResponse(str(index))

        raise HTTPException(404, "Not found")
else:
    @app.get("/")
    async def root():
        return {"message": "SegVision API is running. Frontend not found β€” build it first."}