Spaces:
Running
Running
Update simple_app.py
Browse files- simple_app.py +104 -120
simple_app.py
CHANGED
|
@@ -2,7 +2,7 @@ import gradio as gr
|
|
| 2 |
import re
|
| 3 |
import subprocess
|
| 4 |
import time
|
| 5 |
-
import
|
| 6 |
from tqdm import tqdm
|
| 7 |
from huggingface_hub import snapshot_download
|
| 8 |
|
|
@@ -14,60 +14,27 @@ snapshot_download(
|
|
| 14 |
|
| 15 |
def infer(prompt, progress=gr.Progress(track_tqdm=True)):
|
| 16 |
|
| 17 |
-
# Configuration:
|
| 18 |
total_process_steps = 11 # Total steps (including irrelevant ones)
|
| 19 |
-
irrelevant_steps = 4 # First 4 INFO messages are skipped
|
| 20 |
-
|
| 21 |
-
relevant_steps = total_process_steps - irrelevant_steps
|
| 22 |
|
| 23 |
-
# Create overall
|
| 24 |
overall_bar = tqdm(total=relevant_steps, desc="Overall Process", position=1,
|
| 25 |
ncols=120, dynamic_ncols=False, leave=True)
|
| 26 |
processed_steps = 0
|
| 27 |
|
| 28 |
-
# Regex
|
| 29 |
progress_pattern = re.compile(r"(\d+)%\|.*\| (\d+)/(\d+)")
|
| 30 |
video_progress_bar = None
|
| 31 |
|
| 32 |
-
# Variables for
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
current_sub_thread = None
|
| 37 |
|
| 38 |
-
# A flag to indicate if we are in video generation phase.
|
| 39 |
-
video_phase = False
|
| 40 |
-
|
| 41 |
-
def update_sub_bar(sub_bar, cancel_event):
|
| 42 |
-
# Tick sub_bar once per second for up to 20 seconds
|
| 43 |
-
for _ in range(20):
|
| 44 |
-
if cancel_event.is_set():
|
| 45 |
-
break
|
| 46 |
-
time.sleep(1)
|
| 47 |
-
with sub_lock:
|
| 48 |
-
if sub_bar.n < sub_bar.total:
|
| 49 |
-
sub_bar.update(1)
|
| 50 |
-
sub_bar.refresh()
|
| 51 |
-
|
| 52 |
-
def cancel_sub_bar():
|
| 53 |
-
nonlocal current_sub_bar, current_cancel_event
|
| 54 |
-
with sub_lock:
|
| 55 |
-
if current_cancel_event:
|
| 56 |
-
current_cancel_event.set()
|
| 57 |
-
if current_sub_bar:
|
| 58 |
-
# Finish any remaining ticks
|
| 59 |
-
remaining = current_sub_bar.total - current_sub_bar.n
|
| 60 |
-
if remaining > 0:
|
| 61 |
-
current_sub_bar.update(remaining)
|
| 62 |
-
current_sub_bar.close()
|
| 63 |
-
current_sub_bar = None
|
| 64 |
-
overall_bar.update(1)
|
| 65 |
-
overall_bar.refresh()
|
| 66 |
-
current_cancel_event = None
|
| 67 |
-
|
| 68 |
-
# Build the command.
|
| 69 |
command = [
|
| 70 |
-
"python", "-u", "-m", "generate", # -u
|
| 71 |
"--task", "t2v-1.3B",
|
| 72 |
"--size", "832*480",
|
| 73 |
"--ckpt_dir", "./Wan2.1-T2V-1.3B",
|
|
@@ -77,93 +44,110 @@ def infer(prompt, progress=gr.Progress(track_tqdm=True)):
|
|
| 77 |
"--save_file", "generated_video.mp4"
|
| 78 |
]
|
| 79 |
|
| 80 |
-
process = subprocess.Popen(
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
if
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
video_phase = True
|
| 103 |
-
# Initialize video progress bar.
|
| 104 |
-
# Here we assume the total will come from the log; if not, adjust as needed.
|
| 105 |
current = int(progress_match.group(2))
|
| 106 |
total = int(progress_match.group(3))
|
| 107 |
if video_progress_bar is None:
|
| 108 |
video_progress_bar = tqdm(total=total, desc="Video Generation", position=0,
|
| 109 |
ncols=120, dynamic_ncols=True, leave=True)
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
else:
|
| 135 |
-
|
| 136 |
-
if video_phase:
|
| 137 |
-
continue
|
| 138 |
-
|
| 139 |
-
# If a sub-step bar is already active, cancel it.
|
| 140 |
-
with sub_lock:
|
| 141 |
-
if current_sub_bar is not None:
|
| 142 |
-
cancel_sub_bar()
|
| 143 |
-
# Create a new sub-step bar for this INFO message.
|
| 144 |
-
current_cancel_event = threading.Event()
|
| 145 |
-
current_sub_bar = tqdm(total=20, desc=msg, position=2,
|
| 146 |
-
ncols=120, dynamic_ncols=False, leave=True)
|
| 147 |
-
current_sub_thread = threading.Thread(
|
| 148 |
-
target=update_sub_bar,
|
| 149 |
-
args=(current_sub_bar, current_cancel_event),
|
| 150 |
-
daemon=True
|
| 151 |
-
)
|
| 152 |
-
current_sub_thread.start()
|
| 153 |
-
continue
|
| 154 |
-
|
| 155 |
else:
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
-
# Process finished; clean up any active sub-step.
|
| 159 |
process.wait()
|
| 160 |
-
|
| 161 |
-
if current_cancel_event:
|
| 162 |
-
current_cancel_event.set()
|
| 163 |
-
if current_sub_bar:
|
| 164 |
-
cancel_sub_bar()
|
| 165 |
-
if video_progress_bar:
|
| 166 |
video_progress_bar.close()
|
|
|
|
|
|
|
| 167 |
overall_bar.close()
|
| 168 |
|
| 169 |
if process.returncode == 0:
|
|
|
|
| 2 |
import re
|
| 3 |
import subprocess
|
| 4 |
import time
|
| 5 |
+
import select
|
| 6 |
from tqdm import tqdm
|
| 7 |
from huggingface_hub import snapshot_download
|
| 8 |
|
|
|
|
| 14 |
|
| 15 |
def infer(prompt, progress=gr.Progress(track_tqdm=True)):
|
| 16 |
|
| 17 |
+
# Configuration:
|
| 18 |
total_process_steps = 11 # Total steps (including irrelevant ones)
|
| 19 |
+
irrelevant_steps = 4 # First 4 INFO messages are skipped
|
| 20 |
+
relevant_steps = total_process_steps - irrelevant_steps # 7 overall steps
|
|
|
|
| 21 |
|
| 22 |
+
# Create overall progress bar (level 1)
|
| 23 |
overall_bar = tqdm(total=relevant_steps, desc="Overall Process", position=1,
|
| 24 |
ncols=120, dynamic_ncols=False, leave=True)
|
| 25 |
processed_steps = 0
|
| 26 |
|
| 27 |
+
# Regex for video generation progress (level 3)
|
| 28 |
progress_pattern = re.compile(r"(\d+)%\|.*\| (\d+)/(\d+)")
|
| 29 |
video_progress_bar = None
|
| 30 |
|
| 31 |
+
# Variables for sub-step progress bar (level 2)
|
| 32 |
+
sub_bar = None
|
| 33 |
+
sub_time_elapsed = 0 # seconds elapsed for the current sub-step
|
| 34 |
+
video_phase = False # flag indicating video generation phase
|
|
|
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
command = [
|
| 37 |
+
"python", "-u", "-m", "generate", # -u: unbuffered output
|
| 38 |
"--task", "t2v-1.3B",
|
| 39 |
"--size", "832*480",
|
| 40 |
"--ckpt_dir", "./Wan2.1-T2V-1.3B",
|
|
|
|
| 44 |
"--save_file", "generated_video.mp4"
|
| 45 |
]
|
| 46 |
|
| 47 |
+
process = subprocess.Popen(command,
|
| 48 |
+
stdout=subprocess.PIPE,
|
| 49 |
+
stderr=subprocess.STDOUT,
|
| 50 |
+
text=True,
|
| 51 |
+
bufsize=1)
|
| 52 |
+
|
| 53 |
+
# Poll the process's stdout in a loop.
|
| 54 |
+
while True:
|
| 55 |
+
# Wait up to 1 second for data.
|
| 56 |
+
rlist, _, _ = select.select([process.stdout], [], [], 1)
|
| 57 |
+
if rlist:
|
| 58 |
+
# New line is available.
|
| 59 |
+
line = process.stdout.readline()
|
| 60 |
+
if not line:
|
| 61 |
+
break
|
| 62 |
+
stripped_line = line.strip()
|
| 63 |
+
if not stripped_line:
|
| 64 |
+
continue
|
| 65 |
+
|
| 66 |
+
# Check if line matches video generation progress.
|
| 67 |
+
progress_match = progress_pattern.search(stripped_line)
|
| 68 |
+
if progress_match:
|
| 69 |
+
# Enter video phase: if a sub-step is active, finish it.
|
| 70 |
+
if sub_bar is not None:
|
| 71 |
+
if sub_time_elapsed < 20:
|
| 72 |
+
sub_bar.update(20 - sub_time_elapsed)
|
| 73 |
+
sub_bar.close()
|
| 74 |
+
overall_bar.update(1)
|
| 75 |
+
overall_bar.refresh()
|
| 76 |
+
sub_bar = None
|
| 77 |
+
sub_time_elapsed = 0
|
| 78 |
video_phase = True
|
|
|
|
|
|
|
| 79 |
current = int(progress_match.group(2))
|
| 80 |
total = int(progress_match.group(3))
|
| 81 |
if video_progress_bar is None:
|
| 82 |
video_progress_bar = tqdm(total=total, desc="Video Generation", position=0,
|
| 83 |
ncols=120, dynamic_ncols=True, leave=True)
|
| 84 |
+
video_progress_bar.update(current - video_progress_bar.n)
|
| 85 |
+
video_progress_bar.refresh()
|
| 86 |
+
# When video progress is complete, finish the video phase.
|
| 87 |
+
if video_progress_bar.n >= video_progress_bar.total:
|
| 88 |
+
video_phase = False
|
| 89 |
+
overall_bar.update(1)
|
| 90 |
+
overall_bar.refresh()
|
| 91 |
+
video_progress_bar.close()
|
| 92 |
+
video_progress_bar = None
|
| 93 |
+
continue
|
| 94 |
+
|
| 95 |
+
# Process INFO messages.
|
| 96 |
+
if "INFO:" in stripped_line:
|
| 97 |
+
parts = stripped_line.split("INFO:", 1)
|
| 98 |
+
msg = parts[1].strip() if len(parts) > 1 else ""
|
| 99 |
+
# Print the log line.
|
| 100 |
+
print(stripped_line)
|
| 101 |
+
|
| 102 |
+
if processed_steps < irrelevant_steps:
|
| 103 |
+
processed_steps += 1
|
| 104 |
+
else:
|
| 105 |
+
# If we're in video phase, ignore new INFO messages.
|
| 106 |
+
if video_phase:
|
| 107 |
+
continue
|
| 108 |
+
# If a sub-step is already active, finish it.
|
| 109 |
+
if sub_bar is not None:
|
| 110 |
+
if sub_time_elapsed < 20:
|
| 111 |
+
sub_bar.update(20 - sub_time_elapsed)
|
| 112 |
+
sub_bar.close()
|
| 113 |
+
overall_bar.update(1)
|
| 114 |
+
overall_bar.refresh()
|
| 115 |
+
sub_bar = None
|
| 116 |
+
sub_time_elapsed = 0
|
| 117 |
+
# Start a new sub-step progress bar.
|
| 118 |
+
sub_bar = tqdm(total=20, desc=msg, position=2,
|
| 119 |
+
ncols=120, dynamic_ncols=False, leave=True)
|
| 120 |
+
sub_time_elapsed = 0
|
| 121 |
+
continue
|
| 122 |
else:
|
| 123 |
+
print(stripped_line)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
else:
|
| 125 |
+
# No new data for 1 second.
|
| 126 |
+
if sub_bar is not None:
|
| 127 |
+
sub_bar.update(1)
|
| 128 |
+
sub_time_elapsed += 1
|
| 129 |
+
sub_bar.refresh()
|
| 130 |
+
if sub_time_elapsed >= 20:
|
| 131 |
+
# Complete this sub-step.
|
| 132 |
+
sub_bar.close()
|
| 133 |
+
overall_bar.update(1)
|
| 134 |
+
overall_bar.refresh()
|
| 135 |
+
sub_bar = None
|
| 136 |
+
sub_time_elapsed = 0
|
| 137 |
+
|
| 138 |
+
# Exit loop if the process is finished.
|
| 139 |
+
if process.poll() is not None:
|
| 140 |
+
break
|
| 141 |
+
|
| 142 |
+
# Drain any remaining output.
|
| 143 |
+
for line in process.stdout:
|
| 144 |
+
print(line.strip())
|
| 145 |
|
|
|
|
| 146 |
process.wait()
|
| 147 |
+
if video_progress_bar is not None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
video_progress_bar.close()
|
| 149 |
+
if sub_bar is not None:
|
| 150 |
+
sub_bar.close()
|
| 151 |
overall_bar.close()
|
| 152 |
|
| 153 |
if process.returncode == 0:
|