Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
import zipfile
|
| 4 |
import subprocess
|
| 5 |
-
|
| 6 |
|
| 7 |
|
| 8 |
# --------- AUTO EXTRACT train_log.zip ----------
|
|
@@ -37,8 +37,6 @@ def ensure_train_log():
|
|
| 37 |
return
|
| 38 |
|
| 39 |
os.rename(found_path, target_folder)
|
| 40 |
-
|
| 41 |
-
import shutil
|
| 42 |
shutil.rmtree(tmp_dir)
|
| 43 |
|
| 44 |
print("train_log extracted successfully!")
|
|
@@ -65,38 +63,23 @@ def get_fps(video_path):
|
|
| 65 |
else:
|
| 66 |
fps = float(result)
|
| 67 |
|
| 68 |
-
return int(fps)
|
| 69 |
|
| 70 |
except:
|
| 71 |
return 0
|
| 72 |
|
| 73 |
|
| 74 |
-
# -------- RIFE
|
| 75 |
def run_rife(video, multi):
|
| 76 |
-
progress = Progress(track_tqdm=True)
|
| 77 |
-
|
| 78 |
input_path = video
|
|
|
|
| 79 |
if not os.path.isfile(input_path):
|
| 80 |
-
|
|
|
|
| 81 |
|
| 82 |
ext = os.path.splitext(input_path)[1].lower()
|
| 83 |
output_path = f"output{ext}"
|
| 84 |
|
| 85 |
-
# ---- Lấy tổng frame để chạy % ----
|
| 86 |
-
total_frames = 0
|
| 87 |
-
try:
|
| 88 |
-
cmd_count = [
|
| 89 |
-
"ffprobe", "-v", "error",
|
| 90 |
-
"-count_frames", "-select_streams", "v:0",
|
| 91 |
-
"-show_entries", "stream=nb_read_frames",
|
| 92 |
-
"-of", "default=nokey=1:noprint_wrappers=1",
|
| 93 |
-
input_path
|
| 94 |
-
]
|
| 95 |
-
total_frames = int(subprocess.check_output(cmd_count).decode().strip())
|
| 96 |
-
except:
|
| 97 |
-
total_frames = 0
|
| 98 |
-
|
| 99 |
-
# ---- Chạy RIFE và đọc stdout để cập nhật % ----
|
| 100 |
cmd = [
|
| 101 |
"python3", "inference_video.py",
|
| 102 |
"--video", input_path,
|
|
@@ -104,45 +87,27 @@ def run_rife(video, multi):
|
|
| 104 |
"--multi", str(multi)
|
| 105 |
]
|
| 106 |
|
|
|
|
| 107 |
process = subprocess.Popen(
|
| 108 |
cmd,
|
| 109 |
stdout=subprocess.PIPE,
|
| 110 |
stderr=subprocess.STDOUT,
|
| 111 |
-
text=True
|
|
|
|
| 112 |
)
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
# --- Đọc log realtime ---
|
| 117 |
for line in process.stdout:
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
# RIFE chuẩn hay in "XX/YYY"
|
| 121 |
-
if "/" in line:
|
| 122 |
-
try:
|
| 123 |
-
parts = line.split("/")
|
| 124 |
-
cur = int(parts[0])
|
| 125 |
-
total = int(parts[1])
|
| 126 |
-
|
| 127 |
-
percent = cur / total
|
| 128 |
-
progress(percent)
|
| 129 |
-
|
| 130 |
-
except:
|
| 131 |
-
pass
|
| 132 |
-
|
| 133 |
-
# fallback nếu ffprobe lấy được số frame
|
| 134 |
-
elif total_frames > 0 and "frame" in line:
|
| 135 |
-
current += 1
|
| 136 |
-
progress(current / total_frames)
|
| 137 |
|
| 138 |
process.wait()
|
| 139 |
|
| 140 |
if process.returncode != 0:
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
progress(1.0) # hoàn tất 100%
|
| 144 |
|
| 145 |
-
|
| 146 |
|
| 147 |
|
| 148 |
# -------- AUTO UPDATE FPS WHEN VIDEO CHANGES ----------
|
|
@@ -164,10 +129,10 @@ with gr.Blocks() as demo:
|
|
| 164 |
gr.Markdown("# **Practical RIFE**")
|
| 165 |
gr.Markdown("### https://github.com/hzwer/Practical-RIFE")
|
| 166 |
|
| 167 |
-
video_input = gr.Video(label="Upload video (MP4 / MKV / MOV)"
|
| 168 |
|
| 169 |
fps_display = gr.Number(label="Video FPS (auto-read)", value=0, interactive=False)
|
| 170 |
-
multi_input = gr.Slider(1, 10, value=4, step=1, label="multi (1
|
| 171 |
|
| 172 |
fps_output = gr.Number(label="Output FPS (display only)", value=0, interactive=False)
|
| 173 |
|
|
@@ -176,11 +141,14 @@ with gr.Blocks() as demo:
|
|
| 176 |
|
| 177 |
btn = gr.Button("Run RIFE")
|
| 178 |
|
| 179 |
-
log = gr.Textbox(label="Log")
|
| 180 |
output_video = gr.Video(label="Output video")
|
| 181 |
|
| 182 |
-
btn.click(
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
if __name__ == "__main__":
|
| 186 |
demo.launch()
|
|
|
|
| 2 |
import os
|
| 3 |
import zipfile
|
| 4 |
import subprocess
|
| 5 |
+
import shutil
|
| 6 |
|
| 7 |
|
| 8 |
# --------- AUTO EXTRACT train_log.zip ----------
|
|
|
|
| 37 |
return
|
| 38 |
|
| 39 |
os.rename(found_path, target_folder)
|
|
|
|
|
|
|
| 40 |
shutil.rmtree(tmp_dir)
|
| 41 |
|
| 42 |
print("train_log extracted successfully!")
|
|
|
|
| 63 |
else:
|
| 64 |
fps = float(result)
|
| 65 |
|
| 66 |
+
return int(fps) # làm tròn xuống
|
| 67 |
|
| 68 |
except:
|
| 69 |
return 0
|
| 70 |
|
| 71 |
|
| 72 |
+
# -------- RUN RIFE + STREAM TERMINAL LOG --------
|
| 73 |
def run_rife(video, multi):
|
|
|
|
|
|
|
| 74 |
input_path = video
|
| 75 |
+
|
| 76 |
if not os.path.isfile(input_path):
|
| 77 |
+
yield "Error: invalid input video", None
|
| 78 |
+
return
|
| 79 |
|
| 80 |
ext = os.path.splitext(input_path)[1].lower()
|
| 81 |
output_path = f"output{ext}"
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
cmd = [
|
| 84 |
"python3", "inference_video.py",
|
| 85 |
"--video", input_path,
|
|
|
|
| 87 |
"--multi", str(multi)
|
| 88 |
]
|
| 89 |
|
| 90 |
+
# Run process + stream output line by line
|
| 91 |
process = subprocess.Popen(
|
| 92 |
cmd,
|
| 93 |
stdout=subprocess.PIPE,
|
| 94 |
stderr=subprocess.STDOUT,
|
| 95 |
+
text=True,
|
| 96 |
+
bufsize=1
|
| 97 |
)
|
| 98 |
|
| 99 |
+
log_buffer = ""
|
|
|
|
|
|
|
| 100 |
for line in process.stdout:
|
| 101 |
+
log_buffer += line
|
| 102 |
+
yield log_buffer, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
process.wait()
|
| 105 |
|
| 106 |
if process.returncode != 0:
|
| 107 |
+
yield log_buffer + "\nError while running RIFE!", None
|
| 108 |
+
return
|
|
|
|
| 109 |
|
| 110 |
+
yield log_buffer + "\nDone!", output_path
|
| 111 |
|
| 112 |
|
| 113 |
# -------- AUTO UPDATE FPS WHEN VIDEO CHANGES ----------
|
|
|
|
| 129 |
gr.Markdown("# **Practical RIFE**")
|
| 130 |
gr.Markdown("### https://github.com/hzwer/Practical-RIFE")
|
| 131 |
|
| 132 |
+
video_input = gr.Video(label="Upload video (MP4 / MKV / MOV)")
|
| 133 |
|
| 134 |
fps_display = gr.Number(label="Video FPS (auto-read)", value=0, interactive=False)
|
| 135 |
+
multi_input = gr.Slider(1, 10, value=4, step=1, label="multi (1–10)")
|
| 136 |
|
| 137 |
fps_output = gr.Number(label="Output FPS (display only)", value=0, interactive=False)
|
| 138 |
|
|
|
|
| 141 |
|
| 142 |
btn = gr.Button("Run RIFE")
|
| 143 |
|
| 144 |
+
log = gr.Textbox(label="Terminal Log (realtime)", lines=20)
|
| 145 |
output_video = gr.Video(label="Output video")
|
| 146 |
|
| 147 |
+
btn.click(
|
| 148 |
+
run_rife,
|
| 149 |
+
inputs=[video_input, multi_input],
|
| 150 |
+
outputs=[log, output_video]
|
| 151 |
+
)
|
| 152 |
|
| 153 |
if __name__ == "__main__":
|
| 154 |
demo.launch()
|