Update to use openai-whisper

This commit is contained in:
erew123 authored and GitHub committed 2024-11-15 00:40:02 +00:00
1 parent 5c4726de96
commit b538564da6
1 file changed
+31 -59
+31 -59
View File
@@ -2,46 +2,27 @@ import json
import argparse import argparse
from pathlib import Path from pathlib import Path
import os import os
import glob
import platform
import site
import subprocess
import sys import sys
# version 0.8 import subprocess
os.environ['KMP_DUPLICATE_LIB_OK']='True' # version 0.9
import warnings
def find_files_in_path_with_wildcard(pattern): # Filter Flash Attention warning
# Get the site-packages directory of the current Python environment warnings.filterwarnings("ignore", message="1Torch was not compiled with flash attention")
site_packages_path = site.getsitepackages() warnings.filterwarnings("ignore", message="Failed to launch Triton kernels, likely due to missing CUDA toolkit")
found_paths = [] # Try to import whisper, install if not found
# Adjust the sub-directory based on the operating system try:
sub_directory = "nvidia/cublas" import whisper
if platform.system() == "Linux": except ImportError:
sub_directory = os.path.join(sub_directory, "lib") print("[AllTalk TTSDiff] OpenAI Whisper not found. Attempting to install...")
else: try:
sub_directory = os.path.join(sub_directory, "bin") subprocess.check_call([sys.executable, "-m", "pip", "install", "openai-whisper"])
import whisper
# Iterate over each site-packages directory (there can be more than one) print("[AllTalk TTSDiff] Successfully installed OpenAI Whisper!")
for directory in site_packages_path: except Exception as e:
# Construct the search directory path print("[AllTalk TTSDiff] Failed to install OpenAI Whisper:")
search_directory = os.path.join(directory, sub_directory) print(f"[AllTalk TTSDiff] Error: {str(e)}")
# Use glob to find all files matching the pattern in this directory print("[AllTalk TTSDiff] Please try manually installing with: pip install openai-whisper")
for file_path in glob.glob(os.path.join(search_directory, pattern)): sys.exit(1)
if os.path.isfile(file_path): # Ensure it's a file
found_paths.append(file_path)
return found_paths
def detect_cublas():
# Use different file patterns based on the operating system
file_name_pattern = 'cublas64_11.*' if platform.system() == "Windows" else 'libcublas.so.11*'
found_paths = find_files_in_path_with_wildcard(file_name_pattern)
if found_paths:
print("[AllTalk TTSDiff] \033[94mCublas:\033[0m \033[92mDetected\033[0m", found_paths)
else:
print("[AllTalk TTSDiff] \033[94mCublas:\033[0m \033[91mERROR Not Detected\033[0m")
print("[AllTalk TTSDiff] \033[94mPlease install Cublas from the Nvidia CUDA Toolkit \033[0mhttps://developer.nvidia.com/cuda-downloads")
exit(1)
parser = argparse.ArgumentParser(description="Compare TTS output with the original text using detailed comparison.") parser = argparse.ArgumentParser(description="Compare TTS output with the original text using detailed comparison.")
parser.add_argument("--threshold", type=int, default=98, help="Similarity threshold for considering a match (default: 98)") parser.add_argument("--threshold", type=int, default=98, help="Similarity threshold for considering a match (default: 98)")
@@ -61,7 +42,7 @@ accuracy_threshold = args.threshold
def disclaimer_text(): def disclaimer_text():
print(f"\nDESCRIPTION: ") print(f"\nDESCRIPTION: ")
print(f" This script is designed to assist in identifying bad TTS generation ID's from the TTS Generator.\n") print(f" This script is designed to assist in identifying bad TTS generation ID's from the TTS Generator.\n")
print(f" This will download the Whisper Large-v2 model (if not already downloaded), and use it to compare your text to") print(f" This will download the Whisper Large-v3 model (if not already downloaded), and use it to compare your text to")
print(f" to the generated TTS. If it finds an issue, it will flag the ID number that has the issue. This does NOT 100%") print(f" to the generated TTS. If it finds an issue, it will flag the ID number that has the issue. This does NOT 100%")
print(f" ensure that all TTS generated is perfect, sounds good etc, but it should flag up glaring issues and help with") print(f" ensure that all TTS generated is perfect, sounds good etc, but it should flag up glaring issues and help with")
print(f" the process of identifying bad TTS generations.") print(f" the process of identifying bad TTS generations.")
@@ -78,13 +59,10 @@ def disclaimer_text():
print(f" - The JSON file must be named \033[93mttsList.json\033[0m") print(f" - The JSON file must be named \033[93mttsList.json\033[0m")
print(f" - When you have your ID list, go back into the TTS Generator, correct any lines and regenerate them. If you") print(f" - When you have your ID list, go back into the TTS Generator, correct any lines and regenerate them. If you")
print(f" want to re-test everthing again after re-generating, you will need to export your list again and re-run this") print(f" want to re-test everthing again after re-generating, you will need to export your list again and re-run this")
print(f" script again, against your newly exported JSON list.") print(f" script again, against your newly exported JSON list.\n")
print(f" - This requires access to \033[93mcublas64_11\033[0m, the same as Finetuning.")
print(f" https://github.com/erew123/alltalk_tts/tree/main?#-important-requirements-cuda-118\n")
return return
try: try:
from faster_whisper import WhisperModel
import re import re
import string import string
import spacy import spacy
@@ -102,13 +80,10 @@ try:
# Load a medium-sized spaCy model, adjust to "en_core_web_md" or "en_core_web_lg" as needed # Load a medium-sized spaCy model, adjust to "en_core_web_md" or "en_core_web_lg" as needed
nlp = spacy.load("en_core_web_md") nlp = spacy.load("en_core_web_md")
except Exception as e: except Exception as e:
print("[AllTalk TTSDiff] Failed to load the spaCy language model.")
# Attempt to download the spaCy model automatically
try: try:
print("[AllTalk TTSDiff] Attempting to automatically download the spaCy language model...") print("[AllTalk TTSDiff] SpaCy language model not installed.")
print("[AllTalk TTSDiff] Attempting to automatically download the spaCy language model.")
subprocess.run([sys.executable, "-m", "spacy", "download", "en_core_web_md"], check=True) subprocess.run([sys.executable, "-m", "spacy", "download", "en_core_web_md"], check=True)
# Try to load the model again after downloading # Try to load the model again after downloading
nlp = spacy.load("en_core_web_md") nlp = spacy.load("en_core_web_md")
print("[AllTalk TTSDiff] Model downloaded and loaded successfully.") print("[AllTalk TTSDiff] Model downloaded and loaded successfully.")
@@ -141,7 +116,7 @@ def normalize_text(text):
text = text.lower() text = text.lower()
# Standardize and then remove quotation marks # Standardize and then remove quotation marks
text = text.replace("“", '"').replace("”", '"').replace("‘", "'").replace("’", "'") text = text.replace(""", '"').replace(""", '"').replace("'", "'").replace("'", "'")
text = text.translate(str.maketrans('', '', '"\'')) text = text.translate(str.maketrans('', '', '"\''))
# Remove all other punctuation except hyphens to preserve compound words # Remove all other punctuation except hyphens to preserve compound words
@@ -188,15 +163,12 @@ def transcribe_and_compare(file_url, original_text, model, item_id, flagged_ids)
return # Skip this file if it can't be accessed return # Skip this file if it can't be accessed
try: try:
segments, info = model.transcribe(str(audio_file_path), beam_size=5) result = model.transcribe(str(audio_file_path))
transcribed_text = " ".join([segment.text for segment in segments]) transcribed_text = result["text"]
except Exception as e: except Exception as e:
print(f"[AllTalk TTSDiff] Error transcribing file {audio_file_path}: {e}") print(f"[AllTalk TTSDiff] Error transcribing file {audio_file_path}: {e}")
return # Skip this file if transcription fails return # Skip this file if transcription fails
segments, info = model.transcribe(str(audio_file_path), beam_size=5)
transcribed_text = " ".join([segment.text for segment in segments])
# Normalize texts for comparison # Normalize texts for comparison
original_text_normalized = normalize_text(original_text) original_text_normalized = normalize_text(original_text)
transcribed_text_normalized = normalize_text(transcribed_text) transcribed_text_normalized = normalize_text(transcribed_text)
@@ -210,7 +182,6 @@ def transcribe_and_compare(file_url, original_text, model, item_id, flagged_ids)
# Only log and flag IDs for review if there's a mismatch or detected issues # Only log and flag IDs for review if there's a mismatch or detected issues
if not is_match: if not is_match:
#print(f"\033[93mMismatch Detected - ID:\033[0m {item_id}")
print(f"[AllTalk TTSDiff] \033[93mText:\033[0m {original_text}") print(f"[AllTalk TTSDiff] \033[93mText:\033[0m {original_text}")
print(f"[AllTalk TTSDiff] \033[91mTTS :\033[0m{transcribed_text}") print(f"[AllTalk TTSDiff] \033[91mTTS :\033[0m{transcribed_text}")
if has_spoken_punctuation: if has_spoken_punctuation:
@@ -225,10 +196,11 @@ def main():
print("[AllTalk TTSDiff] \033[94mJSON file :\033[92m", json_file_path, "\033[0m") print("[AllTalk TTSDiff] \033[94mJSON file :\033[92m", json_file_path, "\033[0m")
print("[AllTalk TTSDiff] \033[94mWAV files :\033[92m", wav_file_path, "\033[0m") print("[AllTalk TTSDiff] \033[94mWAV files :\033[92m", wav_file_path, "\033[0m")
print("[AllTalk TTSDiff] \033[94mAccuracy :\033[92m", accuracy_threshold, "%", "\033[0m") print("[AllTalk TTSDiff] \033[94mAccuracy :\033[92m", accuracy_threshold, "%", "\033[0m")
detect_cublas()
model_size = "large-v2"
device = "cuda" if torch.cuda.is_available() else "cpu" device = "cuda" if torch.cuda.is_available() else "cpu"
model = WhisperModel(model_size, device=device, compute_type="float32") print("[AllTalk TTSDiff] \033[94mDevice :\033[92m", device, "\033[0m")
print("[AllTalk TTSDiff] Loading Whisper Large-v3 model...")
model = whisper.load_model("large-v3", device=device)
flagged_ids = [] # Initialize the list to track IDs needing review flagged_ids = [] # Initialize the list to track IDs needing review