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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import subprocess
|
| 3 |
+
import os
|
| 4 |
+
import spaces
|
| 5 |
+
|
| 6 |
+
# The name of your script
|
| 7 |
+
SCRIPT_NAME = "inference_video.py"
|
| 8 |
+
|
| 9 |
+
spaces.GPU()
|
| 10 |
+
def run_rife(
|
| 11 |
+
input_video,
|
| 12 |
+
model_dir,
|
| 13 |
+
multi,
|
| 14 |
+
exp,
|
| 15 |
+
fps,
|
| 16 |
+
scale,
|
| 17 |
+
uhd,
|
| 18 |
+
fp16,
|
| 19 |
+
skip,
|
| 20 |
+
montage,
|
| 21 |
+
png_mode,
|
| 22 |
+
ext
|
| 23 |
+
):
|
| 24 |
+
"""
|
| 25 |
+
Constructs the command line arguments based on Gradio inputs
|
| 26 |
+
and runs the inference_video.py script via subprocess.
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
if input_video is None:
|
| 30 |
+
raise gr.Error("Please upload a video first.")
|
| 31 |
+
|
| 32 |
+
# 1. Define Output Filename
|
| 33 |
+
output_name = f"output_{multi}X.{ext}"
|
| 34 |
+
|
| 35 |
+
# 2. Build the Command
|
| 36 |
+
cmd = ["python3", SCRIPT_NAME]
|
| 37 |
+
|
| 38 |
+
# --video
|
| 39 |
+
cmd.extend(["--video", input_video])
|
| 40 |
+
|
| 41 |
+
# --output
|
| 42 |
+
cmd.extend(["--output", output_name])
|
| 43 |
+
|
| 44 |
+
# --multi (Multiplier)
|
| 45 |
+
cmd.extend(["--multi", str(int(multi))])
|
| 46 |
+
|
| 47 |
+
# --exp
|
| 48 |
+
# Only add exp if it is not default, or if specific logic requires it.
|
| 49 |
+
# Usually multi overrides exp in RIFE logic, but we pass it if set.
|
| 50 |
+
if exp != 1:
|
| 51 |
+
cmd.extend(["--exp", str(int(exp))])
|
| 52 |
+
|
| 53 |
+
# --fps (Target FPS)
|
| 54 |
+
if fps > 0:
|
| 55 |
+
cmd.extend(["--fps", str(int(fps))])
|
| 56 |
+
|
| 57 |
+
# --scale (Resolution scale)
|
| 58 |
+
# Check against float 1.0
|
| 59 |
+
if scale != 1.0:
|
| 60 |
+
cmd.extend(["--scale", str(scale)])
|
| 61 |
+
|
| 62 |
+
# --ext (Extension)
|
| 63 |
+
cmd.extend(["--ext", ext])
|
| 64 |
+
|
| 65 |
+
# --model (Model directory)
|
| 66 |
+
if model_dir and model_dir.strip() != "":
|
| 67 |
+
cmd.extend(["--model", model_dir])
|
| 68 |
+
|
| 69 |
+
# --- Boolean Flags ---
|
| 70 |
+
|
| 71 |
+
if uhd:
|
| 72 |
+
cmd.append("--UHD")
|
| 73 |
+
|
| 74 |
+
if fp16:
|
| 75 |
+
cmd.append("--fp16")
|
| 76 |
+
|
| 77 |
+
if skip:
|
| 78 |
+
cmd.append("--skip")
|
| 79 |
+
|
| 80 |
+
if montage:
|
| 81 |
+
cmd.append("--montage")
|
| 82 |
+
|
| 83 |
+
if png_mode:
|
| 84 |
+
cmd.append("--png")
|
| 85 |
+
|
| 86 |
+
print(f"Executing command: {' '.join(cmd)}")
|
| 87 |
+
|
| 88 |
+
# 3. Run the Subprocess
|
| 89 |
+
try:
|
| 90 |
+
# We use a large timeout because video processing takes time
|
| 91 |
+
process = subprocess.run(cmd, capture_output=True, text=True)
|
| 92 |
+
|
| 93 |
+
# Log stdout/stderr
|
| 94 |
+
if process.stdout:
|
| 95 |
+
print("STDOUT:", process.stdout)
|
| 96 |
+
if process.stderr:
|
| 97 |
+
print("STDERR:", process.stderr)
|
| 98 |
+
|
| 99 |
+
if process.returncode != 0:
|
| 100 |
+
raise gr.Error(f"Inference failed. Error: {process.stderr}")
|
| 101 |
+
|
| 102 |
+
# 4. Return Result
|
| 103 |
+
if png_mode:
|
| 104 |
+
gr.Info("Processing complete. Output is a folder of PNGs (Video preview unavailable for PNG mode).")
|
| 105 |
+
return None
|
| 106 |
+
|
| 107 |
+
if os.path.exists(output_name):
|
| 108 |
+
return output_name
|
| 109 |
+
else:
|
| 110 |
+
raise gr.Error("Output file was not found. Check console for details.")
|
| 111 |
+
|
| 112 |
+
except Exception as e:
|
| 113 |
+
raise gr.Error(f"An error occurred: {str(e)}")
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
# --- Gradio UI Layout ---
|
| 117 |
+
|
| 118 |
+
with gr.Blocks(title="RIFE Video Interpolation", theme=gr.themes.Soft()) as app:
|
| 119 |
+
gr.Markdown("# 🚀 RIFE: Real-Time Intermediate Flow Estimation")
|
| 120 |
+
gr.Markdown("Upload a video to increase its frame rate (smoothness) using AI.")
|
| 121 |
+
|
| 122 |
+
with gr.Row():
|
| 123 |
+
# --- Left Column: Inputs & Settings ---
|
| 124 |
+
with gr.Column(scale=1):
|
| 125 |
+
input_vid = gr.Video(label="Input Video", sources=["upload"])
|
| 126 |
+
|
| 127 |
+
with gr.Group():
|
| 128 |
+
gr.Markdown("### 🎯 Core Parameters")
|
| 129 |
+
|
| 130 |
+
with gr.Row():
|
| 131 |
+
multi_param = gr.Dropdown(
|
| 132 |
+
choices=["2", "4", "8", "16", "32"],
|
| 133 |
+
value="2",
|
| 134 |
+
label="Interpolation Multiplier (--multi)",
|
| 135 |
+
info="How many times to multiply the frames. 2X doubles the FPS (e.g., 30fps -> 60fps). 4X quadruples it."
|
| 136 |
+
)
|
| 137 |
+
ext_param = gr.Dropdown(
|
| 138 |
+
choices=["mp4", "avi", "mov", "mkv"],
|
| 139 |
+
value="mp4",
|
| 140 |
+
label="Output Format (--ext)",
|
| 141 |
+
info="The file extension for the generated video."
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
model_param = gr.Textbox(
|
| 145 |
+
value="train_log",
|
| 146 |
+
label="Model Directory (--model)",
|
| 147 |
+
placeholder="train_log",
|
| 148 |
+
info="Path to the folder containing the trained model files (e.g., 'train_log' or 'rife-v4.6')."
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 152 |
+
gr.Markdown("Fine-tune the inference process.")
|
| 153 |
+
|
| 154 |
+
with gr.Row():
|
| 155 |
+
scale_param = gr.Slider(
|
| 156 |
+
minimum=0.1, maximum=1.0, value=1.0, step=0.1,
|
| 157 |
+
label="Input Scale (--scale)",
|
| 158 |
+
info="1.0 = Original resolution. Set to 0.5 to reduce memory usage for 4K video inputs."
|
| 159 |
+
)
|
| 160 |
+
fps_param = gr.Number(
|
| 161 |
+
value=0,
|
| 162 |
+
label="Force Target FPS (--fps)",
|
| 163 |
+
info="0 = Auto-calculate based on multiplier. Enter a number (e.g., 60) to force a specific output frame rate."
|
| 164 |
+
)
|
| 165 |
+
exp_param = gr.Number(
|
| 166 |
+
value=1,
|
| 167 |
+
label="Exponent Power (--exp)",
|
| 168 |
+
info="Alternative to Multiplier. Sets multiplier to 2^exp. (Usually left at 1 if Multiplier is set)."
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
with gr.Row():
|
| 172 |
+
uhd_chk = gr.Checkbox(
|
| 173 |
+
label="UHD Mode (--UHD)",
|
| 174 |
+
value=False,
|
| 175 |
+
info="Optimized for 4K video. Equivalent to setting scale=0.5 manually."
|
| 176 |
+
)
|
| 177 |
+
fp16_chk = gr.Checkbox(
|
| 178 |
+
label="FP16 Mode (--fp16)",
|
| 179 |
+
value=True,
|
| 180 |
+
info="Uses half-precision floating point. Faster and uses less VRAM with minimal quality loss."
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
with gr.Row():
|
| 184 |
+
skip_chk = gr.Checkbox(
|
| 185 |
+
label="Skip Static Frames (--skip)",
|
| 186 |
+
value=False,
|
| 187 |
+
info="If the video has frames that don't move, skip processing them to save time."
|
| 188 |
+
)
|
| 189 |
+
montage_chk = gr.Checkbox(
|
| 190 |
+
label="Montage (--montage)",
|
| 191 |
+
value=False,
|
| 192 |
+
info="Creates a video with the Original on the Left and Interpolated on the Right for comparison."
|
| 193 |
+
)
|
| 194 |
+
png_chk = gr.Checkbox(
|
| 195 |
+
label="Output as PNGs (--png)",
|
| 196 |
+
value=False,
|
| 197 |
+
info="Outputs a sequence of images instead of a video file. (Video Preview will be disabled)."
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
btn_run = gr.Button("✨ Start Interpolation", variant="primary", size="lg")
|
| 201 |
+
|
| 202 |
+
# --- Right Column: Output ---
|
| 203 |
+
with gr.Column(scale=1):
|
| 204 |
+
output_vid = gr.Video(label="Interpolated Result")
|
| 205 |
+
gr.Markdown("**Note:** Processing time depends on video length, resolution, and your GPU speed.")
|
| 206 |
+
|
| 207 |
+
# --- Bind Logic ---
|
| 208 |
+
btn_run.click(
|
| 209 |
+
fn=run_rife,
|
| 210 |
+
inputs=[
|
| 211 |
+
input_vid,
|
| 212 |
+
model_param,
|
| 213 |
+
multi_param,
|
| 214 |
+
exp_param,
|
| 215 |
+
fps_param,
|
| 216 |
+
scale_param,
|
| 217 |
+
uhd_chk,
|
| 218 |
+
fp16_chk,
|
| 219 |
+
skip_chk,
|
| 220 |
+
montage_chk,
|
| 221 |
+
png_chk,
|
| 222 |
+
ext_param
|
| 223 |
+
],
|
| 224 |
+
outputs=output_vid
|
| 225 |
+
)
|
| 226 |
+
|
| 227 |
+
if __name__ == "__main__":
|
| 228 |
+
app.launch()
|