broadfield-dev commited on
Commit
6d6254a
·
verified ·
1 Parent(s): 6a0b1a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -23
app.py CHANGED
@@ -51,9 +51,7 @@ print(f"Reading {demo_script_path} to apply environment-specific modifications..
51
  try:
52
  modified_content = demo_script_path.read_text()
53
 
54
- # --- Patch Definitions ---
55
-
56
- # Define the original model loading block to be replaced.
57
  original_model_lines = [
58
  ' self.model = VibeVoiceForConditionalGenerationInference.from_pretrained(',
59
  ' self.model_path,',
@@ -63,19 +61,10 @@ try:
63
  ' )'
64
  ]
65
  original_model_block = "\n".join(original_model_lines)
66
-
67
- # Define the generation method signature to add the GPU decorator to.
68
- original_method_lines = [
69
- ' def generate_podcast_streaming(self, ',
70
- ' num_speakers: int,',
71
- ' script: str,',
72
- ' speaker_1: str = None,',
73
- ' speaker_2: str = None,',
74
- ' speaker_3: str = None,',
75
- ' speaker_4: str = None,',
76
- ' cfg_scale: float = 1.3) -> Iterator[tuple]:'
77
- ]
78
- original_method_signature = "\n".join(original_method_lines)
79
 
80
  if USE_ZEROGPU:
81
  print("Optimizing for ZeroGPU execution...")
@@ -94,8 +83,8 @@ try:
94
  ]
95
  replacement_model_block_gpu = "\n".join(replacement_model_lines_gpu)
96
 
97
- # Add the @spaces.GPU decorator to the generation method instead of the class.
98
- replacement_method_signature_gpu = "@spaces.GPU(duration=120)\n" + original_method_signature
99
 
100
  # --- Apply Patches for GPU ---
101
 
@@ -104,14 +93,16 @@ try:
104
  modified_content = modified_content.replace(original_method_signature, replacement_method_signature_gpu)
105
  print("Successfully applied GPU decorator to the generation method.")
106
  else:
107
- print("\033[91mWarning: Could not find the generation method signature to apply the GPU decorator.\033[0m")
 
108
 
109
  # Patch 2: Modify the model loading
110
  if original_model_block in modified_content:
111
  modified_content = modified_content.replace(original_model_block, replacement_model_block_gpu)
112
  print("Successfully patched the model loading block for ZeroGPU.")
113
  else:
114
- print("\033[91mWarning: The original model loading block was not found. Patching may have failed.\033[0m")
 
115
 
116
  else: # Pure CPU execution
117
  print("Modifying for pure CPU execution...")
@@ -131,8 +122,8 @@ try:
131
  modified_content = modified_content.replace(original_model_block, replacement_model_block_cpu)
132
  print("Script modified for CPU successfully.")
133
  else:
134
- print("\033[91mWarning: The original model loading block was not found. Patching may have failed.\033[0m")
135
-
136
 
137
  # Write the dynamically modified content back to the demo file
138
  demo_script_path.write_text(modified_content)
@@ -142,7 +133,7 @@ except Exception as e:
142
  sys.exit(1)
143
 
144
  # --- 4. Launch the Gradio Demo ---
145
- model_id = "microsoft/V_VibeVoice-1.5B"
146
 
147
  # Construct the command to run the modified demo script
148
  command = [
 
51
  try:
52
  modified_content = demo_script_path.read_text()
53
 
54
+ # Define the original model loading block using a list of lines for robustness.
 
 
55
  original_model_lines = [
56
  ' self.model = VibeVoiceForConditionalGenerationInference.from_pretrained(',
57
  ' self.model_path,',
 
61
  ' )'
62
  ]
63
  original_model_block = "\n".join(original_model_lines)
64
+
65
+ # More robustly define the generation method signature to patch.
66
+ # We only need the first line to find our target.
67
+ original_method_signature = " def generate_podcast_streaming(self,"
 
 
 
 
 
 
 
 
 
68
 
69
  if USE_ZEROGPU:
70
  print("Optimizing for ZeroGPU execution...")
 
83
  ]
84
  replacement_model_block_gpu = "\n".join(replacement_model_lines_gpu)
85
 
86
+ # Add the @spaces.GPU decorator *with correct indentation* before the method.
87
+ replacement_method_signature_gpu = " @spaces.GPU(duration=120)\n" + original_method_signature
88
 
89
  # --- Apply Patches for GPU ---
90
 
 
93
  modified_content = modified_content.replace(original_method_signature, replacement_method_signature_gpu)
94
  print("Successfully applied GPU decorator to the generation method.")
95
  else:
96
+ print("\033[91mError: Could not find the generation method signature to apply the GPU decorator.\033[0m")
97
+ sys.exit(1)
98
 
99
  # Patch 2: Modify the model loading
100
  if original_model_block in modified_content:
101
  modified_content = modified_content.replace(original_model_block, replacement_model_block_gpu)
102
  print("Successfully patched the model loading block for ZeroGPU.")
103
  else:
104
+ print("\033[91mError: The original model loading block was not found. Patching may have failed.\033[0m")
105
+ sys.exit(1)
106
 
107
  else: # Pure CPU execution
108
  print("Modifying for pure CPU execution...")
 
122
  modified_content = modified_content.replace(original_model_block, replacement_model_block_cpu)
123
  print("Script modified for CPU successfully.")
124
  else:
125
+ print("\033[91mError: The original model loading block was not found. Patching may have failed.\03-3[0m")
126
+ sys.exit(1)
127
 
128
  # Write the dynamically modified content back to the demo file
129
  demo_script_path.write_text(modified_content)
 
133
  sys.exit(1)
134
 
135
  # --- 4. Launch the Gradio Demo ---
136
+ model_id = "microsoft/VibeVoice-1.5B"
137
 
138
  # Construct the command to run the modified demo script
139
  command = [