9
9
# - docker compose -f docker-compose.dev.yaml up
10
10
11
11
import logging
12
- from collections .abc import Iterator
13
12
from pathlib import Path
14
13
15
- import anyio
16
- import gradio as gr
17
- import numpy as np
18
14
import uvicorn
19
15
from fastapi import FastAPI
20
16
from fastapi .middleware .cors import CORSMiddleware
21
- from fastapi .responses import HTMLResponse
22
- from fastrtc import AdditionalOutputs , Stream
23
- from pydantic import BaseModel , Field
17
+ from fastapi .responses import FileResponse
24
18
25
- from app .api .endpoints import models , pipelines , sinks , sources , system
26
- from app .core import Scheduler , lifespan
19
+ from app .api .endpoints import models , pipelines , sinks , sources , system , webrtc
20
+ from app .core import lifespan
27
21
from app .settings import get_settings
28
22
29
23
settings = get_settings ()
34
28
)
35
29
logger = logging .getLogger (__name__ )
36
30
37
-
38
- def rtc_stream_routine () -> Iterator [tuple [np .ndarray , AdditionalOutputs ]]:
39
- """Iterator to send frames with predictions to the WebRTC visualization stream"""
40
- scheduler = Scheduler ()
41
- while not scheduler .mp_stop_event .is_set ():
42
- yield scheduler .rtc_stream_queue .get ()
43
- logger .info ("Stopped RTC stream routine" )
44
-
45
-
46
- stream = Stream (
47
- handler = rtc_stream_routine ,
48
- modality = "video" ,
49
- mode = "receive" ,
50
- additional_outputs = [
51
- gr .Textbox (label = "Predictions" ),
52
- ],
53
- additional_outputs_handler = lambda _c1 , pred : pred ,
54
- )
55
-
56
31
app = FastAPI (
57
32
title = settings .app_name ,
58
33
version = settings .version ,
@@ -78,28 +53,21 @@ def rtc_stream_routine() -> Iterator[tuple[np.ndarray, AdditionalOutputs]]:
78
53
app .include_router (pipelines .router )
79
54
app .include_router (models .router )
80
55
app .include_router (system .router )
56
+ app .include_router (webrtc .router )
81
57
82
58
cur_dir = Path (__file__ ).parent
83
59
84
60
85
61
@app .get ("/api/docs" , include_in_schema = False )
86
- async def get_scalar_docs () -> HTMLResponse :
62
+ async def get_scalar_docs () -> FileResponse :
87
63
"""Shows docs for our OpenAPI specification using scalar"""
88
- async with await anyio .open_file (cur_dir / "scalar.html" ) as file :
89
- html_content = await file .read ()
90
- return HTMLResponse (content = html_content )
64
+ return FileResponse (cur_dir / "static" / "scalar.html" )
91
65
92
66
93
- class InputData (BaseModel ):
94
- webrtc_id : str
95
- conf_threshold : float = Field (ge = 0 , le = 1 )
96
-
97
-
98
- # TODO remove this endpoint, make sure the UI does not require it
99
- @app .post ("/api/input_hook" , tags = ["webrtc" ])
100
- async def webrtc_input_hook (data : InputData ) -> None :
101
- """Update webrtc input for user"""
102
- stream .set_input (data .webrtc_id , data .conf_threshold )
67
+ @app .get ("/stream" , include_in_schema = False )
68
+ async def get_webrtc_stream () -> FileResponse :
69
+ """Get webrtc player"""
70
+ return FileResponse (cur_dir / "static" / "webrtc.html" )
103
71
104
72
105
73
@app .get ("/health" )
@@ -108,9 +76,6 @@ async def health_check() -> dict[str, str]:
108
76
return {"status" : "ok" }
109
77
110
78
111
- stream .mount (app , "/api" )
112
-
113
-
114
79
def main () -> None :
115
80
"""Main application entry point"""
116
81
logger .info (f"Starting { settings .app_name } in { settings .environment } mode" )
0 commit comments