Spaces:
Running
Running
Update simple_app.py
Browse files- simple_app.py +44 -23
simple_app.py
CHANGED
|
@@ -14,7 +14,7 @@ snapshot_download(
|
|
| 14 |
|
| 15 |
def infer(prompt, progress=gr.Progress(track_tqdm=True)):
|
| 16 |
|
| 17 |
-
|
| 18 |
irrelevant_steps = 4
|
| 19 |
relevant_steps = total_process_steps - irrelevant_steps # 7 steps
|
| 20 |
|
|
@@ -29,26 +29,45 @@ def infer(prompt, progress=gr.Progress(track_tqdm=True)):
|
|
| 29 |
|
| 30 |
# Variables for managing the sub-progress bar for each step.
|
| 31 |
current_sub_bar = None
|
| 32 |
-
|
|
|
|
| 33 |
sub_lock = threading.Lock()
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
def close_sub_bar():
|
| 36 |
-
nonlocal current_sub_bar,
|
| 37 |
with sub_lock:
|
| 38 |
if current_sub_bar is not None:
|
| 39 |
try:
|
| 40 |
-
#
|
| 41 |
-
current_sub_bar.
|
|
|
|
|
|
|
| 42 |
except Exception:
|
| 43 |
pass
|
| 44 |
current_sub_bar.close()
|
| 45 |
overall_bar.update(1)
|
| 46 |
overall_bar.refresh()
|
| 47 |
current_sub_bar = None
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
command = [
|
| 51 |
-
"python", "-u", "-m", "generate", # using -u for unbuffered output and
|
| 52 |
"--task", "t2v-1.3B",
|
| 53 |
"--size", "832*480",
|
| 54 |
"--ckpt_dir", "./Wan2.1-T2V-1.3B",
|
|
@@ -58,7 +77,6 @@ def infer(prompt, progress=gr.Progress(track_tqdm=True)):
|
|
| 58 |
"--save_file", "generated_video.mp4"
|
| 59 |
]
|
| 60 |
|
| 61 |
-
# Start the process with unbuffered output and combine stdout and stderr.
|
| 62 |
process = subprocess.Popen(
|
| 63 |
command,
|
| 64 |
stdout=subprocess.PIPE,
|
|
@@ -88,34 +106,37 @@ def infer(prompt, progress=gr.Progress(track_tqdm=True)):
|
|
| 88 |
if "INFO:" in stripped_line:
|
| 89 |
parts = stripped_line.split("INFO:", 1)
|
| 90 |
msg = parts[1].strip() if len(parts) > 1 else ""
|
| 91 |
-
tqdm.write(stripped_line)
|
| 92 |
|
| 93 |
if processed_steps < irrelevant_steps:
|
| 94 |
processed_steps += 1
|
| 95 |
else:
|
| 96 |
with sub_lock:
|
| 97 |
-
# If a sub-bar is active, cancel its
|
| 98 |
if current_sub_bar is not None:
|
| 99 |
-
if
|
| 100 |
-
|
| 101 |
close_sub_bar()
|
| 102 |
-
#
|
| 103 |
-
current_sub_bar = tqdm(total=
|
| 104 |
ncols=120, dynamic_ncols=False, leave=True)
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
| 108 |
continue
|
| 109 |
|
| 110 |
else:
|
| 111 |
tqdm.write(stripped_line)
|
| 112 |
|
| 113 |
process.wait()
|
| 114 |
-
#
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
| 119 |
if gen_progress_bar:
|
| 120 |
gen_progress_bar.close()
|
| 121 |
overall_bar.close()
|
|
|
|
| 14 |
|
| 15 |
def infer(prompt, progress=gr.Progress(track_tqdm=True)):
|
| 16 |
|
| 17 |
+
total_process_steps = 11
|
| 18 |
irrelevant_steps = 4
|
| 19 |
relevant_steps = total_process_steps - irrelevant_steps # 7 steps
|
| 20 |
|
|
|
|
| 29 |
|
| 30 |
# Variables for managing the sub-progress bar for each step.
|
| 31 |
current_sub_bar = None
|
| 32 |
+
current_sub_thread = None
|
| 33 |
+
current_cancel_event = None
|
| 34 |
sub_lock = threading.Lock()
|
| 35 |
|
| 36 |
+
def update_sub_bar(sub_bar, cancel_event):
|
| 37 |
+
# This function updates the sub_bar once per second up to 20 seconds,
|
| 38 |
+
# unless the cancel_event is set.
|
| 39 |
+
for i in range(20):
|
| 40 |
+
if cancel_event.is_set():
|
| 41 |
+
break
|
| 42 |
+
time.sleep(1)
|
| 43 |
+
sub_bar.update(1)
|
| 44 |
+
sub_bar.refresh()
|
| 45 |
+
# When done (or canceled), do nothing here;
|
| 46 |
+
# closing will be done in close_sub_bar() from the main thread.
|
| 47 |
+
|
| 48 |
def close_sub_bar():
|
| 49 |
+
nonlocal current_sub_bar, current_sub_thread, current_cancel_event
|
| 50 |
with sub_lock:
|
| 51 |
if current_sub_bar is not None:
|
| 52 |
try:
|
| 53 |
+
# Complete any remaining ticks (if any)
|
| 54 |
+
remaining = current_sub_bar.total - current_sub_bar.n
|
| 55 |
+
if remaining > 0:
|
| 56 |
+
current_sub_bar.update(remaining)
|
| 57 |
except Exception:
|
| 58 |
pass
|
| 59 |
current_sub_bar.close()
|
| 60 |
overall_bar.update(1)
|
| 61 |
overall_bar.refresh()
|
| 62 |
current_sub_bar = None
|
| 63 |
+
if current_sub_thread is not None:
|
| 64 |
+
current_sub_thread.join()
|
| 65 |
+
current_sub_thread = None
|
| 66 |
+
if current_cancel_event is not None:
|
| 67 |
+
current_cancel_event = None
|
| 68 |
+
|
| 69 |
command = [
|
| 70 |
+
"python", "-u", "-m", "generate", # using -u for unbuffered output and module name without .py
|
| 71 |
"--task", "t2v-1.3B",
|
| 72 |
"--size", "832*480",
|
| 73 |
"--ckpt_dir", "./Wan2.1-T2V-1.3B",
|
|
|
|
| 77 |
"--save_file", "generated_video.mp4"
|
| 78 |
]
|
| 79 |
|
|
|
|
| 80 |
process = subprocess.Popen(
|
| 81 |
command,
|
| 82 |
stdout=subprocess.PIPE,
|
|
|
|
| 106 |
if "INFO:" in stripped_line:
|
| 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 |
+
# If a sub-bar is already active, cancel its update and close it.
|
| 116 |
if current_sub_bar is not None:
|
| 117 |
+
if current_cancel_event is not None:
|
| 118 |
+
current_cancel_event.set()
|
| 119 |
close_sub_bar()
|
| 120 |
+
# Now create a new sub-bar for the current step.
|
| 121 |
+
current_sub_bar = tqdm(total=20, desc=msg, position=2,
|
| 122 |
ncols=120, dynamic_ncols=False, leave=True)
|
| 123 |
+
current_cancel_event = threading.Event()
|
| 124 |
+
current_sub_thread = threading.Thread(target=update_sub_bar,
|
| 125 |
+
args=(current_sub_bar, current_cancel_event))
|
| 126 |
+
current_sub_thread.start()
|
| 127 |
continue
|
| 128 |
|
| 129 |
else:
|
| 130 |
tqdm.write(stripped_line)
|
| 131 |
|
| 132 |
process.wait()
|
| 133 |
+
# After process ends, if a sub-bar is still active, cancel and close it.
|
| 134 |
+
with sub_lock:
|
| 135 |
+
if current_cancel_event is not None:
|
| 136 |
+
current_cancel_event.set()
|
| 137 |
+
if current_sub_bar is not None:
|
| 138 |
+
close_sub_bar()
|
| 139 |
+
|
| 140 |
if gen_progress_bar:
|
| 141 |
gen_progress_bar.close()
|
| 142 |
overall_bar.close()
|