Spaces:
Running
Running
Update simple_app.py
Browse files- simple_app.py +67 -23
simple_app.py
CHANGED
|
@@ -14,23 +14,32 @@ snapshot_download(
|
|
| 14 |
|
| 15 |
def infer(prompt, progress=gr.Progress(track_tqdm=True)):
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
relevant_steps = total_process_steps - irrelevant_steps
|
| 20 |
|
|
|
|
| 21 |
overall_bar = tqdm(total=relevant_steps, desc="Overall Process", position=1,
|
| 22 |
ncols=120, dynamic_ncols=False, leave=True)
|
| 23 |
processed_steps = 0
|
| 24 |
|
|
|
|
| 25 |
progress_pattern = re.compile(r"(\d+)%\|.*\| (\d+)/(\d+)")
|
| 26 |
-
|
| 27 |
|
|
|
|
| 28 |
current_sub_bar = None
|
| 29 |
current_cancel_event = None
|
| 30 |
sub_lock = threading.Lock()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
def update_sub_bar(sub_bar, cancel_event):
|
| 33 |
-
|
| 34 |
for _ in range(20):
|
| 35 |
if cancel_event.is_set():
|
| 36 |
break
|
|
@@ -41,12 +50,12 @@ def infer(prompt, progress=gr.Progress(track_tqdm=True)):
|
|
| 41 |
sub_bar.refresh()
|
| 42 |
|
| 43 |
def cancel_sub_bar():
|
| 44 |
-
"""Cancels the current sub-bar and advances the overall process."""
|
| 45 |
nonlocal current_sub_bar, current_cancel_event
|
| 46 |
with sub_lock:
|
| 47 |
if current_cancel_event:
|
| 48 |
current_cancel_event.set()
|
| 49 |
if current_sub_bar:
|
|
|
|
| 50 |
remaining = current_sub_bar.total - current_sub_bar.n
|
| 51 |
if remaining > 0:
|
| 52 |
current_sub_bar.update(remaining)
|
|
@@ -56,8 +65,9 @@ def infer(prompt, progress=gr.Progress(track_tqdm=True)):
|
|
| 56 |
overall_bar.refresh()
|
| 57 |
current_cancel_event = None
|
| 58 |
|
|
|
|
| 59 |
command = [
|
| 60 |
-
"python", "-u", "-m", "generate",
|
| 61 |
"--task", "t2v-1.3B",
|
| 62 |
"--size", "832*480",
|
| 63 |
"--ckpt_dir", "./Wan2.1-T2V-1.3B",
|
|
@@ -80,46 +90,80 @@ def infer(prompt, progress=gr.Progress(track_tqdm=True)):
|
|
| 80 |
if not stripped_line:
|
| 81 |
continue
|
| 82 |
|
| 83 |
-
# Check for video generation progress
|
| 84 |
progress_match = progress_pattern.search(stripped_line)
|
| 85 |
if progress_match:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
current = int(progress_match.group(2))
|
| 87 |
total = int(progress_match.group(3))
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
continue
|
| 94 |
|
| 95 |
-
#
|
| 96 |
if "INFO:" in stripped_line:
|
|
|
|
| 97 |
parts = stripped_line.split("INFO:", 1)
|
| 98 |
msg = parts[1].strip() if len(parts) > 1 else ""
|
| 99 |
tqdm.write(stripped_line)
|
| 100 |
|
|
|
|
| 101 |
if processed_steps < irrelevant_steps:
|
| 102 |
processed_steps += 1
|
| 103 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
with sub_lock:
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
#
|
|
|
|
| 108 |
current_sub_bar = tqdm(total=20, desc=msg, position=2,
|
| 109 |
ncols=120, dynamic_ncols=False, leave=True)
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
| 113 |
continue
|
| 114 |
|
| 115 |
else:
|
| 116 |
tqdm.write(stripped_line)
|
| 117 |
|
|
|
|
| 118 |
process.wait()
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
| 123 |
overall_bar.close()
|
| 124 |
|
| 125 |
if process.returncode == 0:
|
|
|
|
| 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 = 11 - 4 = 7 overall steps that will be shown
|
| 21 |
relevant_steps = total_process_steps - irrelevant_steps
|
| 22 |
|
| 23 |
+
# Create overall process progress bar (level 1)
|
| 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 to capture video generation progress lines (for level 3)
|
| 29 |
progress_pattern = re.compile(r"(\d+)%\|.*\| (\d+)/(\d+)")
|
| 30 |
+
video_progress_bar = None
|
| 31 |
|
| 32 |
+
# Variables for managing sub-step progress bar (level 2)
|
| 33 |
current_sub_bar = None
|
| 34 |
current_cancel_event = None
|
| 35 |
sub_lock = threading.Lock()
|
| 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
|
|
|
|
| 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)
|
|
|
|
| 65 |
overall_bar.refresh()
|
| 66 |
current_cancel_event = None
|
| 67 |
|
| 68 |
+
# Build the command.
|
| 69 |
command = [
|
| 70 |
+
"python", "-u", "-m", "generate", # -u forces unbuffered output
|
| 71 |
"--task", "t2v-1.3B",
|
| 72 |
"--size", "832*480",
|
| 73 |
"--ckpt_dir", "./Wan2.1-T2V-1.3B",
|
|
|
|
| 90 |
if not stripped_line:
|
| 91 |
continue
|
| 92 |
|
| 93 |
+
# Check for video generation progress (level 3)
|
| 94 |
progress_match = progress_pattern.search(stripped_line)
|
| 95 |
if progress_match:
|
| 96 |
+
# On the first video progress line, if not already in video phase:
|
| 97 |
+
if not video_phase:
|
| 98 |
+
# Cancel any active sub-step bar before entering video phase.
|
| 99 |
+
with sub_lock:
|
| 100 |
+
if current_sub_bar:
|
| 101 |
+
cancel_sub_bar()
|
| 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 |
+
# Update video generation progress.
|
| 111 |
current = int(progress_match.group(2))
|
| 112 |
total = int(progress_match.group(3))
|
| 113 |
+
video_progress_bar.update(current - video_progress_bar.n)
|
| 114 |
+
video_progress_bar.refresh()
|
| 115 |
+
# If video progress is complete, finish the video phase.
|
| 116 |
+
if video_progress_bar.n >= video_progress_bar.total:
|
| 117 |
+
video_phase = False
|
| 118 |
+
overall_bar.update(1)
|
| 119 |
+
overall_bar.refresh()
|
| 120 |
+
video_progress_bar.close()
|
| 121 |
+
video_progress_bar = None
|
| 122 |
continue
|
| 123 |
|
| 124 |
+
# Process INFO messages (level 2 sub-step)
|
| 125 |
if "INFO:" in stripped_line:
|
| 126 |
+
# Extract the text after "INFO:"
|
| 127 |
parts = stripped_line.split("INFO:", 1)
|
| 128 |
msg = parts[1].strip() if len(parts) > 1 else ""
|
| 129 |
tqdm.write(stripped_line)
|
| 130 |
|
| 131 |
+
# Skip the first 4 irrelevant INFO messages.
|
| 132 |
if processed_steps < irrelevant_steps:
|
| 133 |
processed_steps += 1
|
| 134 |
else:
|
| 135 |
+
# If we are in video phase, ignore new INFO messages (or optionally queue them).
|
| 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 |
tqdm.write(stripped_line)
|
| 157 |
|
| 158 |
+
# Process finished; clean up any active sub-step.
|
| 159 |
process.wait()
|
| 160 |
+
with sub_lock:
|
| 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:
|