Update app.py
Browse files
app.py
CHANGED
|
@@ -51,62 +51,45 @@ print(f"Reading {demo_script_path} to apply environment-specific modifications..
|
|
| 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,',
|
| 58 |
-
' torch_dtype=torch.bfloat16,',
|
| 59 |
-
" device_map='cuda',",
|
| 60 |
-
' attn_implementation="flash_attention_2",',
|
| 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("
|
| 71 |
|
| 72 |
# Add 'import spaces' if it's not already there.
|
| 73 |
if "import spaces" not in modified_content:
|
| 74 |
modified_content = "import spaces\n" + modified_content
|
| 75 |
|
| 76 |
-
#
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
' self.model_path,',
|
| 80 |
-
' torch_dtype=torch.bfloat16,',
|
| 81 |
-
" device_map='cuda',",
|
| 82 |
-
' )'
|
| 83 |
-
]
|
| 84 |
-
replacement_model_block_gpu = "\n".join(replacement_model_lines_gpu)
|
| 85 |
|
| 86 |
-
#
|
| 87 |
replacement_method_signature_gpu = " @spaces.GPU(duration=120)\n" + original_method_signature
|
| 88 |
|
| 89 |
# --- Apply Patches for GPU ---
|
| 90 |
-
|
| 91 |
-
#
|
| 92 |
if original_method_signature in modified_content:
|
| 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...")
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
# New block for CPU: Use float32 and map to CPU.
|
| 111 |
replacement_model_lines_cpu = [
|
| 112 |
' self.model = VibeVoiceForConditionalGenerationInference.from_pretrained(',
|
|
@@ -122,7 +105,7 @@ try:
|
|
| 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
|
| 126 |
sys.exit(1)
|
| 127 |
|
| 128 |
# Write the dynamically modified content back to the demo file
|
|
|
|
| 51 |
try:
|
| 52 |
modified_content = demo_script_path.read_text()
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
if USE_ZEROGPU:
|
| 55 |
+
print("Configuring for ZeroGPU execution while keeping Flash Attention...")
|
| 56 |
|
| 57 |
# Add 'import spaces' if it's not already there.
|
| 58 |
if "import spaces" not in modified_content:
|
| 59 |
modified_content = "import spaces\n" + modified_content
|
| 60 |
|
| 61 |
+
# Define the generation method signature to add the decorator to.
|
| 62 |
+
# We target only the first line for robustness.
|
| 63 |
+
original_method_signature = " def generate_podcast_streaming(self,"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
# Define the replacement with the correctly indented decorator.
|
| 66 |
replacement_method_signature_gpu = " @spaces.GPU(duration=120)\n" + original_method_signature
|
| 67 |
|
| 68 |
# --- Apply Patches for GPU ---
|
| 69 |
+
# The only change needed is to add the decorator. We will NOT modify the
|
| 70 |
+
# from_pretrained call, leaving attn_implementation="flash_attention_2" in place.
|
| 71 |
if original_method_signature in modified_content:
|
| 72 |
modified_content = modified_content.replace(original_method_signature, replacement_method_signature_gpu)
|
| 73 |
print("Successfully applied GPU decorator to the generation method.")
|
| 74 |
+
print("Model loading block remains unchanged to explicitly use Flash Attention.")
|
| 75 |
else:
|
| 76 |
print("\033[91mError: Could not find the generation method signature to apply the GPU decorator.\033[0m")
|
| 77 |
sys.exit(1)
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
else: # Pure CPU execution
|
| 80 |
print("Modifying for pure CPU execution...")
|
| 81 |
|
| 82 |
+
# For the CPU path, we still need to replace the entire CUDA-specific block.
|
| 83 |
+
original_model_lines = [
|
| 84 |
+
' self.model = VibeVoiceForConditionalGenerationInference.from_pretrained(',
|
| 85 |
+
' self.model_path,',
|
| 86 |
+
' torch_dtype=torch.bfloat16,',
|
| 87 |
+
" device_map='cuda',",
|
| 88 |
+
' attn_implementation="flash_attention_2",',
|
| 89 |
+
' )'
|
| 90 |
+
]
|
| 91 |
+
original_model_block = "\n".join(original_model_lines)
|
| 92 |
+
|
| 93 |
# New block for CPU: Use float32 and map to CPU.
|
| 94 |
replacement_model_lines_cpu = [
|
| 95 |
' self.model = VibeVoiceForConditionalGenerationInference.from_pretrained(',
|
|
|
|
| 105 |
modified_content = modified_content.replace(original_model_block, replacement_model_block_cpu)
|
| 106 |
print("Script modified for CPU successfully.")
|
| 107 |
else:
|
| 108 |
+
print("\033[91mError: The original model loading block was not found for CPU patching.\033[0m")
|
| 109 |
sys.exit(1)
|
| 110 |
|
| 111 |
# Write the dynamically modified content back to the demo file
|