Update tts.py
Browse files
tts.py
CHANGED
@@ -1,17 +1,32 @@
|
|
1 |
import torch
|
2 |
from transformers import SpeechT5ForTextToSpeech, SpeechT5Processor
|
|
|
|
|
|
|
|
|
3 |
|
4 |
MODEL_ID = "microsoft/speecht5_tts"
|
5 |
-
processor = SpeechT5Processor.from_pretrained(MODEL_ID)
|
6 |
-
model = SpeechT5ForTextToSpeech.from_pretrained(MODEL_ID)
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
model.
|
12 |
-
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import torch
|
2 |
from transformers import SpeechT5ForTextToSpeech, SpeechT5Processor
|
3 |
+
import logging
|
4 |
+
|
5 |
+
# Set up logging
|
6 |
+
logging.basicConfig(level=logging.DEBUG)
|
7 |
|
8 |
MODEL_ID = "microsoft/speecht5_tts"
|
|
|
|
|
9 |
|
10 |
+
# Try to load the model and processor
|
11 |
+
try:
|
12 |
+
processor = SpeechT5Processor.from_pretrained(MODEL_ID)
|
13 |
+
model = SpeechT5ForTextToSpeech.from_pretrained(MODEL_ID)
|
14 |
+
logging.info("Model and processor loaded successfully.")
|
15 |
+
except Exception as e:
|
16 |
+
logging.error(f"Error loading model or processor: {e}")
|
17 |
|
18 |
+
def synthesize_speech(text):
|
19 |
+
try:
|
20 |
+
inputs = processor(text, return_tensors="pt")
|
21 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
22 |
+
model.to(device)
|
23 |
+
inputs = inputs.to(device)
|
24 |
|
25 |
+
with torch.no_grad():
|
26 |
+
speech = model.generate(**inputs)
|
27 |
+
|
28 |
+
logging.info("Speech generated successfully.")
|
29 |
+
return processor.decode(speech, skip_special_tokens=True)
|
30 |
+
except Exception as e:
|
31 |
+
logging.error(f"Error during speech synthesis: {e}")
|
32 |
+
return None
|