fffiloni commited on
Commit
ae36822
·
verified ·
1 Parent(s): 189dec4

Update simple_app.py

Browse files
Files changed (1) hide show
  1. simple_app.py +33 -35
simple_app.py CHANGED
@@ -16,14 +16,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
 
21
- # Overall progress bar for the process steps.
22
  overall_bar = tqdm(total=relevant_steps, desc="Overall Process", position=1,
23
  ncols=120, dynamic_ncols=False, leave=True)
24
  processed_steps = 0
25
 
26
- # Regex for detecting video generation progress lines (e.g., "10%|...| 5/50")
27
  progress_pattern = re.compile(r"(\d+)%\|.*\| (\d+)/(\d+)")
28
  gen_progress_bar = None
29
 
@@ -34,40 +35,38 @@ def infer(prompt, progress=gr.Progress(track_tqdm=True)):
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",
@@ -90,7 +89,7 @@ def infer(prompt, progress=gr.Progress(track_tqdm=True)):
90
  if not stripped_line:
91
  continue
92
 
93
- # Check if this is a video generation progress line.
94
  progress_match = progress_pattern.search(stripped_line)
95
  if progress_match:
96
  current = int(progress_match.group(2))
@@ -104,38 +103,37 @@ def infer(prompt, progress=gr.Progress(track_tqdm=True)):
104
 
105
  # Check for INFO lines.
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()
 
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
 
 
35
  sub_lock = threading.Lock()
36
 
37
  def update_sub_bar(sub_bar, cancel_event):
38
+ # Update sub-bar once per second for up to 20 seconds,
39
+ # unless cancel_event is set.
40
  for i in range(20):
41
  if cancel_event.is_set():
42
  break
43
  time.sleep(1)
44
  sub_bar.update(1)
45
  sub_bar.refresh()
46
+ # (Closing and overall-bar update are handled externally.)
47
+
48
+ def cancel_sub_bar():
 
49
  nonlocal current_sub_bar, current_sub_thread, current_cancel_event
50
  with sub_lock:
51
+ if current_cancel_event is not None:
52
+ current_cancel_event.set()
53
+ if current_sub_thread is not None:
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", # using unbuffered mode
70
  "--task", "t2v-1.3B",
71
  "--size", "832*480",
72
  "--ckpt_dir", "./Wan2.1-T2V-1.3B",
 
89
  if not stripped_line:
90
  continue
91
 
92
+ # Check for video generation progress lines.
93
  progress_match = progress_pattern.search(stripped_line)
94
  if progress_match:
95
  current = int(progress_match.group(2))
 
103
 
104
  # Check for INFO lines.
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) # Log the full line
110
 
111
  if processed_steps < irrelevant_steps:
112
  processed_steps += 1
113
  else:
114
  with sub_lock:
115
+ # If a sub-bar is active, cancel it immediately.
116
  if current_sub_bar is not None:
117
+ cancel_sub_bar()
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
+ current_sub_thread = threading.Thread(target=update_sub_bar, args=(current_sub_bar, current_cancel_event))
123
+ current_sub_thread.daemon = True
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
  with sub_lock:
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()