murugappanp commited on
Commit
e5c1dc8
·
verified ·
1 Parent(s): 2e79b79

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -17
app.py CHANGED
@@ -1,38 +1,44 @@
1
  import gradio as gr
2
  import torch
3
- from transformers import AutoProcessor, BarkModel
4
- from scipy.io.wavfile import write
 
5
  import tempfile
6
 
7
- # Load model and processor (Free Hugging Face model)
8
- processor = AutoProcessor.from_pretrained("suno/bark-small")
9
- model = BarkModel.from_pretrained("suno/bark-small")
10
 
11
  device = "cuda" if torch.cuda.is_available() else "cpu"
12
  model = model.to(device)
13
 
14
  def text_to_speech(text):
15
- inputs = processor(text, return_tensors="pt").to(device)
16
-
 
 
 
17
  with torch.no_grad():
18
- audio_array = model.generate(**inputs)
19
-
20
- audio_array = audio_array.cpu().numpy().squeeze()
21
 
22
- # Save temporary WAV file
23
- sample_rate = model.generation_config.sample_rate
 
 
 
24
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
25
- write(temp_file.name, sample_rate, audio_array)
26
-
27
  return temp_file.name
28
 
29
- # Gradio Interface
30
  demo = gr.Interface(
31
  fn=text_to_speech,
32
  inputs=gr.Textbox(label="Enter Text"),
33
  outputs=gr.Audio(label="Generated Speech"),
34
- title="Simple Text-to-Speech App",
35
- description="Enter text and generate speech using a free Hugging Face model."
36
  )
37
 
38
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  import torch
3
+ from transformers import VitsModel, AutoTokenizer
4
+ import numpy as np
5
+ import scipy.io.wavfile
6
  import tempfile
7
 
8
+ # Load lightweight MMS TTS model
9
+ model = VitsModel.from_pretrained("facebook/mms-tts-eng")
10
+ tokenizer = AutoTokenizer.from_pretrained("facebook/mms-tts-eng")
11
 
12
  device = "cuda" if torch.cuda.is_available() else "cpu"
13
  model = model.to(device)
14
 
15
  def text_to_speech(text):
16
+ if text.strip() == "":
17
+ return None
18
+
19
+ inputs = tokenizer(text, return_tensors="pt").to(device)
20
+
21
  with torch.no_grad():
22
+ output = model(**inputs).waveform
23
+
24
+ audio = output.squeeze().cpu().numpy()
25
 
26
+ # Normalize audio
27
+ audio = audio / np.max(np.abs(audio))
28
+
29
+ # Save to temporary WAV file
30
+ sample_rate = model.config.sampling_rate
31
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
32
+ scipy.io.wavfile.write(temp_file.name, sample_rate, audio.astype(np.float32))
33
+
34
  return temp_file.name
35
 
 
36
  demo = gr.Interface(
37
  fn=text_to_speech,
38
  inputs=gr.Textbox(label="Enter Text"),
39
  outputs=gr.Audio(label="Generated Speech"),
40
+ title="Fast Text-to-Speech (Lightweight)",
41
+ description="Fast and accurate TTS using facebook/mms-tts-eng"
42
  )
43
 
44
  if __name__ == "__main__":