Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,9 @@
|
|
1 |
import os
|
2 |
import subprocess
|
3 |
import sys
|
4 |
-
import
|
5 |
-
import gradio as gr
|
6 |
|
7 |
-
# --- 1.
|
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 |
-
# ---
|
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 |
-
|
54 |
-
demo_script_path = "demo/gradio_demo.py"
|
55 |
print(f"Modifying {demo_script_path} for CPU execution...")
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
|
|
73 |
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
|
77 |
-
# ---
|
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"
|