feat: use xpu as default if it exists
This commit is contained in:
parent
6065c5224e
commit
66b32b2e4e
2 changed files with 49 additions and 44 deletions
5
.gitignore
vendored
5
.gitignore
vendored
|
@ -169,9 +169,14 @@ tags
|
||||||
.ruff_cache
|
.ruff_cache
|
||||||
|
|
||||||
# our proj
|
# our proj
|
||||||
|
/inputs/
|
||||||
/output/
|
/output/
|
||||||
/outputs/
|
/outputs/
|
||||||
/checkpoint/
|
/checkpoint/
|
||||||
/checkpoints/
|
/checkpoints/
|
||||||
exp
|
exp
|
||||||
.gradio/
|
.gradio/
|
||||||
|
|
||||||
|
*~
|
||||||
|
*swp
|
||||||
|
*swo
|
||||||
|
|
|
@ -16,17 +16,17 @@ logger = logging.get_logger(__name__)
|
||||||
|
|
||||||
class VoiceMapper:
|
class VoiceMapper:
|
||||||
"""Maps speaker names to voice file paths"""
|
"""Maps speaker names to voice file paths"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.setup_voice_presets()
|
self.setup_voice_presets()
|
||||||
|
|
||||||
# change name according to our preset wav file
|
# change name according to our preset wav file
|
||||||
new_dict = {}
|
new_dict = {}
|
||||||
for name, path in self.voice_presets.items():
|
for name, path in self.voice_presets.items():
|
||||||
|
|
||||||
if '_' in name:
|
if '_' in name:
|
||||||
name = name.split('_')[0]
|
name = name.split('_')[0]
|
||||||
|
|
||||||
if '-' in name:
|
if '-' in name:
|
||||||
name = name.split('-')[-1]
|
name = name.split('-')[-1]
|
||||||
|
|
||||||
|
@ -37,21 +37,21 @@ class VoiceMapper:
|
||||||
def setup_voice_presets(self):
|
def setup_voice_presets(self):
|
||||||
"""Setup voice presets by scanning the voices directory."""
|
"""Setup voice presets by scanning the voices directory."""
|
||||||
voices_dir = os.path.join(os.path.dirname(__file__), "voices")
|
voices_dir = os.path.join(os.path.dirname(__file__), "voices")
|
||||||
|
|
||||||
# Check if voices directory exists
|
# Check if voices directory exists
|
||||||
if not os.path.exists(voices_dir):
|
if not os.path.exists(voices_dir):
|
||||||
print(f"Warning: Voices directory not found at {voices_dir}")
|
print(f"Warning: Voices directory not found at {voices_dir}")
|
||||||
self.voice_presets = {}
|
self.voice_presets = {}
|
||||||
self.available_voices = {}
|
self.available_voices = {}
|
||||||
return
|
return
|
||||||
|
|
||||||
# Scan for all WAV files in the voices directory
|
# Scan for all WAV files in the voices directory
|
||||||
self.voice_presets = {}
|
self.voice_presets = {}
|
||||||
|
|
||||||
# Get all .wav files in the voices directory
|
# Get all .wav files in the voices directory
|
||||||
wav_files = [f for f in os.listdir(voices_dir)
|
wav_files = [f for f in os.listdir(voices_dir)
|
||||||
if f.lower().endswith('.wav') and os.path.isfile(os.path.join(voices_dir, f))]
|
if f.lower().endswith('.wav') and os.path.isfile(os.path.join(voices_dir, f))]
|
||||||
|
|
||||||
# Create dictionary with filename (without extension) as key
|
# Create dictionary with filename (without extension) as key
|
||||||
for wav_file in wav_files:
|
for wav_file in wav_files:
|
||||||
# Remove .wav extension to get the name
|
# Remove .wav extension to get the name
|
||||||
|
@ -59,16 +59,16 @@ class VoiceMapper:
|
||||||
# Create full path
|
# Create full path
|
||||||
full_path = os.path.join(voices_dir, wav_file)
|
full_path = os.path.join(voices_dir, wav_file)
|
||||||
self.voice_presets[name] = full_path
|
self.voice_presets[name] = full_path
|
||||||
|
|
||||||
# Sort the voice presets alphabetically by name for better UI
|
# Sort the voice presets alphabetically by name for better UI
|
||||||
self.voice_presets = dict(sorted(self.voice_presets.items()))
|
self.voice_presets = dict(sorted(self.voice_presets.items()))
|
||||||
|
|
||||||
# Filter out voices that don't exist (this is now redundant but kept for safety)
|
# Filter out voices that don't exist (this is now redundant but kept for safety)
|
||||||
self.available_voices = {
|
self.available_voices = {
|
||||||
name: path for name, path in self.voice_presets.items()
|
name: path for name, path in self.voice_presets.items()
|
||||||
if os.path.exists(path)
|
if os.path.exists(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
print(f"Found {len(self.available_voices)} voice files in {voices_dir}")
|
print(f"Found {len(self.available_voices)} voice files in {voices_dir}")
|
||||||
print(f"Available voices: {', '.join(self.available_voices.keys())}")
|
print(f"Available voices: {', '.join(self.available_voices.keys())}")
|
||||||
|
|
||||||
|
@ -77,13 +77,13 @@ class VoiceMapper:
|
||||||
# First try exact match
|
# First try exact match
|
||||||
if speaker_name in self.voice_presets:
|
if speaker_name in self.voice_presets:
|
||||||
return self.voice_presets[speaker_name]
|
return self.voice_presets[speaker_name]
|
||||||
|
|
||||||
# Try partial matching (case insensitive)
|
# Try partial matching (case insensitive)
|
||||||
speaker_lower = speaker_name.lower()
|
speaker_lower = speaker_name.lower()
|
||||||
for preset_name, path in self.voice_presets.items():
|
for preset_name, path in self.voice_presets.items():
|
||||||
if preset_name.lower() in speaker_lower or speaker_lower in preset_name.lower():
|
if preset_name.lower() in speaker_lower or speaker_lower in preset_name.lower():
|
||||||
return path
|
return path
|
||||||
|
|
||||||
# Default to first voice if no match found
|
# Default to first voice if no match found
|
||||||
default_voice = list(self.voice_presets.values())[0]
|
default_voice = list(self.voice_presets.values())[0]
|
||||||
print(f"Warning: No voice preset found for '{speaker_name}', using default voice: {default_voice}")
|
print(f"Warning: No voice preset found for '{speaker_name}', using default voice: {default_voice}")
|
||||||
|
@ -99,25 +99,25 @@ def parse_txt_script(txt_content: str) -> Tuple[List[str], List[str]]:
|
||||||
lines = txt_content.strip().split('\n')
|
lines = txt_content.strip().split('\n')
|
||||||
scripts = []
|
scripts = []
|
||||||
speaker_numbers = []
|
speaker_numbers = []
|
||||||
|
|
||||||
# Pattern to match "Speaker X:" format where X is a number
|
# Pattern to match "Speaker X:" format where X is a number
|
||||||
speaker_pattern = r'^Speaker\s+(\d+):\s*(.*)$'
|
speaker_pattern = r'^Speaker\s+(\d+):\s*(.*)$'
|
||||||
|
|
||||||
current_speaker = None
|
current_speaker = None
|
||||||
current_text = ""
|
current_text = ""
|
||||||
|
|
||||||
for line in lines:
|
for line in lines:
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
if not line:
|
if not line:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
match = re.match(speaker_pattern, line, re.IGNORECASE)
|
match = re.match(speaker_pattern, line, re.IGNORECASE)
|
||||||
if match:
|
if match:
|
||||||
# If we have accumulated text from previous speaker, save it
|
# If we have accumulated text from previous speaker, save it
|
||||||
if current_speaker and current_text:
|
if current_speaker and current_text:
|
||||||
scripts.append(f"Speaker {current_speaker}: {current_text.strip()}")
|
scripts.append(f"Speaker {current_speaker}: {current_text.strip()}")
|
||||||
speaker_numbers.append(current_speaker)
|
speaker_numbers.append(current_speaker)
|
||||||
|
|
||||||
# Start new speaker
|
# Start new speaker
|
||||||
current_speaker = match.group(1).strip()
|
current_speaker = match.group(1).strip()
|
||||||
current_text = match.group(2).strip()
|
current_text = match.group(2).strip()
|
||||||
|
@ -127,12 +127,12 @@ def parse_txt_script(txt_content: str) -> Tuple[List[str], List[str]]:
|
||||||
current_text += " " + line
|
current_text += " " + line
|
||||||
else:
|
else:
|
||||||
current_text = line
|
current_text = line
|
||||||
|
|
||||||
# Don't forget the last speaker
|
# Don't forget the last speaker
|
||||||
if current_speaker and current_text:
|
if current_speaker and current_text:
|
||||||
scripts.append(f"Speaker {current_speaker}: {current_text.strip()}")
|
scripts.append(f"Speaker {current_speaker}: {current_text.strip()}")
|
||||||
speaker_numbers.append(current_speaker)
|
speaker_numbers.append(current_speaker)
|
||||||
|
|
||||||
return scripts, speaker_numbers
|
return scripts, speaker_numbers
|
||||||
|
|
||||||
|
|
||||||
|
@ -144,7 +144,7 @@ def parse_args():
|
||||||
default="microsoft/VibeVoice-1.5b",
|
default="microsoft/VibeVoice-1.5b",
|
||||||
help="Path to the HuggingFace model directory",
|
help="Path to the HuggingFace model directory",
|
||||||
)
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--txt_path",
|
"--txt_path",
|
||||||
type=str,
|
type=str,
|
||||||
|
@ -167,7 +167,7 @@ def parse_args():
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--device",
|
"--device",
|
||||||
type=str,
|
type=str,
|
||||||
default=("cuda" if torch.cuda.is_available() else ("mps" if torch.backends.mps.is_available() else "cpu")),
|
default=("cuda" if torch.cuda.is_available() else ("mps" if torch.backends.mps.is_available() else ("xpu" if torch.xpu.is_available() else "cpu"))),
|
||||||
help="Device for inference: cuda | mps | cpu",
|
help="Device for inference: cuda | mps | cpu",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
|
@ -176,7 +176,7 @@ def parse_args():
|
||||||
default=1.3,
|
default=1.3,
|
||||||
help="CFG (Classifier-Free Guidance) scale for generation (default: 1.3)",
|
help="CFG (Classifier-Free Guidance) scale for generation (default: 1.3)",
|
||||||
)
|
)
|
||||||
|
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -196,44 +196,44 @@ def main():
|
||||||
|
|
||||||
# Initialize voice mapper
|
# Initialize voice mapper
|
||||||
voice_mapper = VoiceMapper()
|
voice_mapper = VoiceMapper()
|
||||||
|
|
||||||
# Check if txt file exists
|
# Check if txt file exists
|
||||||
if not os.path.exists(args.txt_path):
|
if not os.path.exists(args.txt_path):
|
||||||
print(f"Error: txt file not found: {args.txt_path}")
|
print(f"Error: txt file not found: {args.txt_path}")
|
||||||
return
|
return
|
||||||
|
|
||||||
# Read and parse txt file
|
# Read and parse txt file
|
||||||
print(f"Reading script from: {args.txt_path}")
|
print(f"Reading script from: {args.txt_path}")
|
||||||
with open(args.txt_path, 'r', encoding='utf-8') as f:
|
with open(args.txt_path, 'r', encoding='utf-8') as f:
|
||||||
txt_content = f.read()
|
txt_content = f.read()
|
||||||
|
|
||||||
# Parse the txt content to get speaker numbers
|
# Parse the txt content to get speaker numbers
|
||||||
scripts, speaker_numbers = parse_txt_script(txt_content)
|
scripts, speaker_numbers = parse_txt_script(txt_content)
|
||||||
|
|
||||||
if not scripts:
|
if not scripts:
|
||||||
print("Error: No valid speaker scripts found in the txt file")
|
print("Error: No valid speaker scripts found in the txt file")
|
||||||
return
|
return
|
||||||
|
|
||||||
print(f"Found {len(scripts)} speaker segments:")
|
print(f"Found {len(scripts)} speaker segments:")
|
||||||
for i, (script, speaker_num) in enumerate(zip(scripts, speaker_numbers)):
|
for i, (script, speaker_num) in enumerate(zip(scripts, speaker_numbers)):
|
||||||
print(f" {i+1}. Speaker {speaker_num}")
|
print(f" {i+1}. Speaker {speaker_num}")
|
||||||
print(f" Text preview: {script[:100]}...")
|
print(f" Text preview: {script[:100]}...")
|
||||||
|
|
||||||
# Map speaker numbers to provided speaker names
|
# Map speaker numbers to provided speaker names
|
||||||
speaker_name_mapping = {}
|
speaker_name_mapping = {}
|
||||||
speaker_names_list = args.speaker_names if isinstance(args.speaker_names, list) else [args.speaker_names]
|
speaker_names_list = args.speaker_names if isinstance(args.speaker_names, list) else [args.speaker_names]
|
||||||
for i, name in enumerate(speaker_names_list, 1):
|
for i, name in enumerate(speaker_names_list, 1):
|
||||||
speaker_name_mapping[str(i)] = name
|
speaker_name_mapping[str(i)] = name
|
||||||
|
|
||||||
print(f"\nSpeaker mapping:")
|
print(f"\nSpeaker mapping:")
|
||||||
for speaker_num in set(speaker_numbers):
|
for speaker_num in set(speaker_numbers):
|
||||||
mapped_name = speaker_name_mapping.get(speaker_num, f"Speaker {speaker_num}")
|
mapped_name = speaker_name_mapping.get(speaker_num, f"Speaker {speaker_num}")
|
||||||
print(f" Speaker {speaker_num} -> {mapped_name}")
|
print(f" Speaker {speaker_num} -> {mapped_name}")
|
||||||
|
|
||||||
# Map speakers to voice files using the provided speaker names
|
# Map speakers to voice files using the provided speaker names
|
||||||
voice_samples = []
|
voice_samples = []
|
||||||
actual_speakers = []
|
actual_speakers = []
|
||||||
|
|
||||||
# Get unique speaker numbers in order of first appearance
|
# Get unique speaker numbers in order of first appearance
|
||||||
unique_speaker_numbers = []
|
unique_speaker_numbers = []
|
||||||
seen = set()
|
seen = set()
|
||||||
|
@ -241,18 +241,18 @@ def main():
|
||||||
if speaker_num not in seen:
|
if speaker_num not in seen:
|
||||||
unique_speaker_numbers.append(speaker_num)
|
unique_speaker_numbers.append(speaker_num)
|
||||||
seen.add(speaker_num)
|
seen.add(speaker_num)
|
||||||
|
|
||||||
for speaker_num in unique_speaker_numbers:
|
for speaker_num in unique_speaker_numbers:
|
||||||
speaker_name = speaker_name_mapping.get(speaker_num, f"Speaker {speaker_num}")
|
speaker_name = speaker_name_mapping.get(speaker_num, f"Speaker {speaker_num}")
|
||||||
voice_path = voice_mapper.get_voice_path(speaker_name)
|
voice_path = voice_mapper.get_voice_path(speaker_name)
|
||||||
voice_samples.append(voice_path)
|
voice_samples.append(voice_path)
|
||||||
actual_speakers.append(speaker_name)
|
actual_speakers.append(speaker_name)
|
||||||
print(f"Speaker {speaker_num} ('{speaker_name}') -> Voice: {os.path.basename(voice_path)}")
|
print(f"Speaker {speaker_num} ('{speaker_name}') -> Voice: {os.path.basename(voice_path)}")
|
||||||
|
|
||||||
# Prepare data for model
|
# Prepare data for model
|
||||||
full_script = '\n'.join(scripts)
|
full_script = '\n'.join(scripts)
|
||||||
full_script = full_script.replace("’", "'")
|
full_script = full_script.replace("’", "'")
|
||||||
|
|
||||||
print(f"Loading processor & model from {args.model_path}")
|
print(f"Loading processor & model from {args.model_path}")
|
||||||
processor = VibeVoiceProcessor.from_pretrained(args.model_path)
|
processor = VibeVoiceProcessor.from_pretrained(args.model_path)
|
||||||
|
|
||||||
|
@ -314,7 +314,7 @@ def main():
|
||||||
|
|
||||||
if hasattr(model.model, 'language_model'):
|
if hasattr(model.model, 'language_model'):
|
||||||
print(f"Language model attention: {model.model.language_model.config._attn_implementation}")
|
print(f"Language model attention: {model.model.language_model.config._attn_implementation}")
|
||||||
|
|
||||||
# Prepare inputs for the model
|
# Prepare inputs for the model
|
||||||
inputs = processor(
|
inputs = processor(
|
||||||
text=[full_script], # Wrap in list for batch processing
|
text=[full_script], # Wrap in list for batch processing
|
||||||
|
@ -344,7 +344,7 @@ def main():
|
||||||
)
|
)
|
||||||
generation_time = time.time() - start_time
|
generation_time = time.time() - start_time
|
||||||
print(f"Generation time: {generation_time:.2f} seconds")
|
print(f"Generation time: {generation_time:.2f} seconds")
|
||||||
|
|
||||||
# Calculate audio duration and additional metrics
|
# Calculate audio duration and additional metrics
|
||||||
if outputs.speech_outputs and outputs.speech_outputs[0] is not None:
|
if outputs.speech_outputs and outputs.speech_outputs[0] is not None:
|
||||||
# Assuming 24kHz sample rate (common for speech synthesis)
|
# Assuming 24kHz sample rate (common for speech synthesis)
|
||||||
|
@ -352,17 +352,17 @@ def main():
|
||||||
audio_samples = outputs.speech_outputs[0].shape[-1] if len(outputs.speech_outputs[0].shape) > 0 else len(outputs.speech_outputs[0])
|
audio_samples = outputs.speech_outputs[0].shape[-1] if len(outputs.speech_outputs[0].shape) > 0 else len(outputs.speech_outputs[0])
|
||||||
audio_duration = audio_samples / sample_rate
|
audio_duration = audio_samples / sample_rate
|
||||||
rtf = generation_time / audio_duration if audio_duration > 0 else float('inf')
|
rtf = generation_time / audio_duration if audio_duration > 0 else float('inf')
|
||||||
|
|
||||||
print(f"Generated audio duration: {audio_duration:.2f} seconds")
|
print(f"Generated audio duration: {audio_duration:.2f} seconds")
|
||||||
print(f"RTF (Real Time Factor): {rtf:.2f}x")
|
print(f"RTF (Real Time Factor): {rtf:.2f}x")
|
||||||
else:
|
else:
|
||||||
print("No audio output generated")
|
print("No audio output generated")
|
||||||
|
|
||||||
# Calculate token metrics
|
# Calculate token metrics
|
||||||
input_tokens = inputs['input_ids'].shape[1] # Number of input tokens
|
input_tokens = inputs['input_ids'].shape[1] # Number of input tokens
|
||||||
output_tokens = outputs.sequences.shape[1] # Total tokens (input + generated)
|
output_tokens = outputs.sequences.shape[1] # Total tokens (input + generated)
|
||||||
generated_tokens = output_tokens - input_tokens
|
generated_tokens = output_tokens - input_tokens
|
||||||
|
|
||||||
print(f"Prefilling tokens: {input_tokens}")
|
print(f"Prefilling tokens: {input_tokens}")
|
||||||
print(f"Generated tokens: {generated_tokens}")
|
print(f"Generated tokens: {generated_tokens}")
|
||||||
print(f"Total tokens: {output_tokens}")
|
print(f"Total tokens: {output_tokens}")
|
||||||
|
@ -371,13 +371,13 @@ def main():
|
||||||
txt_filename = os.path.splitext(os.path.basename(args.txt_path))[0]
|
txt_filename = os.path.splitext(os.path.basename(args.txt_path))[0]
|
||||||
output_path = os.path.join(args.output_dir, f"{txt_filename}_generated.wav")
|
output_path = os.path.join(args.output_dir, f"{txt_filename}_generated.wav")
|
||||||
os.makedirs(args.output_dir, exist_ok=True)
|
os.makedirs(args.output_dir, exist_ok=True)
|
||||||
|
|
||||||
processor.save_audio(
|
processor.save_audio(
|
||||||
outputs.speech_outputs[0], # First (and only) batch item
|
outputs.speech_outputs[0], # First (and only) batch item
|
||||||
output_path=output_path,
|
output_path=output_path,
|
||||||
)
|
)
|
||||||
print(f"Saved output to {output_path}")
|
print(f"Saved output to {output_path}")
|
||||||
|
|
||||||
# Print summary
|
# Print summary
|
||||||
print("\n" + "="*50)
|
print("\n" + "="*50)
|
||||||
print("GENERATION SUMMARY")
|
print("GENERATION SUMMARY")
|
||||||
|
@ -393,7 +393,7 @@ def main():
|
||||||
print(f"Generation time: {generation_time:.2f} seconds")
|
print(f"Generation time: {generation_time:.2f} seconds")
|
||||||
print(f"Audio duration: {audio_duration:.2f} seconds")
|
print(f"Audio duration: {audio_duration:.2f} seconds")
|
||||||
print(f"RTF (Real Time Factor): {rtf:.2f}x")
|
print(f"RTF (Real Time Factor): {rtf:.2f}x")
|
||||||
|
|
||||||
print("="*50)
|
print("="*50)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in a new issue