Shubhvedi commited on
Commit
b2c25b2
·
verified ·
1 Parent(s): 2a7ff8a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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...")
10
+ try:
11
+ subprocess.run(
12
+ ["git", "clone", "https://github.com/microsoft/VibeVoice.git"],
13
+ check=True,
14
+ capture_output=True,
15
+ text=True
16
+ )
17
+ print("Repository cloned successfully.")
18
+ except subprocess.CalledProcessError as e:
19
+ print(f"Error cloning repository: {e.stderr}")
20
+ sys.exit(1)
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(
31
+ [sys.executable, "-m", "pip", "install", "-e", "."],
32
+ check=True,
33
+ capture_output=True,
34
+ text=True
35
+ )
36
+ print("Package installed successfully.")
37
+ except subprocess.CalledProcessError as e:
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"
89
+ ]
90
+
91
+ print(f"Launching Gradio demo with command: {' '.join(command)}")
92
+ # This command will start the Gradio server
93
+ subprocess.run(command)