r3gm commited on
Commit
4164ab4
Β·
verified Β·
1 Parent(s): 4053aa4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -125
app.py CHANGED
@@ -15,97 +15,66 @@ subprocess.run([
15
  # Unzip the downloaded file
16
  subprocess.run([
17
  "unzip",
 
18
  "RIFE_trained_model_v4.13.2.zip"
19
  ], check=True)
20
 
21
  # The name of your script
22
  SCRIPT_NAME = "inference_video.py"
23
 
24
- @spaces.GPU()
25
  def run_rife(
26
  input_video,
27
- model_dir,
28
- multi,
29
- exp,
30
- fps,
31
- scale,
32
- uhd,
33
- fp16,
34
- skip,
35
- montage,
36
- png_mode,
37
- ext
38
  ):
39
- """
40
- Constructs the command line arguments based on Gradio inputs
41
- and runs the inference_video.py script via subprocess.
42
- """
43
-
44
  if input_video is None:
45
  raise gr.Error("Please upload a video first.")
46
 
47
- # 1. Define Output Filename
48
- output_name = f"output_{multi}X.{ext}"
49
 
50
- # 2. Build the Command
51
- cmd = ["python3", SCRIPT_NAME]
52
 
53
- # --video
54
  cmd.extend(["--video", input_video])
 
 
55
 
56
- # --output
57
- cmd.extend(["--output", output_name])
58
-
59
- # --multi (Multiplier)
60
- cmd.extend(["--multi", str(int(multi))])
61
-
62
- # --exp
63
- # Only add exp if it is not default, or if specific logic requires it.
64
- # Usually multi overrides exp in RIFE logic, but we pass it if set.
65
- if exp != 1:
66
- cmd.extend(["--exp", str(int(exp))])
67
-
68
- # --fps (Target FPS)
69
- if fps > 0:
70
- cmd.extend(["--fps", str(int(fps))])
71
 
72
- # --scale (Resolution scale)
73
- # Check against float 1.0
74
- if scale != 1.0:
75
- cmd.extend(["--scale", str(scale)])
76
 
77
- # --ext (Extension)
78
  cmd.extend(["--ext", ext])
 
 
79
 
80
- # --model (Model directory)
81
- if model_dir and model_dir.strip() != "":
82
- cmd.extend(["--model", model_dir])
83
-
84
- # --- Boolean Flags ---
85
-
86
- if uhd:
87
  cmd.append("--UHD")
88
 
89
- if fp16:
90
- cmd.append("--fp16")
91
-
92
- if skip:
93
  cmd.append("--skip")
94
 
95
- if montage:
96
  cmd.append("--montage")
97
-
98
- if png_mode:
99
- cmd.append("--png")
100
 
101
  print(f"Executing command: {' '.join(cmd)}")
102
 
103
- # 3. Run the Subprocess
104
  try:
105
- # We use a large timeout because video processing takes time
106
  process = subprocess.run(cmd, capture_output=True, text=True)
107
 
108
- # Log stdout/stderr
109
  if process.stdout:
110
  print("STDOUT:", process.stdout)
111
  if process.stderr:
@@ -114,15 +83,14 @@ def run_rife(
114
  if process.returncode != 0:
115
  raise gr.Error(f"Inference failed. Error: {process.stderr}")
116
 
117
- # 4. Return Result
118
- if png_mode:
119
- gr.Info("Processing complete. Output is a folder of PNGs (Video preview unavailable for PNG mode).")
120
- return None
121
 
122
- if os.path.exists(output_name):
123
- return output_name
124
  else:
125
- raise gr.Error("Output file was not found. Check console for details.")
 
 
126
 
127
  except Exception as e:
128
  raise gr.Error(f"An error occurred: {str(e)}")
@@ -130,111 +98,82 @@ def run_rife(
130
 
131
  # --- Gradio UI Layout ---
132
 
133
- with gr.Blocks(title="RIFE Video Interpolation") as app:
134
- gr.Markdown("# πŸš€ RIFE: Real-Time Intermediate Flow Estimation")
135
- gr.Markdown("Upload a video to increase its frame rate (smoothness) using AI.")
 
136
 
137
  with gr.Row():
138
  # --- Left Column: Inputs & Settings ---
139
  with gr.Column(scale=1):
140
- input_vid = gr.Video(label="Input Video", sources=["upload"])
141
 
142
  with gr.Group():
143
- gr.Markdown("### 🎯 Core Parameters")
144
 
145
- with gr.Row():
146
- multi_param = gr.Dropdown(
147
- choices=["2", "4", "8", "16", "32"],
148
- value="2",
149
- label="Interpolation Multiplier (--multi)",
150
- info="How many times to multiply the frames. 2X doubles the FPS (e.g., 30fps -> 60fps). 4X quadruples it."
151
- )
152
- ext_param = gr.Dropdown(
153
- choices=["mp4", "avi", "mov", "mkv"],
154
- value="mp4",
155
- label="Output Format (--ext)",
156
- info="The file extension for the generated video."
157
- )
158
-
159
- model_param = gr.Textbox(
160
- value="train_log",
161
- label="Model Directory (--model)",
162
- placeholder="train_log",
163
- info="Path to the folder containing the trained model files (e.g., 'train_log' or 'rife-v4.6')."
164
  )
165
 
166
- with gr.Accordion("βš™οΈ Advanced Settings", open=False):
167
- gr.Markdown("Fine-tune the inference process.")
168
 
169
  with gr.Row():
170
  scale_param = gr.Slider(
171
  minimum=0.1, maximum=1.0, value=1.0, step=0.1,
172
- label="Input Scale (--scale)",
173
- info="1.0 = Original resolution. Set to 0.5 to reduce memory usage for 4K video inputs."
174
  )
175
  fps_param = gr.Number(
176
  value=0,
177
- label="Force Target FPS (--fps)",
178
- info="0 = Auto-calculate based on multiplier. Enter a number (e.g., 60) to force a specific output frame rate."
179
  )
180
  exp_param = gr.Number(
181
  value=1,
182
- label="Exponent Power (--exp)",
183
- info="Alternative to Multiplier. Sets multiplier to 2^exp. (Usually left at 1 if Multiplier is set)."
184
  )
185
 
186
  with gr.Row():
187
  uhd_chk = gr.Checkbox(
188
- label="UHD Mode (--UHD)",
189
  value=False,
190
- info="Optimized for 4K video. Equivalent to setting scale=0.5 manually."
191
- )
192
- fp16_chk = gr.Checkbox(
193
- label="FP16 Mode (--fp16)",
194
- value=True,
195
- info="Uses half-precision floating point. Faster and uses less VRAM with minimal quality loss."
196
  )
197
-
198
- with gr.Row():
199
  skip_chk = gr.Checkbox(
200
- label="Skip Static Frames (--skip)",
201
  value=False,
202
- info="If the video has frames that don't move, skip processing them to save time."
203
  )
204
  montage_chk = gr.Checkbox(
205
- label="Montage (--montage)",
206
- value=False,
207
- info="Creates a video with the Original on the Left and Interpolated on the Right for comparison."
208
- )
209
- png_chk = gr.Checkbox(
210
- label="Output as PNGs (--png)",
211
  value=False,
212
- info="Outputs a sequence of images instead of a video file. (Video Preview will be disabled)."
213
  )
214
 
215
- btn_run = gr.Button("✨ Start Interpolation", variant="primary", size="lg")
216
 
217
  # --- Right Column: Output ---
218
  with gr.Column(scale=1):
219
- output_vid = gr.Video(label="Interpolated Result")
220
- gr.Markdown("**Note:** Processing time depends on video length, resolution, and your GPU speed.")
221
 
222
  # --- Bind Logic ---
223
  btn_run.click(
224
  fn=run_rife,
225
  inputs=[
226
  input_vid,
227
- model_param,
228
  multi_param,
229
  exp_param,
230
  fps_param,
231
  scale_param,
232
  uhd_chk,
233
- fp16_chk,
234
  skip_chk,
235
- montage_chk,
236
- png_chk,
237
- ext_param
238
  ],
239
  outputs=output_vid
240
  )
 
15
  # Unzip the downloaded file
16
  subprocess.run([
17
  "unzip",
18
+ "-o",
19
  "RIFE_trained_model_v4.13.2.zip"
20
  ], check=True)
21
 
22
  # The name of your script
23
  SCRIPT_NAME = "inference_video.py"
24
 
25
+ @spaces.GPU(duration=120)
26
  def run_rife(
27
  input_video,
28
+ frame_multiplier,
29
+ time_exponent,
30
+ fixed_fps,
31
+ video_scale,
32
+ enable_uhd_mode,
33
+ remove_duplicate_frames,
34
+ create_montage
 
 
 
 
35
  ):
 
 
 
 
 
36
  if input_video is None:
37
  raise gr.Error("Please upload a video first.")
38
 
39
+ ext = "mp4"
40
+ model_dir = "train_log"
41
 
42
+ video_path_wo_ext = os.path.splitext(os.path.basename(input_video))[0]
43
+ output_base_name = "{}_{}X_{}fps.{}".format(video_path_wo_ext, int(frame_multiplier), int(fixed_fps), ext)
44
 
45
+ cmd = ["python3", SCRIPT_NAME]
46
  cmd.extend(["--video", input_video])
47
+ cmd.extend(["--output", output_base_name])
48
+ cmd.extend(["--multi", str(int(frame_multiplier))])
49
 
50
+ if time_exponent != 1:
51
+ cmd.extend(["--exp", str(int(time_exponent))])
52
+
53
+ if fixed_fps > 0:
54
+ cmd.extend(["--fps", str(int(fixed_fps))])
55
+ gr.Warning("Will not merge audio because using fps flag!")
 
 
 
 
 
 
 
 
 
56
 
57
+ if video_scale != 1.0:
58
+ cmd.extend(["--scale", str(video_scale)])
 
 
59
 
 
60
  cmd.extend(["--ext", ext])
61
+ cmd.extend(["--model", model_dir])
62
+ cmd.append("--fp16")
63
 
64
+ if enable_uhd_mode:
 
 
 
 
 
 
65
  cmd.append("--UHD")
66
 
67
+ if remove_duplicate_frames:
 
 
 
68
  cmd.append("--skip")
69
 
70
+ if create_montage:
71
  cmd.append("--montage")
 
 
 
72
 
73
  print(f"Executing command: {' '.join(cmd)}")
74
 
 
75
  try:
 
76
  process = subprocess.run(cmd, capture_output=True, text=True)
77
 
 
78
  if process.stdout:
79
  print("STDOUT:", process.stdout)
80
  if process.stderr:
 
83
  if process.returncode != 0:
84
  raise gr.Error(f"Inference failed. Error: {process.stderr}")
85
 
86
+ final_output_file = f"{output_base_name}.{ext}"
 
 
 
87
 
88
+ if os.path.exists(final_output_file):
89
+ return final_output_file
90
  else:
91
+ if os.path.exists(output_base_name):
92
+ return output_base_name
93
+ raise gr.Error(f"Output file not found. Expected: {final_output_file}")
94
 
95
  except Exception as e:
96
  raise gr.Error(f"An error occurred: {str(e)}")
 
98
 
99
  # --- Gradio UI Layout ---
100
 
101
+ with gr.Blocks(title="Professional FPS Booster") as app:
102
+ gr.Markdown("# ⚑ RIFE: High-Performance FPS Booster")
103
+ gr.Markdown("Maximize video fluidity and frame rate using deep flow estimation.")
104
+ gr.Markdown("⚠️ **Tip:** Optimized for short videos. Avoid long videos to prevent 'GPU task aborted'.")
105
 
106
  with gr.Row():
107
  # --- Left Column: Inputs & Settings ---
108
  with gr.Column(scale=1):
109
+ input_vid = gr.Video(label="🎬 Input Source Video", sources=["upload"])
110
 
111
  with gr.Group():
112
+ gr.Markdown("### πŸš€ Performance Settings")
113
 
114
+ multi_param = gr.Dropdown(
115
+ choices=["2", "3", "4", "5", "6"],
116
+ value="2",
117
+ label="⚑ Frame Multiplier",
118
+ info="2X = Double FPS (e.g. 30 -> 60). Higher multipliers create more intermediate frames."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  )
120
 
121
+ with gr.Accordion("πŸ› οΈ Advanced Configuration", open=False):
122
+ gr.Markdown("Control rendering parameters.")
123
 
124
  with gr.Row():
125
  scale_param = gr.Slider(
126
  minimum=0.1, maximum=1.0, value=1.0, step=0.1,
127
+ label="πŸ“‰ Render Scale",
128
+ info="1.0 = Original Resolution. Reduce to 0.5 for faster processing on 4K content."
129
  )
130
  fps_param = gr.Number(
131
  value=0,
132
+ label="🎯 Force Output FPS",
133
+ info="0 = Auto-calculate. Set to 30 or 60 to lock the framerate. Audio will be removed when forcing FPS"
134
  )
135
  exp_param = gr.Number(
136
  value=1,
137
+ label="πŸ”’ Exponent Power",
138
+ info="Alternative multiplier calculation (2^exp)."
139
  )
140
 
141
  with gr.Row():
142
  uhd_chk = gr.Checkbox(
143
+ label="πŸ’Ž UHD/4K Optimization",
144
  value=False,
145
+ info="Enable for high-resolution sources."
 
 
 
 
 
146
  )
 
 
147
  skip_chk = gr.Checkbox(
148
+ label="⏩ Skip Static Frames",
149
  value=False,
150
+ info="Bypass processing for non-moving scenes to save time."
151
  )
152
  montage_chk = gr.Checkbox(
153
+ label="πŸ†š Split-Screen Comparison",
 
 
 
 
 
154
  value=False,
155
+ info="Output video showing Original vs. Processed."
156
  )
157
 
158
+ btn_run = gr.Button("πŸš€ RENDER HIGH FPS VIDEO", variant="primary", size="lg")
159
 
160
  # --- Right Column: Output ---
161
  with gr.Column(scale=1):
162
+ output_vid = gr.Video(label="⚑ High FPS Result")
163
+ gr.Markdown("**Status:** Rendering time depends on input resolution and duration.")
164
 
165
  # --- Bind Logic ---
166
  btn_run.click(
167
  fn=run_rife,
168
  inputs=[
169
  input_vid,
 
170
  multi_param,
171
  exp_param,
172
  fps_param,
173
  scale_param,
174
  uhd_chk,
 
175
  skip_chk,
176
+ montage_chk
 
 
177
  ],
178
  outputs=output_vid
179
  )