PrashanthB461 commited on
Commit
3bdfb30
·
verified ·
1 Parent(s): b681a83

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -13
app.py CHANGED
@@ -1,4 +1,5 @@
1
  from fastapi import FastAPI, File, UploadFile, HTTPException
 
2
  import torch
3
  from ultralytics import YOLO
4
  import cv2
@@ -21,8 +22,23 @@ SALESFORCE_TOKEN = os.getenv("SALESFORCE_TOKEN", "your_salesforce_access_token")
21
  HUGGINGFACE_API_URL = os.getenv("HUGGINGFACE_API_URL", "https://api-inference.huggingface.co/models/PrashanthB461/SafetyViolationAI1")
22
  HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN", "your_huggingface_api_token")
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  # --- Initialize FastAPI app ---
25
- app = FastAPI()
26
 
27
  # --- YOLOv8 Model ---
28
  class YOLOv8Model:
@@ -170,15 +186,4 @@ async def upload_image(file: UploadFile = File(...)):
170
 
171
  @app.get("/health_check/")
172
  async def health_check():
173
- return {"status": "Running smoothly"}
174
-
175
- @app.on_event("startup")
176
- async def startup_event():
177
- logger.info("FastAPI application starting up")
178
- # Initialize any resources (e.g., check RTSP connection, model load)
179
- try:
180
- model = YOLOv8Model()
181
- logger.info("Startup: YOLOv8 model initialized")
182
- except Exception as e:
183
- logger.error(f"Startup error: {e}")
184
- raise
 
1
  from fastapi import FastAPI, File, UploadFile, HTTPException
2
+ from contextlib import asynccontextmanager
3
  import torch
4
  from ultralytics import YOLO
5
  import cv2
 
22
  HUGGINGFACE_API_URL = os.getenv("HUGGINGFACE_API_URL", "https://api-inference.huggingface.co/models/PrashanthB461/SafetyViolationAI1")
23
  HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN", "your_huggingface_api_token")
24
 
25
+ # --- Lifespan Handler ---
26
+ @asynccontextmanager
27
+ async def lifespan(app: FastAPI):
28
+ # Startup logic
29
+ logger.info("FastAPI application starting up")
30
+ try:
31
+ model = YOLOv8Model()
32
+ logger.info("Startup: YOLOv8 model initialized")
33
+ except Exception as e:
34
+ logger.error(f"Startup error: {e}")
35
+ raise
36
+ yield
37
+ # Shutdown logic (if needed)
38
+ logger.info("FastAPI application shutting down")
39
+
40
  # --- Initialize FastAPI app ---
41
+ app = FastAPI(lifespan=lifespan)
42
 
43
  # --- YOLOv8 Model ---
44
  class YOLOv8Model:
 
186
 
187
  @app.get("/health_check/")
188
  async def health_check():
189
+ return {"status": "Running smoothly"}