Spaces:
Paused
Paused
Update app.py
Browse files
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 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 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 |
-
|
| 48 |
-
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
-
|
| 54 |
cmd.extend(["--video", input_video])
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 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 |
-
|
| 73 |
-
|
| 74 |
-
if scale != 1.0:
|
| 75 |
-
cmd.extend(["--scale", str(scale)])
|
| 76 |
|
| 77 |
-
# --ext (Extension)
|
| 78 |
cmd.extend(["--ext", ext])
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
|
| 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
|
| 90 |
-
cmd.append("--fp16")
|
| 91 |
-
|
| 92 |
-
if skip:
|
| 93 |
cmd.append("--skip")
|
| 94 |
|
| 95 |
-
if
|
| 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 |
-
|
| 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(
|
| 123 |
-
return
|
| 124 |
else:
|
| 125 |
-
|
|
|
|
|
|
|
| 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="
|
| 134 |
-
gr.Markdown("#
|
| 135 |
-
gr.Markdown("
|
|
|
|
| 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("###
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 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("
|
| 167 |
-
gr.Markdown("
|
| 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="
|
| 173 |
-
info="1.0 = Original
|
| 174 |
)
|
| 175 |
fps_param = gr.Number(
|
| 176 |
value=0,
|
| 177 |
-
label="Force
|
| 178 |
-
info="0 = Auto-calculate
|
| 179 |
)
|
| 180 |
exp_param = gr.Number(
|
| 181 |
value=1,
|
| 182 |
-
label="Exponent Power
|
| 183 |
-
info="Alternative
|
| 184 |
)
|
| 185 |
|
| 186 |
with gr.Row():
|
| 187 |
uhd_chk = gr.Checkbox(
|
| 188 |
-
label="UHD
|
| 189 |
value=False,
|
| 190 |
-
info="
|
| 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
|
| 201 |
value=False,
|
| 202 |
-
info="
|
| 203 |
)
|
| 204 |
montage_chk = gr.Checkbox(
|
| 205 |
-
label="
|
| 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="
|
| 213 |
)
|
| 214 |
|
| 215 |
-
btn_run = gr.Button("
|
| 216 |
|
| 217 |
# --- Right Column: Output ---
|
| 218 |
with gr.Column(scale=1):
|
| 219 |
-
output_vid = gr.Video(label="
|
| 220 |
-
gr.Markdown("**
|
| 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 |
)
|