Update tts.py
Browse files
tts.py
CHANGED
@@ -1,42 +1,19 @@
|
|
1 |
-
import torch
|
2 |
-
from transformers import SpeechT5ForTextToSpeech, SpeechT5Processor
|
3 |
-
import logging
|
4 |
import numpy as np
|
5 |
-
import soundfile as sf
|
6 |
import torchaudio
|
7 |
-
|
8 |
-
# Set up logging
|
9 |
-
logging.basicConfig(level=logging.DEBUG)
|
10 |
-
|
11 |
-
MODEL_ID = "microsoft/speecht5_tts"
|
12 |
-
|
13 |
-
# Try to load the model and processor
|
14 |
-
try:
|
15 |
-
processor = SpeechT5Processor.from_pretrained(MODEL_ID)
|
16 |
-
model = SpeechT5ForTextToSpeech.from_pretrained(MODEL_ID)
|
17 |
-
logging.info("Model and processor loaded successfully.")
|
18 |
-
except Exception as e:
|
19 |
-
logging.error(f"Error loading model or processor: {e}")
|
20 |
|
21 |
def synthesize_speech(text):
|
22 |
try:
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
with torch.no_grad():
|
29 |
-
speech = model.generate(**inputs)
|
30 |
-
|
31 |
-
logging.info("Speech generated successfully.")
|
32 |
|
33 |
-
#
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
torchaudio.save(file_path, torch.tensor(waveform).unsqueeze(0), 16000)
|
38 |
-
logging.info(f"Audio file saved successfully at {file_path}.")
|
39 |
return file_path
|
40 |
except Exception as e:
|
41 |
-
logging.error(f"Error during
|
42 |
return None
|
|
|
|
|
|
|
|
|
1 |
import numpy as np
|
|
|
2 |
import torchaudio
|
3 |
+
import logging
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
def synthesize_speech(text):
|
6 |
try:
|
7 |
+
# Generate a simple sine wave for testing
|
8 |
+
sr = 16000
|
9 |
+
t = np.linspace(0, 1, sr)
|
10 |
+
waveform = 0.5 * np.sin(2 * np.pi * 440 * t).astype(np.float32)
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
# Save the sine wave to a file
|
13 |
+
file_path = "/tmp/output.wav"
|
14 |
+
torchaudio.save(file_path, torch.tensor(waveform).unsqueeze(0), sr)
|
15 |
+
logging.info(f"Test audio file saved successfully at {file_path}.")
|
|
|
|
|
16 |
return file_path
|
17 |
except Exception as e:
|
18 |
+
logging.error(f"Error during test audio generation: {e}")
|
19 |
return None
|