broadfield-dev commited on
Commit
4324db0
·
verified ·
1 Parent(s): c04543f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -37
app.py CHANGED
@@ -1,21 +1,9 @@
1
  import os
2
  import subprocess
3
  import sys
4
- import fileinput
5
- import gradio as gr
6
 
7
- # --- 1. Setup Environment ---
8
- # Install ffmpeg if it's not already installed by packages.txt
9
- print("Updating apt and installing ffmpeg...")
10
- try:
11
- subprocess.run(["apt-get", "update", "-y"], check=True)
12
- subprocess.run(["apt-get", "install", "ffmpeg", "-y"], check=True)
13
- print("ffmpeg installed successfully.")
14
- except subprocess.CalledProcessError as e:
15
- print(f"Error installing ffmpeg: {e.stderr}")
16
- # Continue anyway, as it might be pre-installed
17
-
18
- # --- 2. Clone the VibeVoice Repository ---
19
  repo_dir = "VibeVoice"
20
  if not os.path.exists(repo_dir):
21
  print("Cloning the VibeVoice repository...")
@@ -33,9 +21,10 @@ if not os.path.exists(repo_dir):
33
  else:
34
  print("Repository already exists. Skipping clone.")
35
 
36
- # --- 3. Install the Package ---
37
  os.chdir(repo_dir)
38
  print(f"Changed directory to: {os.getcwd()}")
 
39
  print("Installing the VibeVoice package...")
40
  try:
41
  subprocess.run(
@@ -49,38 +38,51 @@ except subprocess.CalledProcessError as e:
49
  print(f"Error installing package: {e.stderr}")
50
  sys.exit(1)
51
 
52
-
53
- # --- 4. Modify the demo script for CPU execution ---
54
- demo_script_path = "demo/gradio_demo.py"
55
  print(f"Modifying {demo_script_path} for CPU execution...")
56
 
57
- # Use fileinput to perform in-place replacement
58
- with fileinput.FileInput(demo_script_path, inplace=True) as file:
59
- for line in file:
60
- # Change the model loading line to be CPU-compatible
61
- if 'VibeVoiceForConditionalGenerationInference.from_pretrained(' in line:
62
- print(' self.model = VibeVoiceForConditionalGenerationInference.from_pretrained(')
63
- print(' self.model_path,')
64
- print(' torch_dtype=torch.float32, # Use float32 for CPU')
65
- print(' device_map="cpu",')
66
- print(' )')
67
- # Skip the next few lines of the original arguments
68
- next(file, None)
69
- next(file, None)
70
- next(file, None)
71
- else:
72
- print(line, end='')
 
 
73
 
74
- print("Script modified successfully.")
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
 
77
- # --- 5. Launch the Gradio Demo ---
78
  model_id = "microsoft/VibeVoice-1.5B"
79
 
80
  # Construct the command as specified in the README
81
  command = [
82
  "python",
83
- demo_script_path,
84
  "--model_path",
85
  model_id,
86
  "--share"
 
1
  import os
2
  import subprocess
3
  import sys
4
+ from pathlib import Path
 
5
 
6
+ # --- 1. Clone the VibeVoice Repository ---
 
 
 
 
 
 
 
 
 
 
 
7
  repo_dir = "VibeVoice"
8
  if not os.path.exists(repo_dir):
9
  print("Cloning the VibeVoice repository...")
 
21
  else:
22
  print("Repository already exists. Skipping clone.")
23
 
24
+ # --- 2. Install the Package ---
25
  os.chdir(repo_dir)
26
  print(f"Changed directory to: {os.getcwd()}")
27
+
28
  print("Installing the VibeVoice package...")
29
  try:
30
  subprocess.run(
 
38
  print(f"Error installing package: {e.stderr}")
39
  sys.exit(1)
40
 
41
+ # --- 3. Modify the demo script for CPU execution (Robust Method) ---
42
+ demo_script_path = Path("demo/gradio_demo.py")
 
43
  print(f"Modifying {demo_script_path} for CPU execution...")
44
 
45
+ try:
46
+ # Read the entire file content
47
+ file_content = demo_script_path.read_text()
48
+
49
+ # Define the original GPU-specific model loading block
50
+ original_block = """ self.model = VibeVoiceForConditionalGenerationInference.from_pretrained(
51
+ self.model_path,
52
+ torch_dtype=torch.bfloat16,
53
+ device_map='cuda',
54
+ attn_implementation="flash_attention_2",
55
+ )"""
56
+
57
+ # Define the new CPU-compatible block
58
+ replacement_block = """ self.model = VibeVoiceForConditionalGenerationInference.from_pretrained(
59
+ self.model_path,
60
+ torch_dtype=torch.float32, # Use float32 for CPU
61
+ device_map="cpu",
62
+ )"""
63
 
64
+ # Replace the entire block
65
+ if original_block in file_content:
66
+ modified_content = file_content.replace(original_block, replacement_block)
67
+
68
+ # Write the modified content back to the file
69
+ demo_script_path.write_text(modified_content)
70
+ print("Script modified successfully.")
71
+ else:
72
+ print("Warning: GPU-specific model loading block not found. The script might have been updated. Proceeding without modification.")
73
+
74
+ except Exception as e:
75
+ print(f"An error occurred while modifying the script: {e}")
76
+ sys.exit(1)
77
 
78
 
79
+ # --- 4. Launch the Gradio Demo ---
80
  model_id = "microsoft/VibeVoice-1.5B"
81
 
82
  # Construct the command as specified in the README
83
  command = [
84
  "python",
85
+ str(demo_script_path),
86
  "--model_path",
87
  model_id,
88
  "--share"