fomext commited on
Commit
e825f8c
·
verified ·
1 Parent(s): c1e1248

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -9
app.py CHANGED
@@ -19,30 +19,37 @@ os.makedirs(UPLOAD_DIR, exist_ok=True)
19
  os.makedirs(OUTPUT_DIR, exist_ok=True)
20
 
21
  # ===============================
22
- # Device / dtype (CRITICAL)
23
  # ===============================
24
 
25
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
26
  DTYPE = torch.float16 if DEVICE == "cuda" else torch.float32
27
 
28
  # ===============================
29
- # Job store
30
  # ===============================
31
 
32
  jobs = {}
33
 
34
  # ===============================
35
- # Load models (safe on startup)
36
  # ===============================
37
 
38
  whisper_model = whisper.load_model("base")
39
  yolo = YOLO("yolov8n.pt")
40
 
41
  svd = StableVideoDiffusionPipeline.from_pretrained(
42
- "stabilityai/stable-video-diffusion-img2vid",
43
- dtype=DTYPE
44
  )
45
- svd.to(DEVICE)
 
 
 
 
 
 
 
 
46
 
47
  # ===============================
48
  # Endpoints
@@ -99,8 +106,16 @@ async def smart_crop(
99
  ret, frame = cap.read()
100
  cap.release()
101
 
 
 
 
102
  results = yolo(frame)
103
- box = results[0].boxes.xyxy[0].cpu().numpy()
 
 
 
 
 
104
 
105
  return {
106
  "crop_box": box.tolist(),
@@ -113,12 +128,14 @@ async def smart_crop(
113
 
114
  def run_edit_job(job_id: str, video_path: str, frame_path: str):
115
  try:
 
116
  subprocess.run(
117
  [
118
  "ffmpeg", "-y",
119
  "-i", video_path,
120
  "-vf", "scale=512:512:force_original_aspect_ratio=decrease",
121
  "-frames:v", "1",
 
122
  frame_path
123
  ],
124
  check=True
@@ -129,8 +146,8 @@ def run_edit_job(job_id: str, video_path: str, frame_path: str):
129
  with torch.no_grad():
130
  output = svd(
131
  image=img,
132
- num_frames=16,
133
- decode_chunk_size=8
134
  )
135
 
136
  jobs[job_id]["status"] = "done"
 
19
  os.makedirs(OUTPUT_DIR, exist_ok=True)
20
 
21
  # ===============================
22
+ # Device / dtype
23
  # ===============================
24
 
25
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
26
  DTYPE = torch.float16 if DEVICE == "cuda" else torch.float32
27
 
28
  # ===============================
29
+ # In-memory job store
30
  # ===============================
31
 
32
  jobs = {}
33
 
34
  # ===============================
35
+ # Load models (startup-safe)
36
  # ===============================
37
 
38
  whisper_model = whisper.load_model("base")
39
  yolo = YOLO("yolov8n.pt")
40
 
41
  svd = StableVideoDiffusionPipeline.from_pretrained(
42
+ "stabilityai/stable-video-diffusion-img2vid"
 
43
  )
44
+ svd.to(device=DEVICE, dtype=DTYPE)
45
+
46
+ # ===============================
47
+ # Health check
48
+ # ===============================
49
+
50
+ @app.get("/")
51
+ def root():
52
+ return {"status": "ok"}
53
 
54
  # ===============================
55
  # Endpoints
 
106
  ret, frame = cap.read()
107
  cap.release()
108
 
109
+ if not ret:
110
+ return {"error": "Failed to read video frame"}
111
+
112
  results = yolo(frame)
113
+ boxes = results[0].boxes
114
+
115
+ if boxes is None or len(boxes) == 0:
116
+ return {"error": "No subject detected"}
117
+
118
+ box = boxes.xyxy[0].cpu().numpy()
119
 
120
  return {
121
  "crop_box": box.tolist(),
 
128
 
129
  def run_edit_job(job_id: str, video_path: str, frame_path: str):
130
  try:
131
+ # Extract single frame safely
132
  subprocess.run(
133
  [
134
  "ffmpeg", "-y",
135
  "-i", video_path,
136
  "-vf", "scale=512:512:force_original_aspect_ratio=decrease",
137
  "-frames:v", "1",
138
+ "-update", "1",
139
  frame_path
140
  ],
141
  check=True
 
146
  with torch.no_grad():
147
  output = svd(
148
  image=img,
149
+ num_frames=8, # CPU-safe default
150
+ decode_chunk_size=4
151
  )
152
 
153
  jobs[job_id]["status"] = "done"