Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,9 +12,27 @@ import torchvision.transforms.functional as F
|
|
12 |
from decord import VideoReader
|
13 |
from nncore.engine import load_checkpoint
|
14 |
from nncore.nn import build_model
|
|
|
15 |
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
|
|
18 |
app.add_middleware(
|
19 |
CORSMiddleware,
|
20 |
allow_origins=["http://localhost:3000"],
|
@@ -23,11 +41,10 @@ app.add_middleware(
|
|
23 |
allow_headers=["*"],
|
24 |
)
|
25 |
|
|
|
26 |
CONFIG = 'configs/qvhighlights/r2_tuning_qvhighlights.py'
|
27 |
WEIGHT = 'r2_tuning_qvhighlights-ed516355.pth'
|
28 |
|
29 |
-
model, cfg = None, None
|
30 |
-
|
31 |
def convert_time(seconds):
|
32 |
minutes, seconds = divmod(round(max(seconds, 0)), 60)
|
33 |
return f'{minutes:02d}:{seconds:02d}'
|
@@ -73,12 +90,6 @@ def process_video(video_path: str, query: str, model, cfg) -> dict:
|
|
73 |
hd = [{"x": i * 2, "y": y} for i, y in enumerate(hd)]
|
74 |
return {"moment_retrieval": mr, "highlight_detection": hd}
|
75 |
|
76 |
-
@app.on_event("startup")
|
77 |
-
async def startup_event():
|
78 |
-
global model, cfg
|
79 |
-
model, cfg = init_model(CONFIG, WEIGHT)
|
80 |
-
print("Model loaded successfully.")
|
81 |
-
|
82 |
@app.post("/predict")
|
83 |
async def predict(video: UploadFile = File(...), query: str = Form(...)):
|
84 |
try:
|
|
|
12 |
from decord import VideoReader
|
13 |
from nncore.engine import load_checkpoint
|
14 |
from nncore.nn import build_model
|
15 |
+
from contextlib import asynccontextmanager
|
16 |
|
17 |
+
# Global variables for model and config
|
18 |
+
model, cfg = None, None
|
19 |
+
|
20 |
+
# Lifespan handler to manage startup and shutdown
|
21 |
+
@asynccontextmanager
|
22 |
+
async def lifespan(app: FastAPI):
|
23 |
+
# Startup: Load the model and config
|
24 |
+
global model, cfg
|
25 |
+
print("Loading model on startup...")
|
26 |
+
model, cfg = init_model(CONFIG, WEIGHT)
|
27 |
+
print("Model loaded successfully.")
|
28 |
+
yield # Application runs here
|
29 |
+
# Shutdown: Clean up (if needed)
|
30 |
+
print("Shutting down...")
|
31 |
+
|
32 |
+
# Initialize FastAPI app with lifespan
|
33 |
+
app = FastAPI(title="R2-Tuning API", lifespan=lifespan)
|
34 |
|
35 |
+
# Enable CORS for React app
|
36 |
app.add_middleware(
|
37 |
CORSMiddleware,
|
38 |
allow_origins=["http://localhost:3000"],
|
|
|
41 |
allow_headers=["*"],
|
42 |
)
|
43 |
|
44 |
+
# Configuration
|
45 |
CONFIG = 'configs/qvhighlights/r2_tuning_qvhighlights.py'
|
46 |
WEIGHT = 'r2_tuning_qvhighlights-ed516355.pth'
|
47 |
|
|
|
|
|
48 |
def convert_time(seconds):
|
49 |
minutes, seconds = divmod(round(max(seconds, 0)), 60)
|
50 |
return f'{minutes:02d}:{seconds:02d}'
|
|
|
90 |
hd = [{"x": i * 2, "y": y} for i, y in enumerate(hd)]
|
91 |
return {"moment_retrieval": mr, "highlight_detection": hd}
|
92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
@app.post("/predict")
|
94 |
async def predict(video: UploadFile = File(...), query: str = Form(...)):
|
95 |
try:
|