Spaces:
Running
Running
Update simple_app.py
Browse files- simple_app.py +35 -25
simple_app.py
CHANGED
@@ -14,25 +14,27 @@ snapshot_download(
|
|
14 |
|
15 |
def infer(prompt, progress=gr.Progress(track_tqdm=True)):
|
16 |
|
17 |
-
# Configuration
|
18 |
-
total_process_steps = 11 # Total
|
19 |
-
irrelevant_steps = 4 # First 4 INFO messages are
|
20 |
-
relevant_steps = total_process_steps - irrelevant_steps # 7 overall steps
|
21 |
|
22 |
-
# Create overall progress bar (
|
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 (
|
28 |
progress_pattern = re.compile(r"(\d+)%\|.*\| (\d+)/(\d+)")
|
29 |
video_progress_bar = None
|
30 |
|
31 |
-
# Variables for sub-step progress bar (
|
|
|
32 |
sub_bar = None
|
33 |
-
sub_ticks = 0
|
34 |
-
sub_tick_total = 500
|
35 |
-
|
|
|
36 |
|
37 |
command = [
|
38 |
"python", "-u", "-m", "generate", # using -u for unbuffered output
|
@@ -52,7 +54,7 @@ def infer(prompt, progress=gr.Progress(track_tqdm=True)):
|
|
52 |
bufsize=1)
|
53 |
|
54 |
while True:
|
55 |
-
# Poll
|
56 |
rlist, _, _ = select.select([process.stdout], [], [], 0.04)
|
57 |
if rlist:
|
58 |
line = process.stdout.readline()
|
@@ -62,10 +64,10 @@ def infer(prompt, progress=gr.Progress(track_tqdm=True)):
|
|
62 |
if not stripped_line:
|
63 |
continue
|
64 |
|
65 |
-
# Check
|
66 |
progress_match = progress_pattern.search(stripped_line)
|
67 |
if progress_match:
|
68 |
-
#
|
69 |
if sub_bar is not None:
|
70 |
if sub_ticks < sub_tick_total:
|
71 |
sub_bar.update(sub_tick_total - sub_ticks)
|
@@ -74,7 +76,7 @@ def infer(prompt, progress=gr.Progress(track_tqdm=True)):
|
|
74 |
overall_bar.refresh()
|
75 |
sub_bar = None
|
76 |
sub_ticks = 0
|
77 |
-
|
78 |
current = int(progress_match.group(2))
|
79 |
total = int(progress_match.group(3))
|
80 |
if video_progress_bar is None:
|
@@ -82,27 +84,34 @@ def infer(prompt, progress=gr.Progress(track_tqdm=True)):
|
|
82 |
ncols=120, dynamic_ncols=True, leave=True)
|
83 |
video_progress_bar.update(current - video_progress_bar.n)
|
84 |
video_progress_bar.refresh()
|
|
|
85 |
if video_progress_bar.n >= video_progress_bar.total:
|
86 |
-
video_phase = False
|
87 |
overall_bar.update(1)
|
88 |
overall_bar.refresh()
|
89 |
video_progress_bar.close()
|
90 |
video_progress_bar = None
|
91 |
continue
|
92 |
|
93 |
-
# Process INFO messages
|
94 |
if "INFO:" in stripped_line:
|
95 |
parts = stripped_line.split("INFO:", 1)
|
96 |
msg = parts[1].strip() if len(parts) > 1 else ""
|
97 |
-
print(stripped_line)
|
98 |
|
|
|
99 |
if processed_steps < irrelevant_steps:
|
100 |
processed_steps += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
else:
|
102 |
-
#
|
103 |
-
|
104 |
-
|
105 |
-
# If a sub-step is already active, finish it.
|
106 |
if sub_bar is not None:
|
107 |
if sub_ticks < sub_tick_total:
|
108 |
sub_bar.update(sub_tick_total - sub_ticks)
|
@@ -111,7 +120,7 @@ def infer(prompt, progress=gr.Progress(track_tqdm=True)):
|
|
111 |
overall_bar.refresh()
|
112 |
sub_bar = None
|
113 |
sub_ticks = 0
|
114 |
-
# Start a new sub-step
|
115 |
sub_bar = tqdm(total=sub_tick_total, desc=msg, position=2,
|
116 |
ncols=120, dynamic_ncols=False, leave=True)
|
117 |
sub_ticks = 0
|
@@ -119,13 +128,14 @@ def infer(prompt, progress=gr.Progress(track_tqdm=True)):
|
|
119 |
else:
|
120 |
print(stripped_line)
|
121 |
else:
|
122 |
-
# No new data within 40ms
|
|
|
123 |
if sub_bar is not None:
|
124 |
sub_bar.update(1)
|
125 |
sub_ticks += 1
|
126 |
sub_bar.refresh()
|
127 |
if sub_ticks >= sub_tick_total:
|
128 |
-
# 20 seconds have elapsed;
|
129 |
sub_bar.close()
|
130 |
overall_bar.update(1)
|
131 |
overall_bar.refresh()
|
@@ -135,7 +145,7 @@ def infer(prompt, progress=gr.Progress(track_tqdm=True)):
|
|
135 |
if process.poll() is not None:
|
136 |
break
|
137 |
|
138 |
-
# Drain
|
139 |
for line in process.stdout:
|
140 |
print(line.strip())
|
141 |
process.wait()
|
|
|
14 |
|
15 |
def infer(prompt, progress=gr.Progress(track_tqdm=True)):
|
16 |
|
17 |
+
# Configuration
|
18 |
+
total_process_steps = 11 # Total INFO messages expected
|
19 |
+
irrelevant_steps = 4 # First 4 INFO messages are ignored
|
20 |
+
relevant_steps = total_process_steps - irrelevant_steps # 7 overall (relevant) steps
|
21 |
|
22 |
+
# Create overall process 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 |
+
# We use a tick total of 500 ticks = 20 seconds (each tick = 40ms)
|
33 |
sub_bar = None
|
34 |
+
sub_ticks = 0
|
35 |
+
sub_tick_total = 500
|
36 |
+
# Flag indicating whether we're still waiting for the first relevant step.
|
37 |
+
waiting_for_first_relevant = True
|
38 |
|
39 |
command = [
|
40 |
"python", "-u", "-m", "generate", # using -u for unbuffered output
|
|
|
54 |
bufsize=1)
|
55 |
|
56 |
while True:
|
57 |
+
# Poll stdout with a 40ms timeout.
|
58 |
rlist, _, _ = select.select([process.stdout], [], [], 0.04)
|
59 |
if rlist:
|
60 |
line = process.stdout.readline()
|
|
|
64 |
if not stripped_line:
|
65 |
continue
|
66 |
|
67 |
+
# Check if line matches video generation progress (Level 3)
|
68 |
progress_match = progress_pattern.search(stripped_line)
|
69 |
if progress_match:
|
70 |
+
# Before entering video phase, cancel any active sub-step bar.
|
71 |
if sub_bar is not None:
|
72 |
if sub_ticks < sub_tick_total:
|
73 |
sub_bar.update(sub_tick_total - sub_ticks)
|
|
|
76 |
overall_bar.refresh()
|
77 |
sub_bar = None
|
78 |
sub_ticks = 0
|
79 |
+
# Enter video phase.
|
80 |
current = int(progress_match.group(2))
|
81 |
total = int(progress_match.group(3))
|
82 |
if video_progress_bar is None:
|
|
|
84 |
ncols=120, dynamic_ncols=True, leave=True)
|
85 |
video_progress_bar.update(current - video_progress_bar.n)
|
86 |
video_progress_bar.refresh()
|
87 |
+
# When video generation completes, update overall bar.
|
88 |
if video_progress_bar.n >= video_progress_bar.total:
|
|
|
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(stripped_line)
|
100 |
|
101 |
+
# For the first 4 INFO messages, we simply increment processed_steps.
|
102 |
if processed_steps < irrelevant_steps:
|
103 |
processed_steps += 1
|
104 |
+
# If we're waiting for the first relevant step, start a waiting sub-bar if not already started.
|
105 |
+
if waiting_for_first_relevant and sub_bar is None:
|
106 |
+
sub_bar = tqdm(total=sub_tick_total, desc="Waiting for first step...", position=2,
|
107 |
+
ncols=120, dynamic_ncols=False, leave=True)
|
108 |
+
sub_ticks = 0
|
109 |
+
# Continue reading logs.
|
110 |
+
continue
|
111 |
else:
|
112 |
+
# Now we are in the relevant phase.
|
113 |
+
waiting_for_first_relevant = False
|
114 |
+
# If a sub-bar exists (either waiting or from a previous step), finish it.
|
|
|
115 |
if sub_bar is not None:
|
116 |
if sub_ticks < sub_tick_total:
|
117 |
sub_bar.update(sub_tick_total - sub_ticks)
|
|
|
120 |
overall_bar.refresh()
|
121 |
sub_bar = None
|
122 |
sub_ticks = 0
|
123 |
+
# Start a new sub-step bar with the current INFO message.
|
124 |
sub_bar = tqdm(total=sub_tick_total, desc=msg, position=2,
|
125 |
ncols=120, dynamic_ncols=False, leave=True)
|
126 |
sub_ticks = 0
|
|
|
128 |
else:
|
129 |
print(stripped_line)
|
130 |
else:
|
131 |
+
# No new data within 40ms.
|
132 |
+
# If a sub-bar is active, update it.
|
133 |
if sub_bar is not None:
|
134 |
sub_bar.update(1)
|
135 |
sub_ticks += 1
|
136 |
sub_bar.refresh()
|
137 |
if sub_ticks >= sub_tick_total:
|
138 |
+
# 20 seconds have elapsed; finish this sub-step.
|
139 |
sub_bar.close()
|
140 |
overall_bar.update(1)
|
141 |
overall_bar.refresh()
|
|
|
145 |
if process.poll() is not None:
|
146 |
break
|
147 |
|
148 |
+
# Drain remaining output.
|
149 |
for line in process.stdout:
|
150 |
print(line.strip())
|
151 |
process.wait()
|