fffiloni commited on
Commit
3893fde
·
verified ·
1 Parent(s): 4c0cc6b

Update simple_app.py

Browse files
Files changed (1) hide show
  1. simple_app.py +43 -28
simple_app.py CHANGED
@@ -1,6 +1,8 @@
1
  import gradio as gr
2
  import re
3
  import subprocess
 
 
4
  from tqdm import tqdm
5
  from huggingface_hub import snapshot_download
6
 
@@ -16,17 +18,30 @@ def infer(prompt, progress=gr.Progress(track_tqdm=True)):
16
  irrelevant_steps = 4
17
  relevant_steps = total_process_steps - irrelevant_steps # 7 steps
18
 
19
- # Create the overall progress bar for the process steps.
20
- overall_bar = tqdm(total=relevant_steps, desc="Overall Process", position=1, dynamic_ncols=True, leave=True)
21
- processed_steps = 0
22
-
23
- # Regex for detecting video generation progress lines.
24
  progress_pattern = re.compile(r"(\d+)%\|.*\| (\d+)/(\d+)")
25
  gen_progress_bar = None
26
 
27
- # We'll maintain a persistent sub-progress bar for the current step.
28
  current_sub_bar = None
29
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  command = [
31
  "python", "-u", "-m", "generate", # using -u for unbuffered output and omitting .py extension
32
  "--task", "t2v-1.3B",
@@ -59,43 +74,43 @@ def infer(prompt, progress=gr.Progress(track_tqdm=True)):
59
  total = int(progress_match.group(3))
60
  if gen_progress_bar is None:
61
  gen_progress_bar = tqdm(total=total, desc="Video Generation", position=0,
62
- dynamic_ncols=True, leave=True)
63
  gen_progress_bar.update(current - gen_progress_bar.n)
64
  gen_progress_bar.refresh()
65
  continue
66
 
67
  # Check for INFO lines.
68
  if "INFO:" in stripped_line:
69
- # Extract the INFO message.
70
  parts = stripped_line.split("INFO:", 1)
71
  msg = parts[1].strip() if len(parts) > 1 else ""
72
- tqdm.write(stripped_line) # print the log line
73
 
74
- # Skip the first few irrelevant INFO lines.
75
  if processed_steps < irrelevant_steps:
76
  processed_steps += 1
77
  else:
78
- # If there's a current sub-progress bar, mark it complete and close it.
79
- if current_sub_bar is not None:
80
- current_sub_bar.update(1)
81
- current_sub_bar.close()
82
- overall_bar.update(1)
83
- overall_bar.refresh()
84
- # Now create a new sub-progress bar for this new step.
85
- # (It will remain visible until the next INFO message arrives.)
86
- current_sub_bar = tqdm(total=1, desc=msg, position=2,
87
- ncols=120, dynamic_ncols=False, leave=True)
 
 
 
 
88
  else:
89
  tqdm.write(stripped_line)
90
 
91
- # When process ends, if there's an open sub-bar, finish it.
92
- if current_sub_bar is not None:
93
- current_sub_bar.update(1)
94
- current_sub_bar.close()
95
- overall_bar.update(1)
96
- overall_bar.refresh()
97
-
98
  process.wait()
 
 
 
 
 
99
  if gen_progress_bar:
100
  gen_progress_bar.close()
101
  overall_bar.close()
 
1
  import gradio as gr
2
  import re
3
  import subprocess
4
+ import time
5
+ import threading
6
  from tqdm import tqdm
7
  from huggingface_hub import snapshot_download
8
 
 
18
  irrelevant_steps = 4
19
  relevant_steps = total_process_steps - irrelevant_steps # 7 steps
20
 
21
+ # Regex for detecting video generation progress lines (e.g., "10%|...| 5/50")
 
 
 
 
22
  progress_pattern = re.compile(r"(\d+)%\|.*\| (\d+)/(\d+)")
23
  gen_progress_bar = None
24
 
25
+ # Variables for managing the sub-progress bar for each step.
26
  current_sub_bar = None
27
+ current_timer = None
28
+ sub_lock = threading.Lock()
29
+
30
+ def close_sub_bar():
31
+ nonlocal current_sub_bar, current_timer, overall_bar
32
+ with sub_lock:
33
+ if current_sub_bar is not None:
34
+ try:
35
+ # Ensure the sub-bar is complete.
36
+ current_sub_bar.update(1 - current_sub_bar.n)
37
+ except Exception:
38
+ pass
39
+ current_sub_bar.close()
40
+ overall_bar.update(1)
41
+ overall_bar.refresh()
42
+ current_sub_bar = None
43
+ current_timer = None
44
+
45
  command = [
46
  "python", "-u", "-m", "generate", # using -u for unbuffered output and omitting .py extension
47
  "--task", "t2v-1.3B",
 
74
  total = int(progress_match.group(3))
75
  if gen_progress_bar is None:
76
  gen_progress_bar = tqdm(total=total, desc="Video Generation", position=0,
77
+ ncols=120, dynamic_ncols=True, leave=True)
78
  gen_progress_bar.update(current - gen_progress_bar.n)
79
  gen_progress_bar.refresh()
80
  continue
81
 
82
  # Check for INFO lines.
83
  if "INFO:" in stripped_line:
 
84
  parts = stripped_line.split("INFO:", 1)
85
  msg = parts[1].strip() if len(parts) > 1 else ""
86
+ tqdm.write(stripped_line) # Print the log line
87
 
 
88
  if processed_steps < irrelevant_steps:
89
  processed_steps += 1
90
  else:
91
+ with sub_lock:
92
+ # If a sub-bar is active, cancel its timer and close it immediately.
93
+ if current_sub_bar is not None:
94
+ if current_timer is not None:
95
+ current_timer.cancel()
96
+ close_sub_bar()
97
+ # Create a new sub-bar for the current step.
98
+ current_sub_bar = tqdm(total=1, desc=msg, position=2,
99
+ ncols=120, dynamic_ncols=False, leave=True)
100
+ # Start a timer to automatically close this sub-bar after 20 seconds.
101
+ current_timer = threading.Timer(20, close_sub_bar)
102
+ current_timer.start()
103
+ continue
104
+
105
  else:
106
  tqdm.write(stripped_line)
107
 
 
 
 
 
 
 
 
108
  process.wait()
109
+ # Clean up: if a sub-bar is still active, close it.
110
+ if current_timer is not None:
111
+ current_timer.cancel()
112
+ if current_sub_bar is not None:
113
+ close_sub_bar()
114
  if gen_progress_bar:
115
  gen_progress_bar.close()
116
  overall_bar.close()