Spaces:
Build error
Build error
DSatishchandra
commited on
Commit
•
4649eb9
1
Parent(s):
13ac391
Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,35 @@
|
|
1 |
-
import
|
2 |
from speechbrain.pretrained import Tacotron2, HIFIGAN
|
3 |
-
import
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
vocoder = HIFIGAN.from_hparams(source="speechbrain/tts-hifigan-ljspeech", savedir="tmpdir_vocoder")
|
9 |
-
except Exception as e:
|
10 |
-
print(f"Error loading the TTS model: {e}")
|
11 |
-
exit(1)
|
12 |
-
|
13 |
-
# Function to speak text using Speechbrain's Tacotron2 and HIFIGAN
|
14 |
-
def speak(text):
|
15 |
-
print(f"Speaking: {text}")
|
16 |
-
try:
|
17 |
-
# Generate the mel spectrogram from text
|
18 |
-
mel_output, mel_length, alignment = tts.encode_text(text)
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
|
30 |
-
#
|
31 |
-
|
32 |
-
recognizer = sr.Recognizer()
|
33 |
-
with sr.Microphone() as source:
|
34 |
-
print("Listening for command...")
|
35 |
-
audio = recognizer.listen(source)
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
|
46 |
-
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
while True:
|
51 |
-
command = listen()
|
52 |
-
if command:
|
53 |
-
speak(command)
|
|
|
1 |
+
import logging
|
2 |
from speechbrain.pretrained import Tacotron2, HIFIGAN
|
3 |
+
import torch
|
4 |
+
import os
|
5 |
|
6 |
+
# Set up logging
|
7 |
+
logging.basicConfig(level=logging.INFO)
|
8 |
+
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
try:
|
11 |
+
# Load TTS model and vocoder
|
12 |
+
logger.info("Loading Tacotron2 model for TTS...")
|
13 |
+
tts_model = Tacotron2.from_hparams(source="speechbrain/tts-tacotron2-ljspeech", savedir="tmpdir_tts")
|
14 |
+
logger.info("Tacotron2 model loaded successfully!")
|
15 |
|
16 |
+
logger.info("Loading HIFIGAN vocoder...")
|
17 |
+
vocoder = HIFIGAN.from_hparams(source="speechbrain/tts-hifigan-ljspeech", savedir="tmpdir_vocoder")
|
18 |
+
logger.info("HIFIGAN vocoder loaded successfully!")
|
19 |
|
20 |
+
# Define the text to synthesize
|
21 |
+
text = "Hello, I am an AI voice assistant. How can I help you today?"
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
# Run TTS and Vocoder to generate the audio
|
24 |
+
mel_output, mel_length, alignment = tts_model.encode_text(text)
|
25 |
+
waveforms, _ = vocoder.decode_batch(mel_output)
|
26 |
+
|
27 |
+
# Save the generated waveform as an audio file
|
28 |
+
audio_output_path = "output_audio.wav"
|
29 |
+
logger.info(f"Saving audio to {audio_output_path}...")
|
30 |
+
torch.save(waveforms.squeeze(1), audio_output_path)
|
31 |
|
32 |
+
logger.info(f"Audio saved successfully to {audio_output_path}!")
|
33 |
|
34 |
+
except Exception as e:
|
35 |
+
logger.error(f"Error during the TTS process: {str(e)}")
|
|
|
|
|
|
|
|