unijoh commited on
Commit
a90990e
1 Parent(s): 9bf6d10

Update tts.py

Browse files
Files changed (1) hide show
  1. tts.py +8 -2
tts.py CHANGED
@@ -1,6 +1,8 @@
1
  import torch
2
  from transformers import SpeechT5ForTextToSpeech, SpeechT5Processor
3
  import logging
 
 
4
 
5
  # Set up logging
6
  logging.basicConfig(level=logging.DEBUG)
@@ -24,9 +26,13 @@ def synthesize_speech(text):
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
 
1
  import torch
2
  from transformers import SpeechT5ForTextToSpeech, SpeechT5Processor
3
  import logging
4
+ import numpy as np
5
+ import soundfile as sf
6
 
7
  # Set up logging
8
  logging.basicConfig(level=logging.DEBUG)
 
26
 
27
  with torch.no_grad():
28
  speech = model.generate(**inputs)
29
+
30
  logging.info("Speech generated successfully.")
31
+
32
+ # Decode the generated speech and save to an audio file
33
+ waveform = speech.cpu().numpy()
34
+ sf.write("output.wav", waveform, 16000)
35
+ return "output.wav"
36
  except Exception as e:
37
  logging.error(f"Error during speech synthesis: {e}")
38
  return None