krishna195 commited on
Commit
10bf035
·
verified ·
1 Parent(s): 45b014a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Install necessary libraries (if not installed)
2
+ # !pip install gradio transformers soundfile torch
3
+
4
+ import gradio as gr
5
+ import torch
6
+ import soundfile as sf
7
+ from transformers import SpeechT5ForTextToSpeech, SpeechT5Processor, SpeechT5HifiGan
8
+
9
+ # Load the pre-trained model, vocoder, and processor
10
+ model = SpeechT5ForTextToSpeech.from_pretrained("krishna195/speecht5_krishna_finatuned")
11
+ vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
12
+ processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
13
+
14
+ # Speaker embeddings for speech generation (replace this with actual embeddings if needed)
15
+ speaker_embeddings = torch.randn(1, 512) # Example speaker embedding size (dummy embeddings)
16
+
17
+ # Function to generate speech from input text
18
+ def text_to_speech(input_text):
19
+ # Process the input text
20
+ inputs = processor(text=input_text, return_tensors="pt")
21
+
22
+ # Generate speech using the model and vocoder
23
+ speech = model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder)
24
+
25
+ # Save the audio to a file (temporary storage)
26
+ output_file = "generated_speech.wav"
27
+ sf.write(output_file, speech.numpy(), 16000)
28
+
29
+ # Return the path to the audio file for Gradio to play it
30
+ return output_file
31
+
32
+ # Create Gradio UI
33
+ iface = gr.Interface(
34
+ fn=text_to_speech,
35
+ inputs="text",
36
+ outputs="audio",
37
+ title="Text to Speech Generator",
38
+ description="Enter the text you want to convert to speech, and the model will generate the corresponding speech.",
39
+ examples=[
40
+ ["Hello, how are you doing today?"],
41
+ ["The CUDA programming model allows parallel computing on GPUs."],
42
+ ["TensorFlow and PyTorch are popular machine learning frameworks."]
43
+ ]
44
+ )
45
+
46
+ # Launch the Gradio interface
47
+ iface.launch()