hysts HF Staff commited on
Commit
db2263e
·
1 Parent(s): 129cbd1

Improve ZeroGPU compatibility

Browse files
Files changed (1) hide show
  1. app.py +37 -14
app.py CHANGED
@@ -18,23 +18,46 @@ import gradio as gr
18
 
19
  device = "cuda" if torch.cuda.is_available() else "cpu"
20
  yaml_args = OmegaConf.load(f"{REPO_ROOT}/ckpt/model_config.yaml")
21
- pipeline = None
22
 
 
 
 
23
 
24
- @spaces.GPU
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  def fn(input_video):
26
- global pipeline, yaml_args, device
27
- if pipeline is None:
28
- if not os.path.exists(f"{REPO_ROOT}/ckpt/model.safetensors"):
29
- subprocess.run(["bash", f"{REPO_ROOT}/infer_bash/download_ckpt.sh"], check=True)
30
- pipeline = load_model(f"{REPO_ROOT}/ckpt", yaml_args)
31
-
32
- input_video_basename = os.path.basename(input_video)
33
- input_tensor, orig_size, origin_fps = load_video_data(Namespace(
34
- input_video=input_video,
35
- height=480,
36
- width=640,
37
- ))
38
  depth = predict_depth(pipeline, input_tensor, orig_size, Namespace(
39
  window_size=81,
40
  overlap=21
 
18
 
19
  device = "cuda" if torch.cuda.is_available() else "cpu"
20
  yaml_args = OmegaConf.load(f"{REPO_ROOT}/ckpt/model_config.yaml")
 
21
 
22
+ if not os.path.exists(f"{REPO_ROOT}/ckpt/model.safetensors"):
23
+ subprocess.run(["bash", f"{REPO_ROOT}/infer_bash/download_ckpt.sh"], check=True)
24
+ pipeline = load_model(f"{REPO_ROOT}/ckpt", yaml_args)
25
 
26
+
27
+ MAX_FRAMES = 300
28
+
29
+
30
+ def read_video_limited(video_path, max_frames):
31
+ """Read up to max_frames from a video without loading the entire file."""
32
+ cap = cv2.VideoCapture(video_path)
33
+ if not cap.isOpened():
34
+ raise gr.Error(f"Cannot open video: {video_path}")
35
+ fps = cap.get(cv2.CAP_PROP_FPS)
36
+ total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
37
+ if total_frames > max_frames:
38
+ gr.Warning(
39
+ f"Video has {total_frames} frames, processing only the first {max_frames}."
40
+ )
41
+ frames = []
42
+ while len(frames) < max_frames:
43
+ ret, frame = cap.read()
44
+ if not ret:
45
+ break
46
+ frames.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
47
+ cap.release()
48
+ video_np = np.stack(frames)
49
+ video_tensor = (
50
+ torch.from_numpy(video_np).permute(0, 3, 1, 2).float() / 255.0
51
+ )
52
+ return video_tensor.unsqueeze(0), fps
53
+
54
+
55
+ @spaces.GPU(duration=90)
56
  def fn(input_video):
57
+ input_tensor, origin_fps = read_video_limited(input_video, MAX_FRAMES)
58
+ input_tensor, orig_size = resize_for_training_scale(
59
+ input_tensor, 480, 640
60
+ )
 
 
 
 
 
 
 
 
61
  depth = predict_depth(pipeline, input_tensor, orig_size, Namespace(
62
  window_size=81,
63
  overlap=21