Prathamesh1420's picture
Upload app.py
018cf2d verified
import json
import os
from pathlib import Path
import cv2
import gradio as gr
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from huggingface_hub import hf_hub_download
from pydantic import BaseModel, Field
from livekit_token import generate_token
try:
from demo.object_detection.inference import YOLOv10
except (ImportError, ModuleNotFoundError):
from inference import YOLOv10
cur_dir = Path(__file__).parent
model_file = hf_hub_download(repo_id="onnx-community/yolov10n", filename="onnx/model.onnx")
model = YOLOv10(model_file)
def detection(image, conf_threshold=0.3):
image = cv2.resize(image, (model.input_width, model.input_height))
new_image = model.detect_objects(image, conf_threshold)
return cv2.resize(new_image, (500, 500))
app = FastAPI()
LIVEKIT_URL = os.getenv("LIVEKIT_URL")
LIVEKIT_API_KEY = os.getenv("LIVEKIT_API_KEY")
LIVEKIT_API_SECRET = os.getenv("LIVEKIT_API_SECRET")
@app.get("/")
async def _():
token = generate_token(LIVEKIT_API_KEY, LIVEKIT_API_SECRET, identity="user123")
html_content = open(cur_dir / "index.html").read()
html_content = html_content.replace("__LIVEKIT_URL__", LIVEKIT_URL)
html_content = html_content.replace("__LIVEKIT_TOKEN__", f'"{token}"')
return HTMLResponse(content=html_content)
class InputData(BaseModel):
identity: str
conf_threshold: float = Field(ge=0, le=1)
@app.post("/input_hook")
async def _(data: InputData):
print(f"Received input for {data.identity} with threshold {data.conf_threshold}")
return {"status": "ok"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)