metaambod / tts.py
unijoh's picture
Update tts.py
58f6d57 verified
raw
history blame
No virus
1.05 kB
import torch
from transformers import SpeechT5ForTextToSpeech, SpeechT5Processor
import logging
# Set up logging
logging.basicConfig(level=logging.DEBUG)
MODEL_ID = "microsoft/speecht5_tts"
# Try to load the model and processor
try:
processor = SpeechT5Processor.from_pretrained(MODEL_ID)
model = SpeechT5ForTextToSpeech.from_pretrained(MODEL_ID)
logging.info("Model and processor loaded successfully.")
except Exception as e:
logging.error(f"Error loading model or processor: {e}")
def synthesize_speech(text):
try:
inputs = processor(text, return_tensors="pt")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
inputs = inputs.to(device)
with torch.no_grad():
speech = model.generate(**inputs)
logging.info("Speech generated successfully.")
return processor.decode(speech, skip_special_tokens=True)
except Exception as e:
logging.error(f"Error during speech synthesis: {e}")
return None