Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Commit
·
0e9007a
1
Parent(s):
0a2959b
Modify with new transformer support
Browse files- app.py +11 -27
- requirements.txt +2 -10
- setup.py +0 -89
app.py
CHANGED
|
@@ -6,13 +6,10 @@ import librosa
|
|
| 6 |
import soundfile as sf
|
| 7 |
import torch
|
| 8 |
import traceback
|
| 9 |
-
import threading
|
| 10 |
from spaces import GPU
|
| 11 |
from datetime import datetime
|
| 12 |
|
| 13 |
-
from
|
| 14 |
-
from processor.vibevoice_processor import VibeVoiceProcessor
|
| 15 |
-
from modular.streamer import AudioStreamer
|
| 16 |
from transformers.utils import logging
|
| 17 |
from transformers import set_seed
|
| 18 |
|
|
@@ -47,17 +44,6 @@ class VibeVoiceDemo:
|
|
| 47 |
def load_models(self):
|
| 48 |
print("Loading processors and models on CPU...")
|
| 49 |
|
| 50 |
-
# Debug: Show cache location
|
| 51 |
-
import os
|
| 52 |
-
cache_dir = os.path.expanduser("~/.cache/huggingface/hub")
|
| 53 |
-
print(f"HuggingFace cache directory: {cache_dir}")
|
| 54 |
-
if os.path.exists(cache_dir):
|
| 55 |
-
print(f"Cache exists. Size: {sum(os.path.getsize(os.path.join(dirpath, filename)) for dirpath, _, filenames in os.walk(cache_dir) for filename in filenames) / (1024**3):.2f} GB")
|
| 56 |
-
print("Cached models:")
|
| 57 |
-
for item in os.listdir(cache_dir):
|
| 58 |
-
if item.startswith("models--"):
|
| 59 |
-
print(f" - {item}")
|
| 60 |
-
|
| 61 |
for name, path in self.model_paths.items():
|
| 62 |
print(f" - {name} from {path}")
|
| 63 |
proc = VibeVoiceProcessor.from_pretrained(path)
|
|
@@ -173,15 +159,7 @@ class VibeVoiceDemo:
|
|
| 173 |
log += f"Parameters: CFG Scale={cfg_scale}\n"
|
| 174 |
log += f"Speakers: {', '.join(selected_speakers)}\n"
|
| 175 |
|
| 176 |
-
|
| 177 |
-
for speaker_name in selected_speakers:
|
| 178 |
-
audio_path = self.available_voices[speaker_name]
|
| 179 |
-
audio_data = self.read_audio(audio_path)
|
| 180 |
-
if len(audio_data) == 0:
|
| 181 |
-
raise gr.Error(f"Error: Failed to load audio for {speaker_name}")
|
| 182 |
-
voice_samples.append(audio_data)
|
| 183 |
-
|
| 184 |
-
log += f"Loaded {len(voice_samples)} voice samples\n"
|
| 185 |
|
| 186 |
lines = script.strip().split('\n')
|
| 187 |
formatted_script_lines = []
|
|
@@ -199,13 +177,18 @@ class VibeVoiceDemo:
|
|
| 199 |
log += f"Formatted script with {len(formatted_script_lines)} turns\n"
|
| 200 |
log += "Processing with VibeVoice...\n"
|
| 201 |
|
|
|
|
|
|
|
|
|
|
| 202 |
inputs = processor(
|
| 203 |
text=[formatted_script],
|
| 204 |
-
voice_samples=[
|
| 205 |
padding=True,
|
| 206 |
return_tensors="pt",
|
| 207 |
-
return_attention_mask=True,
|
| 208 |
)
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
start_time = time.time()
|
| 211 |
outputs = model.generate(
|
|
@@ -227,7 +210,8 @@ class VibeVoiceDemo:
|
|
| 227 |
if audio.ndim > 1:
|
| 228 |
audio = audio.squeeze()
|
| 229 |
|
| 230 |
-
|
|
|
|
| 231 |
|
| 232 |
output_dir = "outputs"
|
| 233 |
os.makedirs(output_dir, exist_ok=True)
|
|
|
|
| 6 |
import soundfile as sf
|
| 7 |
import torch
|
| 8 |
import traceback
|
|
|
|
| 9 |
from spaces import GPU
|
| 10 |
from datetime import datetime
|
| 11 |
|
| 12 |
+
from transformers import VibeVoiceForConditionalGenerationInference, VibeVoiceProcessor
|
|
|
|
|
|
|
| 13 |
from transformers.utils import logging
|
| 14 |
from transformers import set_seed
|
| 15 |
|
|
|
|
| 44 |
def load_models(self):
|
| 45 |
print("Loading processors and models on CPU...")
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
for name, path in self.model_paths.items():
|
| 48 |
print(f" - {name} from {path}")
|
| 49 |
proc = VibeVoiceProcessor.from_pretrained(path)
|
|
|
|
| 159 |
log += f"Parameters: CFG Scale={cfg_scale}\n"
|
| 160 |
log += f"Speakers: {', '.join(selected_speakers)}\n"
|
| 161 |
|
| 162 |
+
log += f"Using voice samples from selected speakers\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
lines = script.strip().split('\n')
|
| 165 |
formatted_script_lines = []
|
|
|
|
| 177 |
log += f"Formatted script with {len(formatted_script_lines)} turns\n"
|
| 178 |
log += "Processing with VibeVoice...\n"
|
| 179 |
|
| 180 |
+
# Processor now expects file paths, not audio arrays
|
| 181 |
+
voice_sample_paths = [self.available_voices[speaker] for speaker in selected_speakers]
|
| 182 |
+
|
| 183 |
inputs = processor(
|
| 184 |
text=[formatted_script],
|
| 185 |
+
voice_samples=[voice_sample_paths],
|
| 186 |
padding=True,
|
| 187 |
return_tensors="pt",
|
|
|
|
| 188 |
)
|
| 189 |
+
|
| 190 |
+
# Move inputs to device
|
| 191 |
+
inputs = {key: val.to(self.device) if isinstance(val, torch.Tensor) else val for key, val in inputs.items()}
|
| 192 |
|
| 193 |
start_time = time.time()
|
| 194 |
outputs = model.generate(
|
|
|
|
| 210 |
if audio.ndim > 1:
|
| 211 |
audio = audio.squeeze()
|
| 212 |
|
| 213 |
+
# Get sample rate from processor
|
| 214 |
+
sample_rate = processor.audio_processor.sampling_rate if hasattr(processor, 'audio_processor') else 24000
|
| 215 |
|
| 216 |
output_dir = "outputs"
|
| 217 |
os.makedirs(output_dir, exist_ok=True)
|
requirements.txt
CHANGED
|
@@ -1,19 +1,11 @@
|
|
| 1 |
spaces
|
| 2 |
torch
|
| 3 |
-
accelerate
|
| 4 |
-
transformers
|
| 5 |
-
diffusers
|
| 6 |
tqdm
|
| 7 |
numpy
|
| 8 |
scipy
|
| 9 |
-
ml-collections
|
| 10 |
-
absl-py
|
| 11 |
gradio
|
| 12 |
-
av
|
| 13 |
-
aiortc
|
| 14 |
soundfile
|
| 15 |
librosa
|
| 16 |
-
pydub
|
| 17 |
-
requests
|
| 18 |
-
python-dotenv
|
| 19 |
|
|
|
|
| 1 |
spaces
|
| 2 |
torch
|
| 3 |
+
accelerate
|
| 4 |
+
git+https://github.com/huggingface/transformers.git@refs/pull/40546/head
|
|
|
|
| 5 |
tqdm
|
| 6 |
numpy
|
| 7 |
scipy
|
|
|
|
|
|
|
| 8 |
gradio
|
|
|
|
|
|
|
| 9 |
soundfile
|
| 10 |
librosa
|
|
|
|
|
|
|
|
|
|
| 11 |
|
setup.py
DELETED
|
@@ -1,89 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import subprocess
|
| 3 |
-
import sys
|
| 4 |
-
import shutil
|
| 5 |
-
from pathlib import Path
|
| 6 |
-
|
| 7 |
-
# setup.py will clone and install VibeVoice, copy voice files if they exist
|
| 8 |
-
|
| 9 |
-
def setup_vibevoice():
|
| 10 |
-
repo_dir = "VibeVoice"
|
| 11 |
-
original_dir = os.getcwd()
|
| 12 |
-
|
| 13 |
-
# clone repo if needed
|
| 14 |
-
if not os.path.exists(repo_dir):
|
| 15 |
-
print("Cloning the VibeVoice repository...")
|
| 16 |
-
try:
|
| 17 |
-
subprocess.run(
|
| 18 |
-
["git", "clone", "https://github.com/vibevoice-community/VibeVoice"],
|
| 19 |
-
check=True,
|
| 20 |
-
capture_output=True,
|
| 21 |
-
text=True
|
| 22 |
-
)
|
| 23 |
-
print("Repository cloned successfully.")
|
| 24 |
-
except subprocess.CalledProcessError as e:
|
| 25 |
-
print(f"Error cloning repository: {e.stderr}")
|
| 26 |
-
sys.exit(1)
|
| 27 |
-
else:
|
| 28 |
-
print("Repository already exists. Skipping clone.")
|
| 29 |
-
|
| 30 |
-
# install the package
|
| 31 |
-
os.chdir(repo_dir)
|
| 32 |
-
print(f"Changed directory to: {os.getcwd()}")
|
| 33 |
-
|
| 34 |
-
print("Installing the VibeVoice package...")
|
| 35 |
-
try:
|
| 36 |
-
subprocess.run(
|
| 37 |
-
[sys.executable, "-m", "pip", "install", "-e", "."],
|
| 38 |
-
check=True,
|
| 39 |
-
capture_output=True,
|
| 40 |
-
text=True
|
| 41 |
-
)
|
| 42 |
-
print("Package installed successfully.")
|
| 43 |
-
except subprocess.CalledProcessError as e:
|
| 44 |
-
print(f"Error installing package: {e.stderr}")
|
| 45 |
-
sys.exit(1)
|
| 46 |
-
|
| 47 |
-
# add to python path
|
| 48 |
-
sys.path.insert(0, os.getcwd())
|
| 49 |
-
|
| 50 |
-
# go back to original directory
|
| 51 |
-
os.chdir(original_dir)
|
| 52 |
-
print(f"Changed back to original directory: {os.getcwd()}")
|
| 53 |
-
|
| 54 |
-
# copy voice files if they exist
|
| 55 |
-
if os.path.exists("public/voices"):
|
| 56 |
-
target_voices_dir = os.path.join(repo_dir, "demo", "voices")
|
| 57 |
-
|
| 58 |
-
# clear existing voices and use only ours
|
| 59 |
-
if os.path.exists(target_voices_dir):
|
| 60 |
-
shutil.rmtree(target_voices_dir)
|
| 61 |
-
os.makedirs(target_voices_dir)
|
| 62 |
-
|
| 63 |
-
for file in os.listdir("public/voices"):
|
| 64 |
-
if file.endswith(('.mp3', '.wav', '.flac', '.ogg', '.m4a', '.aac')):
|
| 65 |
-
src = os.path.join("public/voices", file)
|
| 66 |
-
dst = os.path.join(target_voices_dir, file)
|
| 67 |
-
shutil.copy2(src, dst)
|
| 68 |
-
print(f"Copied voice file: {file}")
|
| 69 |
-
|
| 70 |
-
return repo_dir
|
| 71 |
-
|
| 72 |
-
def setup_voice_presets():
|
| 73 |
-
# get voice files from vibevoice demo directory
|
| 74 |
-
voices_dir = Path("VibeVoice/demo/voices")
|
| 75 |
-
|
| 76 |
-
voice_presets = {}
|
| 77 |
-
audio_extensions = ('.wav', '.mp3', '.flac', '.ogg', '.m4a', '.aac')
|
| 78 |
-
|
| 79 |
-
if voices_dir.exists():
|
| 80 |
-
for audio_file in voices_dir.glob("*"):
|
| 81 |
-
if audio_file.suffix.lower() in audio_extensions:
|
| 82 |
-
name = audio_file.stem
|
| 83 |
-
voice_presets[name] = str(audio_file)
|
| 84 |
-
|
| 85 |
-
# if no voices found, create directory
|
| 86 |
-
if not voice_presets and not voices_dir.exists():
|
| 87 |
-
voices_dir.mkdir(parents=True, exist_ok=True)
|
| 88 |
-
|
| 89 |
-
return dict(sorted(voice_presets.items()))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|