File size: 9,907 Bytes
58f0729
5efe51a
58f0729
 
5efe51a
 
 
58f0729
5efe51a
 
 
 
 
b98c114
5efe51a
b98c114
5efe51a
 
 
 
58f0729
 
 
 
 
 
5efe51a
 
 
 
 
 
58f0729
 
 
 
 
 
 
5efe51a
58f0729
 
5efe51a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58f0729
 
 
5efe51a
1223036
 
5efe51a
58f0729
 
 
 
b98c114
58f0729
5efe51a
58f0729
 
 
 
5efe51a
 
 
b98c114
 
 
 
5efe51a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58f0729
 
 
5efe51a
 
 
 
 
 
 
 
c3d87b5
 
 
 
 
 
 
 
 
 
 
5efe51a
 
 
 
 
 
 
 
 
 
58f0729
6deb156
 
 
 
 
 
 
 
5efe51a
58f0729
 
 
 
5efe51a
 
58f0729
5efe51a
58f0729
5efe51a
 
1223036
58f0729
5efe51a
 
 
 
 
 
58f0729
5efe51a
 
 
 
 
 
 
 
 
 
 
 
58f0729
1223036
 
 
 
921c0b3
8cb8f7a
 
 
921c0b3
8cb8f7a
921c0b3
8cb8f7a
921c0b3
 
 
8cb8f7a
 
 
 
 
 
 
a18a676
 
 
 
 
 
921c0b3
5efe51a
1223036
 
58f0729
1223036
 
921c0b3
5efe51a
 
58f0729
5efe51a
 
 
 
58f0729
5efe51a
1223036
 
5efe51a
 
 
 
 
58f0729
 
 
5efe51a
58f0729
 
5efe51a
58f0729
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
"""
Production-focused FastAPI wrapper for SmolVLM2 video highlights.
"""

import asyncio
import json
import logging
import os
import re
import sys
import time
import uuid
from pathlib import Path
from typing import Dict, Optional

from fastapi import FastAPI, File, HTTPException, UploadFile
from fastapi.concurrency import run_in_threadpool
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from pydantic import BaseModel

# Set cache directories to writable locations for HuggingFace Spaces
# Use /tmp which is guaranteed to be writable in containers
CACHE_DIR = os.path.join("/tmp", ".cache", "huggingface")
os.makedirs(CACHE_DIR, exist_ok=True)
os.makedirs(os.path.join("/tmp", ".cache", "torch"), exist_ok=True)
os.environ["HF_HOME"] = CACHE_DIR
os.environ["HF_DATASETS_CACHE"] = CACHE_DIR
os.environ["TORCH_HOME"] = os.path.join("/tmp", ".cache", "torch")
os.environ["XDG_CACHE_HOME"] = os.path.join("/tmp", ".cache")
os.environ["HUGGINGFACE_HUB_CACHE"] = CACHE_DIR
os.environ["TOKENIZERS_PARALLELISM"] = "false"

# Add src directory to path for imports
sys.path.append(str(Path(__file__).parent / "src"))

try:
    from huggingface_exact_approach import VideoHighlightDetector
except ImportError:
    print("Cannot import huggingface_exact_approach.py")
    sys.exit(1)

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

# Runtime configuration
APP_START_TIME = time.time()
DEFAULT_MODEL = os.getenv("DEFAULT_MODEL_NAME", "HuggingFaceTB/SmolVLM2-256M-Video-Instruct")
MODEL_DEVICE = os.getenv("MODEL_DEVICE", "auto").lower()
MAX_UPLOAD_BYTES = int(os.getenv("MAX_UPLOAD_BYTES", str(512 * 1024 * 1024)))  # 512MB
MAX_CONCURRENT_JOBS = int(os.getenv("MAX_CONCURRENT_JOBS", "1"))
PROCESS_TIMEOUT_SECONDS = int(os.getenv("PROCESS_TIMEOUT_SECONDS", "3600"))

# Directories
TEMP_DIR = os.path.join("/tmp", "temp")
OUTPUTS_DIR = os.path.join("/tmp", "outputs")
os.makedirs(OUTPUTS_DIR, mode=0o755, exist_ok=True)
os.makedirs(TEMP_DIR, mode=0o755, exist_ok=True)

if MODEL_DEVICE not in {"auto", "cpu", "cuda", "mps"}:
    raise RuntimeError(f"Invalid MODEL_DEVICE '{MODEL_DEVICE}'. Use auto/cpu/cuda/mps.")


class AnalysisResponse(BaseModel):
    success: bool
    message: str
    video_description: str
    highlights: str
    analysis_file: str


def _sentence_count(text: str) -> int:
    return len([s.strip() for s in re.split(r"[.!?]+", text or "") if s.strip()])


def _device_for_detector() -> Optional[str]:
    return None if MODEL_DEVICE == "auto" else MODEL_DEVICE


class DetectorRegistry:
    """In-memory singleton detector registry keyed by model name."""

    def __init__(self) -> None:
        self._detectors: Dict[str, VideoHighlightDetector] = {}
        self._lock = asyncio.Lock()

    async def get(self, model_name: str) -> VideoHighlightDetector:
        if model_name in self._detectors:
            return self._detectors[model_name]

        async with self._lock:
            # Double-check after lock acquire.
            if model_name in self._detectors:
                return self._detectors[model_name]

            logger.info("Loading detector model '%s' (device=%s)", model_name, MODEL_DEVICE)
            detector = await run_in_threadpool(
                VideoHighlightDetector,
                model_name,
                _device_for_detector(),
                16,
            )
            self._detectors[model_name] = detector
            logger.info("Model '%s' loaded and cached", model_name)
            return detector

    async def warmup(self, model_name: str) -> None:
        await self.get(model_name)

    def loaded_models(self) -> Dict[str, str]:
        return {name: getattr(detector, "device", "unknown") for name, detector in self._detectors.items()}


detector_registry = DetectorRegistry()
processing_semaphore = asyncio.Semaphore(MAX_CONCURRENT_JOBS)

app = FastAPI(
    title="SmolVLM2 Optimized HuggingFace Video Highlights API",
    description="Generate intelligent video highlights using SmolVLM2 segment-based approach",
    version="3.0.0",
    openapi_url=None,
    docs_url=None,
    redoc_url=None,
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["POST", "GET"],
    allow_headers=["*"],
)


@app.on_event("startup")
async def _startup() -> None:
    logger.info("Startup: default_model=%s, model_device=%s", DEFAULT_MODEL, MODEL_DEVICE)
    try:
        await detector_registry.warmup(DEFAULT_MODEL)
    except Exception:
        logger.exception("Model warmup failed")


async def _save_upload_stream(upload: UploadFile, path: str) -> int:
    size = 0
    chunk_size = 1024 * 1024
    with open(path, "wb") as buffer:
        while True:
            chunk = await upload.read(chunk_size)
            if not chunk:
                break
            size += len(chunk)
            if size > MAX_UPLOAD_BYTES:
                raise HTTPException(
                    status_code=413,
                    detail=f"Uploaded file too large. Max size is {MAX_UPLOAD_BYTES} bytes.",
                )
            buffer.write(chunk)
    return size


async def _acquire_processing_slot() -> None:
    try:
        await asyncio.wait_for(processing_semaphore.acquire(), timeout=0.05)
    except asyncio.TimeoutError:
        raise HTTPException(status_code=429, detail="Server is busy. Try again shortly.")


@app.get("/health")
async def health_check():
    return {
        "status": "healthy",
        "uptime_seconds": int(time.time() - APP_START_TIME),
        "default_model": DEFAULT_MODEL,
        "loaded_models": detector_registry.loaded_models(),
    }


@app.get("/")
async def root():
    return {
        "service": "SmolVLM2 Video Highlights API",
        "status": "ok",
        "health": "/health",
        "ready": "/ready",
        "upload": "/upload-video",
    }


@app.get("/ready")
async def readiness_check():
    loaded = detector_registry.loaded_models()
    ready = DEFAULT_MODEL in loaded
    return {
        "status": "ready" if ready else "not_ready",
        "default_model": DEFAULT_MODEL,
        "loaded_models": loaded,
    }


@app.get("/tmp/outputs/{filename}")
async def get_output_file(filename: str):
    safe_name = os.path.basename(filename)
    file_path = os.path.join(OUTPUTS_DIR, safe_name)
    if not os.path.exists(file_path):
        raise HTTPException(status_code=404, detail="File not found")
    return FileResponse(path=file_path, filename=safe_name)


@app.post("/upload-video", response_model=AnalysisResponse)
async def upload_video(
    video: UploadFile = File(...),
    segment_length: float = 5.0,
    model_name: str = DEFAULT_MODEL,
    with_effects: bool = True,
):
    if not video.content_type or not video.content_type.startswith("video/"):
        raise HTTPException(status_code=400, detail="File must be a video")
    if segment_length <= 0:
        raise HTTPException(status_code=400, detail="segment_length must be > 0")
    job_id = str(uuid.uuid4())
    temp_video_path = os.path.join(TEMP_DIR, f"{job_id}_input.mp4")
    output_filename = f"{job_id}_highlights.mp4"
    analysis_filename = f"{job_id}_analysis.json"
    output_path = os.path.join(OUTPUTS_DIR, output_filename)
    analysis_path = os.path.join(OUTPUTS_DIR, analysis_filename)

    await _acquire_processing_slot()
    try:
        await _save_upload_stream(video, temp_video_path)
        detector = await detector_registry.get(model_name)

        results = await asyncio.wait_for(
            run_in_threadpool(
                detector.process_video,
                temp_video_path,
                output_path,
                segment_length,
                with_effects,
            ),
            timeout=PROCESS_TIMEOUT_SECONDS,
        )

        if "error" in results:
            raise HTTPException(status_code=500, detail=results["error"])

        selected_set = str(results.get("selected_set", "")).strip()
        h1 = results.get("highlights1", "")
        h2 = results.get("highlights2", "")
        base_desc = results.get("video_description", "")
        if selected_set == "1":
            enriched_description = h1
        elif selected_set == "2":
            enriched_description = h2
        else:
            enriched_description = h1 or h2 or base_desc

        if _sentence_count(h1) > _sentence_count(enriched_description):
            enriched_description = h1
        if _sentence_count(h2) > _sentence_count(enriched_description):
            enriched_description = h2
        if not enriched_description:
            enriched_description = base_desc

        logger.info(
            "API response selected_set=%s video_description=%s",
            selected_set or "fallback",
            enriched_description,
        )

        results["video_description"] = enriched_description
        with open(analysis_path, "w") as f:
            json.dump(results, f, indent=2)

        return AnalysisResponse(
            success=True,
            message="Video description generated successfully",
            video_description=enriched_description,
            highlights=f"/tmp/outputs/{output_filename}",
            analysis_file=f"/tmp/outputs/{analysis_filename}",
        )
    except asyncio.TimeoutError:
        raise HTTPException(status_code=504, detail="Processing timed out")
    except HTTPException:
        raise
    except Exception as e:
        logger.exception("Upload processing failed")
        raise HTTPException(status_code=500, detail=f"Failed to process upload: {str(e)}")
    finally:
        processing_semaphore.release()
        try:
            await video.close()
        except Exception:
            pass
        if os.path.exists(temp_video_path):
            os.unlink(temp_video_path)


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, host="0.0.0.0", port=7860)