broadfield-dev commited on
Commit
8a1f431
·
verified ·
1 Parent(s): 7dc8d63

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -31
app.py CHANGED
@@ -26,12 +26,12 @@ if not os.path.exists(repo_dir):
26
  else:
27
  print("Repository already exists. Skipping clone.")
28
 
29
- # --- 2. Install Dependencies ---
 
30
  os.chdir(repo_dir)
31
  print(f"Changed directory to: {os.getcwd()}")
32
 
33
- # Install the main package
34
- print("Installing the VibeVoice package...")
35
  try:
36
  subprocess.run(
37
  [sys.executable, "-m", "pip", "install", "-e", "."],
@@ -44,30 +44,15 @@ except subprocess.CalledProcessError as e:
44
  print(f"Error installing package: {e.stderr}")
45
  sys.exit(1)
46
 
47
- # Install 'spaces' if using ZeroGPU, as it's required for the decorator
48
- if USE_ZEROGPU:
49
- print("Installing the 'spaces' library for ZeroGPU...")
50
- try:
51
- subprocess.run(
52
- [sys.executable, "-m", "pip", "install", "spaces"],
53
- check=True,
54
- capture_output=True,
55
- text=True
56
- )
57
- print("'spaces' library installed successfully.")
58
- except subprocess.CalledProcessError as e:
59
- print(f"Error installing 'spaces' library: {e.stderr}")
60
- sys.exit(1)
61
-
62
- # --- 3. Modify the demo script based on the toggle ---
63
  demo_script_path = Path("demo/gradio_demo.py")
64
- print(f"Reading {demo_script_path}...")
65
 
66
  try:
67
  file_content = demo_script_path.read_text()
68
 
69
- # Define the original GPU-specific model loading block we want to replace
70
- # This block is problematic because it hardcodes FlashAttention
71
  original_block = """ self.model = VibeVoiceForConditionalGenerationInference.from_pretrained(
72
  self.model_path,
73
  torch_dtype=torch.bfloat16,
@@ -78,18 +63,18 @@ try:
78
  if USE_ZEROGPU:
79
  print("Optimizing for ZeroGPU execution...")
80
 
81
- # New block for ZeroGPU: We remove the problematic flash_attention line.
82
- # Transformers will automatically use the best available attention mechanism.
83
  replacement_block_gpu = """ self.model = VibeVoiceForConditionalGenerationInference.from_pretrained(
84
  self.model_path,
85
  torch_dtype=torch.bfloat16,
86
  device_map='cuda',
87
  )"""
88
 
89
- # Add 'import spaces' at the beginning of the file
90
  modified_content = "import spaces\n" + file_content
91
 
92
- # Decorate the main class with @spaces.GPU to request a GPU
93
  modified_content = modified_content.replace(
94
  "class VibeVoiceGradioInterface:",
95
  "@spaces.GPU(duration=120)\nclass VibeVoiceGradioInterface:"
@@ -102,19 +87,18 @@ try:
102
  else: # Pure CPU execution
103
  print("Modifying for pure CPU execution...")
104
 
105
- # New block for CPU: Use float32 and map directly to CPU.
106
- # FlashAttention is not compatible with CPU.
107
  replacement_block_cpu = """ self.model = VibeVoiceForConditionalGenerationInference.from_pretrained(
108
  self.model_path,
109
  torch_dtype=torch.float32, # Use float32 for CPU
110
  device_map="cpu",
111
  )"""
112
 
113
- # Replace the model loading block
114
  modified_content = file_content.replace(original_block, replacement_block_cpu)
115
  print("Script modified for CPU successfully.")
116
 
117
- # Write the modified content back to the file
118
  demo_script_path.write_text(modified_content)
119
 
120
  except Exception as e:
@@ -124,7 +108,7 @@ except Exception as e:
124
  # --- 4. Launch the Gradio Demo ---
125
  model_id = "microsoft/VibeVoice-1.5B"
126
 
127
- # Construct the command as specified in the README
128
  command = [
129
  "python",
130
  str(demo_script_path),
 
26
  else:
27
  print("Repository already exists. Skipping clone.")
28
 
29
+ # --- 2. Install the VibeVoice Package ---
30
+ # Note: Other dependencies are installed via requirements.txt
31
  os.chdir(repo_dir)
32
  print(f"Changed directory to: {os.getcwd()}")
33
 
34
+ print("Installing the VibeVoice package in editable mode...")
 
35
  try:
36
  subprocess.run(
37
  [sys.executable, "-m", "pip", "install", "-e", "."],
 
44
  print(f"Error installing package: {e.stderr}")
45
  sys.exit(1)
46
 
47
+ # --- 3. Modify the demo script to be environment-aware ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  demo_script_path = Path("demo/gradio_demo.py")
49
+ print(f"Reading {demo_script_path} to apply environment-specific modifications...")
50
 
51
  try:
52
  file_content = demo_script_path.read_text()
53
 
54
+ # Define the original model loading block that we need to replace.
55
+ # This block is problematic because it hardcodes FlashAttention.
56
  original_block = """ self.model = VibeVoiceForConditionalGenerationInference.from_pretrained(
57
  self.model_path,
58
  torch_dtype=torch.bfloat16,
 
63
  if USE_ZEROGPU:
64
  print("Optimizing for ZeroGPU execution...")
65
 
66
+ # New block for ZeroGPU: We remove the problematic `attn_implementation` line.
67
+ # `transformers` will automatically use the best available attention mechanism.
68
  replacement_block_gpu = """ self.model = VibeVoiceForConditionalGenerationInference.from_pretrained(
69
  self.model_path,
70
  torch_dtype=torch.bfloat16,
71
  device_map='cuda',
72
  )"""
73
 
74
+ # Add 'import spaces' at the beginning of the file for the @spaces.GPU decorator
75
  modified_content = "import spaces\n" + file_content
76
 
77
+ # Decorate the main interface class to request a GPU from the Spaces infrastructure
78
  modified_content = modified_content.replace(
79
  "class VibeVoiceGradioInterface:",
80
  "@spaces.GPU(duration=120)\nclass VibeVoiceGradioInterface:"
 
87
  else: # Pure CPU execution
88
  print("Modifying for pure CPU execution...")
89
 
90
+ # New block for CPU: Use float32 and map directly to the CPU.
 
91
  replacement_block_cpu = """ self.model = VibeVoiceForConditionalGenerationInference.from_pretrained(
92
  self.model_path,
93
  torch_dtype=torch.float32, # Use float32 for CPU
94
  device_map="cpu",
95
  )"""
96
 
97
+ # Replace the original model loading block with the CPU version
98
  modified_content = file_content.replace(original_block, replacement_block_cpu)
99
  print("Script modified for CPU successfully.")
100
 
101
+ # Write the dynamically modified content back to the demo file
102
  demo_script_path.write_text(modified_content)
103
 
104
  except Exception as e:
 
108
  # --- 4. Launch the Gradio Demo ---
109
  model_id = "microsoft/VibeVoice-1.5B"
110
 
111
+ # Construct the command to run the modified demo script
112
  command = [
113
  "python",
114
  str(demo_script_path),