multimodalart HF Staff commited on
Commit
d373f5e
·
verified ·
1 Parent(s): 01fe360

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +40 -18
  2. requirements.txt +1 -1
app.py CHANGED
@@ -1,6 +1,12 @@
1
  import os
2
 
3
  os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")
 
 
 
 
 
 
4
 
5
  import spaces # noqa: E402 (must precede any CUDA-touching import)
6
 
@@ -25,6 +31,7 @@ from pipeline.stream_diffvsr_pipeline import ( # noqa: E402
25
  from temporal_autoencoder.autoencoder_tiny import TemporalAutoencoderTiny # noqa: E402
26
 
27
  torch.backends.cuda.matmul.allow_tf32 = True
 
28
 
29
  MODEL_ID = "Jamichsu/Stream-DiffVSR"
30
  SCALE = 4 # the model is a x4 super-resolver
@@ -42,8 +49,12 @@ pipe = StreamDiffVSRPipeline.from_pretrained(
42
  unet=unet,
43
  vae=vae,
44
  scheduler=scheduler,
 
 
 
45
  custom_pipeline=None,
46
  )
 
47
  pipe = pipe.to("cuda")
48
 
49
  of_model = raft_large(weights=Raft_Large_Weights.DEFAULT).eval().to("cuda")
@@ -106,28 +117,39 @@ def _write_video(frames: List[Image.Image], fps: float, suffix: str) -> str:
106
  return path
107
 
108
 
109
- def _estimate_duration(
110
- video=None,
111
- num_frames: int = 24,
112
- max_input_size: int = 320,
113
- num_inference_steps: int = 4,
114
- seed: int = 42,
115
- *args,
116
- **kwargs,
117
- ):
 
 
 
 
 
 
118
  try:
119
- pixels = (int(max_input_size) ** 2) / (320.0 * 320.0)
120
- per_frame = 0.55 * pixels * (int(num_inference_steps) / 4.0)
121
- return int(min(240, 40 + per_frame * int(num_frames)))
122
- except Exception:
123
- return 180
 
 
 
 
 
124
 
125
 
126
  # ----------------------------------------------------------------- inference
127
  @spaces.GPU(duration=_estimate_duration)
128
  def upscale_video(
129
  video: str,
130
- num_frames: int = 24,
131
  max_input_size: int = 320,
132
  num_inference_steps: int = 4,
133
  seed: int = 42,
@@ -228,14 +250,14 @@ and a temporal-aware decoder, so every frame is restored from **past frames only
228
  num_frames = gr.Slider(
229
  label="Frames to process",
230
  minimum=4,
231
- maximum=48,
232
  step=1,
233
- value=24,
234
  )
235
  max_input_size = gr.Slider(
236
  label="Max input size (long side, px)",
237
  minimum=128,
238
- maximum=448,
239
  step=32,
240
  value=320,
241
  info="The input is downscaled to this before the 4× upscale.",
 
1
  import os
2
 
3
  os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")
4
+ # `~/.cache/torch` is not writable in the Space image and RAFT weights are fetched
5
+ # through torch.hub, so point TORCH_HOME (and matplotlib's config dir) somewhere writable.
6
+ os.environ.setdefault("TORCH_HOME", "/tmp/torch_home")
7
+ os.environ.setdefault("MPLCONFIGDIR", "/tmp/mplconfig")
8
+ os.makedirs(os.path.join(os.environ["TORCH_HOME"], "hub", "checkpoints"), exist_ok=True)
9
+ os.makedirs(os.environ["MPLCONFIGDIR"], exist_ok=True)
10
 
11
  import spaces # noqa: E402 (must precede any CUDA-touching import)
12
 
 
31
  from temporal_autoencoder.autoencoder_tiny import TemporalAutoencoderTiny # noqa: E402
32
 
33
  torch.backends.cuda.matmul.allow_tf32 = True
34
+ torch.backends.cudnn.allow_tf32 = True
35
 
36
  MODEL_ID = "Jamichsu/Stream-DiffVSR"
37
  SCALE = 4 # the model is a x4 super-resolver
 
49
  unet=unet,
50
  vae=vae,
51
  scheduler=scheduler,
52
+ safety_checker=None,
53
+ feature_extractor=None,
54
+ requires_safety_checker=False,
55
  custom_pipeline=None,
56
  )
57
+ pipe.set_progress_bar_config(desc="Stream-DiffVSR")
58
  pipe = pipe.to("cuda")
59
 
60
  of_model = raft_large(weights=Raft_Large_Weights.DEFAULT).eval().to("cuda")
 
117
  return path
118
 
119
 
120
+ def _estimate_duration(*args, **kwargs) -> int:
121
+ """ZeroGPU duration estimator, fitted to runtime measured on this Space.
122
+
123
+ Measured at 312x176 LR: ~0.65 s/frame at 4 steps and ~1.15 s/frame at 8 steps,
124
+ i.e. ~0.15 s/frame fixed + ~0.125 s/frame/step, scaling with the LR pixel count.
125
+ """
126
+ values = list(args)
127
+
128
+ def _pick(name, index, default):
129
+ if name in kwargs and kwargs[name] is not None:
130
+ return kwargs[name]
131
+ if len(values) > index and values[index] is not None:
132
+ return values[index]
133
+ return default
134
+
135
  try:
136
+ num_frames = int(_pick("num_frames", 1, 30))
137
+ max_input_size = int(_pick("max_input_size", 2, 320))
138
+ steps = int(_pick("num_inference_steps", 3, 4))
139
+ except (TypeError, ValueError):
140
+ num_frames, max_input_size, steps = 30, 320, 4
141
+
142
+ # assume a 16:9 clip for the pixel-count scaling against the 312x176 reference
143
+ px = (max_input_size * max_input_size * 9.0 / 16.0) / (312.0 * 176.0)
144
+ seconds = 12.0 + num_frames * (0.15 + 0.125 * steps) * px * 1.25
145
+ return int(min(115, max(20, round(seconds))))
146
 
147
 
148
  # ----------------------------------------------------------------- inference
149
  @spaces.GPU(duration=_estimate_duration)
150
  def upscale_video(
151
  video: str,
152
+ num_frames: int = 30,
153
  max_input_size: int = 320,
154
  num_inference_steps: int = 4,
155
  seed: int = 42,
 
250
  num_frames = gr.Slider(
251
  label="Frames to process",
252
  minimum=4,
253
+ maximum=40,
254
  step=1,
255
+ value=30,
256
  )
257
  max_input_size = gr.Slider(
258
  label="Max input size (long side, px)",
259
  minimum=128,
260
+ maximum=384,
261
  step=32,
262
  value=320,
263
  info="The input is downscaled to this before the 4× upscale.",
requirements.txt CHANGED
@@ -1,5 +1,5 @@
1
  torchvision
2
- diffusers
3
  transformers
4
  accelerate
5
  safetensors
 
1
  torchvision
2
+ diffusers==0.40.0
3
  transformers
4
  accelerate
5
  safetensors