broadfield-dev commited on
Commit
88cdeea
·
verified ·
1 Parent(s): 2877f43

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -9
app.py CHANGED
@@ -34,7 +34,7 @@ except subprocess.CalledProcessError as e:
34
  print(f"Error installing package: {e.stderr}")
35
  sys.exit(1)
36
 
37
- # --- 3. Refactor the demo script using a robust line-by-line patch ---
38
  demo_script_path = Path("demo/gradio_demo.py")
39
  print(f"Refactoring {demo_script_path} for ZeroGPU lazy loading...")
40
 
@@ -63,13 +63,16 @@ try:
63
  print("Model and processor loaded successfully on GPU worker.")
64
  """
65
 
66
- # --- Perform the line-by-line modifications ---
67
  new_lines = []
68
  # Add 'import spaces' at the top if it doesn't exist
69
  if not any("import spaces" in line for line in lines):
70
  new_lines.append("import spaces\n")
71
 
72
- patched = False
 
 
 
73
  for line in lines:
74
  # Defer the initial model loading to prevent PicklingError
75
  if "self.load_model()" in line and "def __init__" in "".join(lines[lines.index(line)-2:lines.index(line)]):
@@ -77,21 +80,30 @@ try:
77
  new_lines.append(" self.model = None\n")
78
  new_lines.append(" self.processor = None\n")
79
  print("Successfully patched __init__ to prevent startup model load.")
80
- # Find the generation method to add the decorator and lazy-loading logic
81
- elif "def generate_podcast_streaming(self," in line:
 
82
  new_lines.append(" @spaces.GPU(duration=120)\n")
83
  new_lines.append(line)
84
- elif "-> Iterator[tuple]:" in line and "generate_podcast_streaming" in new_lines[-1]:
 
 
 
85
  new_lines.append(line)
86
- # Indent the lazy load code correctly
87
  for code_line in lazy_load_code.strip().split('\n'):
88
  new_lines.append(' ' * 8 + code_line + '\n')
89
- patched = True
 
 
 
90
  print("Successfully patched generation method for lazy loading.")
 
 
91
  else:
92
  new_lines.append(line)
93
 
94
- if not patched:
95
  print("\033[91mError: Failed to apply the lazy-loading patch. The target method signature may have changed.\033[0m")
96
  sys.exit(1)
97
 
 
34
  print(f"Error installing package: {e.stderr}")
35
  sys.exit(1)
36
 
37
+ # --- 3. Refactor the demo script using a robust state-machine patcher ---
38
  demo_script_path = Path("demo/gradio_demo.py")
39
  print(f"Refactoring {demo_script_path} for ZeroGPU lazy loading...")
40
 
 
63
  print("Model and processor loaded successfully on GPU worker.")
64
  """
65
 
66
+ # --- Perform the line-by-line modifications using a state machine ---
67
  new_lines = []
68
  # Add 'import spaces' at the top if it doesn't exist
69
  if not any("import spaces" in line for line in lines):
70
  new_lines.append("import spaces\n")
71
 
72
+ # State machine variables
73
+ in_generate_method = False
74
+ patched_generate_method = False
75
+
76
  for line in lines:
77
  # Defer the initial model loading to prevent PicklingError
78
  if "self.load_model()" in line and "def __init__" in "".join(lines[lines.index(line)-2:lines.index(line)]):
 
80
  new_lines.append(" self.model = None\n")
81
  new_lines.append(" self.processor = None\n")
82
  print("Successfully patched __init__ to prevent startup model load.")
83
+
84
+ # Start of the target method
85
+ elif "def generate_podcast_streaming(self," in line and not patched_generate_method:
86
  new_lines.append(" @spaces.GPU(duration=120)\n")
87
  new_lines.append(line)
88
+ in_generate_method = True
89
+
90
+ # End of the target method signature
91
+ elif "-> Iterator[tuple]:" in line and in_generate_method:
92
  new_lines.append(line)
93
+ # Indent and insert the lazy load code
94
  for code_line in lazy_load_code.strip().split('\n'):
95
  new_lines.append(' ' * 8 + code_line + '\n')
96
+
97
+ # Reset state
98
+ in_generate_method = False
99
+ patched_generate_method = True
100
  print("Successfully patched generation method for lazy loading.")
101
+
102
+ # All other lines
103
  else:
104
  new_lines.append(line)
105
 
106
+ if not patched_generate_method:
107
  print("\033[91mError: Failed to apply the lazy-loading patch. The target method signature may have changed.\033[0m")
108
  sys.exit(1)
109