Spaces:
Sleeping
Sleeping
Download iris/api/routes.py from NextNeural/IRIS: direct link, hf CLI and curl.
- Browser
- Download file 6.43 kB
-
https://huggingface.co/spaces/NextNeural/IRIS/resolve/main/iris/api/routes.py
- Command line
-
hf download hf://spaces/NextNeural/IRIS/iris/api/routes.py
-
curl -L -o routes.py https://huggingface.co/spaces/NextNeural/IRIS/resolve/main/iris/api/routes.py
6.43 kB
| from __future__ import annotations | |
| from pathlib import Path | |
| from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, UploadFile, status | |
| from iris.api.dependencies import get_database, get_file_store, get_settings, get_video_processor | |
| from iris.config import Settings, device_info | |
| from iris.models.schemas import ( | |
| SearchRequest, | |
| SearchResponse, | |
| SearchResultResponse, | |
| TranscriptChunkResponse, | |
| TranscriptResponse, | |
| UploadVideoResponse, | |
| VideoStatusResponse, | |
| ) | |
| from iris.pipeline.chunk import window_transcript_chunks | |
| from iris.pipeline.embed import LocalEmbedder | |
| from iris.pipeline.search import detail_window_settings, format_timestamp, rank_speech_chunks | |
| from iris.pipeline.visual import VisualSearcher, sample_video_frames | |
| from iris.storage.database import Database | |
| from iris.storage.files import FileStore | |
| from iris.pipeline.orchestrator import VideoProcessor | |
| router = APIRouter() | |
| def health(settings: Settings = Depends(get_settings)) -> dict: | |
| return {"status": "ok", "device": device_info(settings.device, settings.torch_dtype)} | |
| def upload_video( | |
| file: UploadFile, | |
| background_tasks: BackgroundTasks, | |
| database: Database = Depends(get_database), | |
| file_store: FileStore = Depends(get_file_store), | |
| processor: VideoProcessor = Depends(get_video_processor), | |
| ) -> UploadVideoResponse: | |
| try: | |
| video_id, path = file_store.save_upload(file) | |
| except ValueError as exc: | |
| raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc | |
| database.create_video(video_id, file.filename or path.name, path) | |
| if getattr(processor, "process_video", None) and processor is not None: | |
| background_tasks.add_task(processor.process_video, video_id) | |
| return UploadVideoResponse(video_id=video_id, status="queued") | |
| def process_video_again( | |
| video_id: str, | |
| background_tasks: BackgroundTasks, | |
| database: Database = Depends(get_database), | |
| processor: VideoProcessor = Depends(get_video_processor), | |
| ) -> UploadVideoResponse: | |
| _get_video_or_404(database, video_id) | |
| database.update_video_status(video_id, "queued") | |
| background_tasks.add_task(processor.process_video, video_id) | |
| return UploadVideoResponse(video_id=video_id, status="queued") | |
| def get_video_status( | |
| video_id: str, | |
| database: Database = Depends(get_database), | |
| ) -> VideoStatusResponse: | |
| record = _get_video_or_404(database, video_id) | |
| return VideoStatusResponse(video_id=video_id, status=record["status"], error=record["error"]) | |
| def get_transcript( | |
| video_id: str, | |
| database: Database = Depends(get_database), | |
| ) -> TranscriptResponse: | |
| _get_video_or_404(database, video_id) | |
| chunks = database.list_transcript_chunks(video_id) | |
| return TranscriptResponse( | |
| video_id=video_id, | |
| chunks=[TranscriptChunkResponse(start=chunk.start, end=chunk.end, text=chunk.text) for chunk in chunks], | |
| ) | |
| def search_video( | |
| video_id: str, | |
| request: SearchRequest, | |
| database: Database = Depends(get_database), | |
| file_store: FileStore = Depends(get_file_store), | |
| settings: Settings = Depends(get_settings), | |
| ) -> SearchResponse: | |
| record = _get_video_or_404(database, video_id) | |
| if record["status"] != "completed": | |
| raise HTTPException( | |
| status_code=status.HTTP_409_CONFLICT, | |
| detail=f"Video is not ready for search. Current status: {record['status']}.", | |
| ) | |
| atomic_chunks = database.list_transcript_chunks(video_id) | |
| default_window, default_overlap, default_merge_gap = detail_window_settings(request.detail) | |
| window_seconds = request.window_seconds or default_window | |
| overlap_seconds = min(default_overlap, max(0.0, window_seconds - 0.1)) | |
| merge_gap_seconds = request.merge_gap_seconds if request.merge_gap_seconds is not None else default_merge_gap | |
| chunks = window_transcript_chunks( | |
| atomic_chunks, | |
| window_seconds=window_seconds, | |
| overlap_seconds=overlap_seconds, | |
| ) | |
| embedder = LocalEmbedder(settings) | |
| chunk_embeddings = embedder.encode([chunk.text for chunk in chunks]) | |
| query_embedding = embedder.encode([request.query])[0] | |
| speech_results = rank_speech_chunks( | |
| query=request.query, | |
| chunks=chunks, | |
| chunk_embeddings=chunk_embeddings, | |
| query_embedding=query_embedding, | |
| settings=settings, | |
| limit=request.limit, | |
| merge_gap_seconds=merge_gap_seconds, | |
| ) | |
| results = list(speech_results) | |
| if request.include_visual: | |
| if not settings.visual_search_enabled: | |
| raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Visual search is disabled.") | |
| frames = sample_video_frames( | |
| Path(record["video_path"]), | |
| file_store.frames_dir(video_id), | |
| settings.frame_sample_interval_seconds, | |
| ) | |
| visual_results = VisualSearcher(settings).search(request.query, frames, request.limit) | |
| results.extend(visual_results) | |
| database.record_search(video_id, request.query, request.include_visual) | |
| ranked = sorted(results, key=lambda item: (-item.score, item.start))[: request.limit] | |
| return SearchResponse( | |
| video_id=video_id, | |
| query=request.query, | |
| results=[ | |
| SearchResultResponse( | |
| start=item.start, | |
| end=item.end, | |
| start_time=format_timestamp(item.start), | |
| end_time=format_timestamp(item.end), | |
| timestamp=f"{format_timestamp(item.start)}-{format_timestamp(item.end)}", | |
| text=item.text, | |
| score=item.score, | |
| source=item.source, | |
| ) | |
| for item in ranked | |
| ], | |
| ) | |
| def _get_video_or_404(database: Database, video_id: str) -> dict: | |
| record = database.get_video(video_id) | |
| if record is None: | |
| raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Video not found.") | |
| return record | |