ejazhabibdar commited on
Commit
9897ed3
1 Parent(s): b1b7a18

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import SpeechT5ForTextToSpeech, SpeechT5Processor, SpeechT5HifiGan
2
+ import torch
3
+ import gradio as gr
4
+
5
+ # Load the TTS model and processor
6
+ model_checkpoint = "ejazhabibdar/speecht5_finetuned_voxpopuli_nl"
7
+ model = SpeechT5ForTextToSpeech.from_pretrained(model_checkpoint)
8
+ processor = SpeechT5Processor.from_pretrained(model_checkpoint)
9
+
10
+ # Load the vocoder for generating speech
11
+ vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
12
+
13
+ # Set the device
14
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
15
+ model.to(device)
16
+ vocoder.to(device)
17
+
18
+ # Set the model to evaluation mode
19
+ model.eval()
20
+ vocoder.eval()
21
+
22
+ # Define the TTS function
23
+ def text_to_speech(text):
24
+ # Preprocess the input text
25
+ inputs = processor(text=text, return_tensors="pt")
26
+ inputs = {k: v.to(device) for k, v in inputs.items()}
27
+
28
+ # Generate speech
29
+ with torch.no_grad():
30
+ speech = model.generate_speech(inputs["input_ids"], vocoder=vocoder)
31
+
32
+ return speech.tolist()
33
+
34
+ # Create a Gradio interface
35
+ iface = gr.Interface(
36
+ fn=text_to_speech,
37
+ inputs="text",
38
+ outputs="audio",
39
+ title="Text-to-Speech",
40
+ description="Enter the text and listen to the generated speech.",
41
+ theme="huggingface",
42
+ )
43
+
44
+ if __name__ == "__main__":
45
+ iface.launch()