Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import os
|
|
| 2 |
import subprocess
|
| 3 |
import torch
|
| 4 |
import gradio as gr
|
| 5 |
-
from transformers import AutoModelForTextToSpeech, AutoTokenizer, AutoConfig
|
| 6 |
import scipy.io.wavfile
|
| 7 |
import numpy as np
|
| 8 |
from datetime import datetime
|
|
@@ -14,14 +13,21 @@ logger = logging.getLogger(__name__)
|
|
| 14 |
|
| 15 |
# Function to install dependencies
|
| 16 |
def install_dependencies():
|
| 17 |
-
"""Install required dependencies
|
| 18 |
try:
|
| 19 |
-
# Install
|
| 20 |
subprocess.run(["pip", "install", "-r", "requirements.txt"], check=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
# Install flash-attn only if GPU is available
|
| 22 |
if torch.cuda.is_available():
|
| 23 |
subprocess.run(["pip", "install", "flash-attn==2.6.3", "--no-build-isolation"], check=True)
|
| 24 |
-
|
|
|
|
| 25 |
if os.name == "posix":
|
| 26 |
subprocess.run(["apt", "update"], check=True)
|
| 27 |
subprocess.run(["apt", "install", "ffmpeg", "-y"], check=True)
|
|
@@ -31,6 +37,20 @@ def install_dependencies():
|
|
| 31 |
logger.error(f"Failed to install dependencies: {e}")
|
| 32 |
raise
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# Determine device and attention implementation
|
| 35 |
def get_device():
|
| 36 |
"""Determine the device (CPU/GPU) and attention implementation."""
|
|
@@ -43,11 +63,13 @@ def get_device():
|
|
| 43 |
# Load model and tokenizer
|
| 44 |
def load_model(model_name="microsoft/VibeVoice-1.5B"):
|
| 45 |
"""Load VibeVoice model and tokenizer with memory optimization."""
|
|
|
|
|
|
|
| 46 |
device, attn_impl = get_device()
|
| 47 |
try:
|
| 48 |
# Load config with specified attention implementation
|
| 49 |
config = AutoConfig.from_pretrained(model_name, attn_implementation=attn_impl)
|
| 50 |
-
# Enable memory-efficient loading
|
| 51 |
model = AutoModelForTextToSpeech.from_pretrained(
|
| 52 |
model_name, config=config, torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
| 53 |
)
|
|
@@ -82,14 +104,14 @@ def generate_audio(text_input, speaker_names, model, tokenizer, device, output_p
|
|
| 82 |
# Generate audio with memory management
|
| 83 |
with torch.no_grad():
|
| 84 |
if device == "cuda":
|
| 85 |
-
with torch.cuda.amp.autocast():
|
| 86 |
audio_output = model.generate(**inputs)
|
| 87 |
else:
|
| 88 |
audio_output = model.generate(**inputs)
|
| 89 |
|
| 90 |
# Convert to numpy array
|
| 91 |
audio_np = audio_output.cpu().numpy().squeeze()
|
| 92 |
-
if audio_np.ndim > 1:
|
| 93 |
audio_np = audio_np[0]
|
| 94 |
|
| 95 |
# Normalize audio to [-1, 1]
|
|
@@ -121,8 +143,9 @@ def gradio_interface(text_input, speakers_str):
|
|
| 121 |
except ValueError as e:
|
| 122 |
return str(e)
|
| 123 |
|
| 124 |
-
# Global model and tokenizer
|
| 125 |
try:
|
|
|
|
| 126 |
model, tokenizer, device = load_model()
|
| 127 |
except Exception as e:
|
| 128 |
logger.error(f"Failed to initialize: {e}")
|
|
|
|
| 2 |
import subprocess
|
| 3 |
import torch
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
import scipy.io.wavfile
|
| 6 |
import numpy as np
|
| 7 |
from datetime import datetime
|
|
|
|
| 13 |
|
| 14 |
# Function to install dependencies
|
| 15 |
def install_dependencies():
|
| 16 |
+
"""Install required dependencies, including Microsoft's custom transformers fork."""
|
| 17 |
try:
|
| 18 |
+
# Install core dependencies from requirements.txt
|
| 19 |
subprocess.run(["pip", "install", "-r", "requirements.txt"], check=True)
|
| 20 |
+
|
| 21 |
+
# Install Microsoft's custom transformers fork for VibeVoice
|
| 22 |
+
subprocess.run([
|
| 23 |
+
"pip", "install", "git+https://github.com/microsoft/VibeVoice.git#subdirectory=transformers"
|
| 24 |
+
], check=True)
|
| 25 |
+
|
| 26 |
# Install flash-attn only if GPU is available
|
| 27 |
if torch.cuda.is_available():
|
| 28 |
subprocess.run(["pip", "install", "flash-attn==2.6.3", "--no-build-isolation"], check=True)
|
| 29 |
+
|
| 30 |
+
# Install ffmpeg for audio processing
|
| 31 |
if os.name == "posix":
|
| 32 |
subprocess.run(["apt", "update"], check=True)
|
| 33 |
subprocess.run(["apt", "install", "ffmpeg", "-y"], check=True)
|
|
|
|
| 37 |
logger.error(f"Failed to install dependencies: {e}")
|
| 38 |
raise
|
| 39 |
|
| 40 |
+
# Verify transformers fork installation
|
| 41 |
+
def verify_transformers_fork():
|
| 42 |
+
"""Verify that the custom transformers fork is installed correctly."""
|
| 43 |
+
try:
|
| 44 |
+
from transformers import AutoModelForTextToSpeech
|
| 45 |
+
logger.info("Custom transformers fork with AutoModelForTextToSpeech loaded successfully")
|
| 46 |
+
except ImportError as e:
|
| 47 |
+
logger.error(
|
| 48 |
+
f"Failed to import AutoModelForTextToSpeech. Ensure the custom transformers fork is installed.\n"
|
| 49 |
+
f"Run: pip install git+https://github.com/microsoft/VibeVoice.git#subdirectory=transformers\n"
|
| 50 |
+
f"Error: {e}"
|
| 51 |
+
)
|
| 52 |
+
raise
|
| 53 |
+
|
| 54 |
# Determine device and attention implementation
|
| 55 |
def get_device():
|
| 56 |
"""Determine the device (CPU/GPU) and attention implementation."""
|
|
|
|
| 63 |
# Load model and tokenizer
|
| 64 |
def load_model(model_name="microsoft/VibeVoice-1.5B"):
|
| 65 |
"""Load VibeVoice model and tokenizer with memory optimization."""
|
| 66 |
+
from transformers import AutoModelForTextToSpeech, AutoTokenizer, AutoConfig
|
| 67 |
+
|
| 68 |
device, attn_impl = get_device()
|
| 69 |
try:
|
| 70 |
# Load config with specified attention implementation
|
| 71 |
config = AutoConfig.from_pretrained(model_name, attn_implementation=attn_impl)
|
| 72 |
+
# Enable memory-efficient loading
|
| 73 |
model = AutoModelForTextToSpeech.from_pretrained(
|
| 74 |
model_name, config=config, torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
| 75 |
)
|
|
|
|
| 104 |
# Generate audio with memory management
|
| 105 |
with torch.no_grad():
|
| 106 |
if device == "cuda":
|
| 107 |
+
with torch.cuda.amp.autocast():
|
| 108 |
audio_output = model.generate(**inputs)
|
| 109 |
else:
|
| 110 |
audio_output = model.generate(**inputs)
|
| 111 |
|
| 112 |
# Convert to numpy array
|
| 113 |
audio_np = audio_output.cpu().numpy().squeeze()
|
| 114 |
+
if audio_np.ndim > 1:
|
| 115 |
audio_np = audio_np[0]
|
| 116 |
|
| 117 |
# Normalize audio to [-1, 1]
|
|
|
|
| 143 |
except ValueError as e:
|
| 144 |
return str(e)
|
| 145 |
|
| 146 |
+
# Global model and tokenizer
|
| 147 |
try:
|
| 148 |
+
verify_transformers_fork()
|
| 149 |
model, tokenizer, device = load_model()
|
| 150 |
except Exception as e:
|
| 151 |
logger.error(f"Failed to initialize: {e}")
|