Spaces:
Running
Running
Update simple_app.py
Browse files- simple_app.py +21 -38
simple_app.py
CHANGED
|
@@ -16,57 +16,48 @@ def infer(prompt, progress=gr.Progress(track_tqdm=True)):
|
|
| 16 |
|
| 17 |
total_process_steps = 11
|
| 18 |
irrelevant_steps = 4
|
| 19 |
-
# Only steps 5 through 11 (i.e. 7 steps) count.
|
| 20 |
relevant_steps = total_process_steps - irrelevant_steps
|
| 21 |
|
| 22 |
-
# Create the overall progress bar for the steps.
|
| 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 detecting video generation progress lines (e.g. "10%|...| 5/50")
|
| 28 |
progress_pattern = re.compile(r"(\d+)%\|.*\| (\d+)/(\d+)")
|
| 29 |
gen_progress_bar = None
|
| 30 |
|
| 31 |
-
# Variables for managing the sub-progress bar for each step.
|
| 32 |
current_sub_bar = None
|
| 33 |
-
current_sub_thread = None
|
| 34 |
current_cancel_event = None
|
| 35 |
sub_lock = threading.Lock()
|
| 36 |
|
| 37 |
def update_sub_bar(sub_bar, cancel_event):
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
for i in range(20):
|
| 41 |
if cancel_event.is_set():
|
| 42 |
break
|
| 43 |
time.sleep(1)
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
def cancel_sub_bar():
|
| 49 |
-
|
|
|
|
| 50 |
with sub_lock:
|
| 51 |
-
if current_cancel_event
|
| 52 |
current_cancel_event.set()
|
| 53 |
-
if
|
| 54 |
-
current_sub_thread.join(timeout=1)
|
| 55 |
-
current_sub_thread = None
|
| 56 |
-
if current_sub_bar is not None:
|
| 57 |
-
# Complete any remaining ticks.
|
| 58 |
remaining = current_sub_bar.total - current_sub_bar.n
|
| 59 |
if remaining > 0:
|
| 60 |
current_sub_bar.update(remaining)
|
| 61 |
current_sub_bar.close()
|
| 62 |
current_sub_bar = None
|
| 63 |
-
# Update overall progress by one step.
|
| 64 |
overall_bar.update(1)
|
| 65 |
overall_bar.refresh()
|
| 66 |
current_cancel_event = None
|
| 67 |
|
| 68 |
command = [
|
| 69 |
-
"python", "-u", "-m", "generate",
|
| 70 |
"--task", "t2v-1.3B",
|
| 71 |
"--size", "832*480",
|
| 72 |
"--ckpt_dir", "./Wan2.1-T2V-1.3B",
|
|
@@ -81,7 +72,7 @@ def infer(prompt, progress=gr.Progress(track_tqdm=True)):
|
|
| 81 |
stdout=subprocess.PIPE,
|
| 82 |
stderr=subprocess.STDOUT,
|
| 83 |
text=True,
|
| 84 |
-
bufsize=1
|
| 85 |
)
|
| 86 |
|
| 87 |
for line in iter(process.stdout.readline, ''):
|
|
@@ -89,7 +80,7 @@ def infer(prompt, progress=gr.Progress(track_tqdm=True)):
|
|
| 89 |
if not stripped_line:
|
| 90 |
continue
|
| 91 |
|
| 92 |
-
# Check for video generation progress
|
| 93 |
progress_match = progress_pattern.search(stripped_line)
|
| 94 |
if progress_match:
|
| 95 |
current = int(progress_match.group(2))
|
|
@@ -101,39 +92,31 @@ def infer(prompt, progress=gr.Progress(track_tqdm=True)):
|
|
| 101 |
gen_progress_bar.refresh()
|
| 102 |
continue
|
| 103 |
|
| 104 |
-
# Check for INFO
|
| 105 |
if "INFO:" in stripped_line:
|
| 106 |
-
# Extract the INFO message (the text after "INFO:")
|
| 107 |
parts = stripped_line.split("INFO:", 1)
|
| 108 |
msg = parts[1].strip() if len(parts) > 1 else ""
|
| 109 |
-
tqdm.write(stripped_line)
|
| 110 |
|
| 111 |
if processed_steps < irrelevant_steps:
|
| 112 |
processed_steps += 1
|
| 113 |
else:
|
| 114 |
with sub_lock:
|
| 115 |
-
#
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
# Create a new sub-progress bar for this step (lasting up to 20 seconds).
|
| 119 |
current_sub_bar = tqdm(total=20, desc=msg, position=2,
|
| 120 |
ncols=120, dynamic_ncols=False, leave=True)
|
| 121 |
current_cancel_event = threading.Event()
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
current_sub_thread.start()
|
| 125 |
continue
|
| 126 |
|
| 127 |
else:
|
| 128 |
tqdm.write(stripped_line)
|
| 129 |
|
| 130 |
-
# Process has ended; cancel any active sub-progress bar.
|
| 131 |
process.wait()
|
| 132 |
-
|
| 133 |
-
if current_cancel_event is not None:
|
| 134 |
-
current_cancel_event.set()
|
| 135 |
-
if current_sub_bar is not None:
|
| 136 |
-
cancel_sub_bar()
|
| 137 |
|
| 138 |
if gen_progress_bar:
|
| 139 |
gen_progress_bar.close()
|
|
|
|
| 16 |
|
| 17 |
total_process_steps = 11
|
| 18 |
irrelevant_steps = 4
|
|
|
|
| 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 |
gen_progress_bar = None
|
| 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 |
+
"""Updates the sub-bar every second up to 20 seconds unless canceled."""
|
| 34 |
+
for _ in range(20):
|
|
|
|
| 35 |
if cancel_event.is_set():
|
| 36 |
break
|
| 37 |
time.sleep(1)
|
| 38 |
+
with sub_lock:
|
| 39 |
+
if sub_bar.n < sub_bar.total:
|
| 40 |
+
sub_bar.update(1)
|
| 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)
|
| 53 |
current_sub_bar.close()
|
| 54 |
current_sub_bar = None
|
|
|
|
| 55 |
overall_bar.update(1)
|
| 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",
|
|
|
|
| 72 |
stdout=subprocess.PIPE,
|
| 73 |
stderr=subprocess.STDOUT,
|
| 74 |
text=True,
|
| 75 |
+
bufsize=1
|
| 76 |
)
|
| 77 |
|
| 78 |
for line in iter(process.stdout.readline, ''):
|
|
|
|
| 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))
|
|
|
|
| 92 |
gen_progress_bar.refresh()
|
| 93 |
continue
|
| 94 |
|
| 95 |
+
# Check for 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 |
+
tqdm.write(stripped_line)
|
| 100 |
|
| 101 |
if processed_steps < irrelevant_steps:
|
| 102 |
processed_steps += 1
|
| 103 |
else:
|
| 104 |
with sub_lock:
|
| 105 |
+
# Cancel the previous sub-bar if it exists
|
| 106 |
+
cancel_sub_bar()
|
| 107 |
+
# Start a new sub-bar
|
|
|
|
| 108 |
current_sub_bar = tqdm(total=20, desc=msg, position=2,
|
| 109 |
ncols=120, dynamic_ncols=False, leave=True)
|
| 110 |
current_cancel_event = threading.Event()
|
| 111 |
+
threading.Thread(target=update_sub_bar, args=(current_sub_bar, current_cancel_event),
|
| 112 |
+
daemon=True).start()
|
|
|
|
| 113 |
continue
|
| 114 |
|
| 115 |
else:
|
| 116 |
tqdm.write(stripped_line)
|
| 117 |
|
|
|
|
| 118 |
process.wait()
|
| 119 |
+
cancel_sub_bar()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
if gen_progress_bar:
|
| 122 |
gen_progress_bar.close()
|