Spaces:
Sleeping
Sleeping
File size: 3,459 Bytes
5e55742 e19b795 bed8d9e 981025a e19b795 5e55742 c303abd b4627df 22a85f1 9a7d942 c5f1aa5 5e55742 e19b795 0363778 e19b795 5e55742 3d237cb efc36ce e19b795 efc36ce 605dd3b efc36ce b0aa005 efc36ce d6b54f6 83180e3 efc36ce e19b795 efc36ce b0aa005 efc36ce 83180e3 a3a6b45 605dd3b 75be613 efc36ce 22a85f1 e19b795 5e55742 e19b795 b4627df 22a85f1 c303abd efc36ce 5e55742 efc36ce b7549df c303abd 3d237cb 84dbb52 c303abd e19b795 efc36ce 9a7d942 efc36ce e19b795 efc36ce e19b795 efc36ce e19b795 efc36ce | 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 | from ai.utils.hugging_face import hf_fetch_model
from ai.depth.depth_anything import DepthAnything
from ai.detectors.yolo_detector import YOLO_Detector
from config.settings import AppConfig
from api.routers.metrics import metrics_asgi_app
from api.routers import camera_stream
from api.routers import dashboard_stream
from api.routers import health
from infra.logger_structlog import StructLogger
from contextlib import asynccontextmanager
import mlflow
import torch
import redis.asyncio as aioredis
from fastapi.middleware.trustedhost import TrustedHostMiddleware
import dagshub
from fastapi import FastAPI
@asynccontextmanager
async def lifespan(app: FastAPI):
"""
This is on_event("startup") new alternative, Make sure you load models here.
"""
settings = AppConfig()
logger = StructLogger(settings=settings)
# Using this way to can store data. it is acts as a dict which holds instances
app.state.logger = logger
app.state.settings = settings
app.state.mlflow_run_id = parent_run.info.run_id
logger.info("Starting Server.... ")
# asyncio.create_task(log_system_metrics(logger, logger_interval_sec=settings.intervals.system_metrics_seconds))
logger.info("Downloading Models..")
detection_model_path = hf_fetch_model(
repo_id="Ultralytics/YOLO26",
filename=settings.yolo.model_name,
)
app.state.detection_model = YOLO_Detector(detection_model_path)
depth_model_path = hf_fetch_model(
repo_id="depth-anything/Depth-Anything-V2-Small",
filename=settings.depth.model_name,
)
app.state.depth_model = DepthAnything(
encoder=settings.depth.encoder,
depth_model_path=depth_model_path,
DEVICE=settings.depth.device,
)
safety_detection_path = hf_fetch_model(
repo_id="e1250/safety_detection",
filename=settings.security_detector.model_name,
)
app.state.safety_detection_model = YOLO_Detector(safety_detection_path)
logger.info("Connecting to Redis Server...")
app.state.redis = aioredis.from_url(settings.redis_url, decode_responses=True)
# Checking connection to redis - TODO add to health check
try:
await app.state.redis.ping()
logger.info("Redis connected successfully...")
except Exception as e:
logger.error(f"Failed to connect to Redis: {e}")
raise e
yield
# Here You remove connections and release gpu here..
logger.warn("Shutting down the server....")
torch.cuda.empty_cache()
await app.state.redis.close()
# MLFlow setup
dagshub.init(repo_owner="eslam760000", repo_name="p-tracking_system", mlflow=True)
# mlflow.set_tracking_uri("sqlite:///config/logs/mlflow.db")
mlflow.set_experiment("realtime-detection-system")
parent_run = mlflow.start_run(run_name="server_session")
mlflow.enable_system_metrics_logging()
app = FastAPI(
title="Tracking System Backend",
description="real-time frame processing API",
version="0.1.0",
lifespan=lifespan,
)
app.add_middleware(TrustedHostMiddleware, allowed_hosts=["*"])
# Routes
app.mount(
"/metrics", metrics_asgi_app
) # Starting Prometheus server attached to my server.
app.include_router(camera_stream.router, prefix="/detectors")
app.include_router(dashboard_stream.router, prefix="/dashboard")
app.include_router(health.router, prefix="/health")
@app.get("/")
async def root():
return {"status": "Real-Time tracker backend is running..."}
|