Prathamesh1420 commited on
Commit
018cf2d
·
verified ·
1 Parent(s): 990607c

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ from pathlib import Path
4
+ import cv2
5
+ import gradio as gr
6
+ from fastapi import FastAPI
7
+ from fastapi.responses import HTMLResponse
8
+ from huggingface_hub import hf_hub_download
9
+ from pydantic import BaseModel, Field
10
+ from livekit_token import generate_token
11
+
12
+ try:
13
+ from demo.object_detection.inference import YOLOv10
14
+ except (ImportError, ModuleNotFoundError):
15
+ from inference import YOLOv10
16
+
17
+ cur_dir = Path(__file__).parent
18
+
19
+ model_file = hf_hub_download(repo_id="onnx-community/yolov10n", filename="onnx/model.onnx")
20
+ model = YOLOv10(model_file)
21
+
22
+ def detection(image, conf_threshold=0.3):
23
+ image = cv2.resize(image, (model.input_width, model.input_height))
24
+ new_image = model.detect_objects(image, conf_threshold)
25
+ return cv2.resize(new_image, (500, 500))
26
+
27
+ app = FastAPI()
28
+
29
+ LIVEKIT_URL = os.getenv("LIVEKIT_URL")
30
+ LIVEKIT_API_KEY = os.getenv("LIVEKIT_API_KEY")
31
+ LIVEKIT_API_SECRET = os.getenv("LIVEKIT_API_SECRET")
32
+
33
+ @app.get("/")
34
+ async def _():
35
+ token = generate_token(LIVEKIT_API_KEY, LIVEKIT_API_SECRET, identity="user123")
36
+ html_content = open(cur_dir / "index.html").read()
37
+ html_content = html_content.replace("__LIVEKIT_URL__", LIVEKIT_URL)
38
+ html_content = html_content.replace("__LIVEKIT_TOKEN__", f'"{token}"')
39
+ return HTMLResponse(content=html_content)
40
+
41
+ class InputData(BaseModel):
42
+ identity: str
43
+ conf_threshold: float = Field(ge=0, le=1)
44
+
45
+ @app.post("/input_hook")
46
+ async def _(data: InputData):
47
+ print(f"Received input for {data.identity} with threshold {data.conf_threshold}")
48
+ return {"status": "ok"}
49
+
50
+ if __name__ == "__main__":
51
+ import uvicorn
52
+ uvicorn.run(app, host="0.0.0.0", port=7860)