Spaces:
Running
Running
Nimesh Naik
commited on
Commit
·
1061733
1
Parent(s):
e1e20b7
Text to speech_1
Browse files
app.py
CHANGED
@@ -1,7 +1,46 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq
|
4 |
+
import soundfile as sf
|
5 |
+
import tempfile
|
6 |
+
import os
|
7 |
|
8 |
+
# Load processor and model
|
9 |
+
processor = AutoProcessor.from_pretrained("ai4bharat/indic-parler-tts")
|
10 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained("ai4bharat/indic-parler-tts")
|
11 |
+
model.eval()
|
12 |
|
13 |
+
LANGUAGE_OPTIONS = {
|
14 |
+
"Hindi": "hi", "Tamil": "ta", "Telugu": "te", "Malayalam": "ml", "Kannada": "kn",
|
15 |
+
"Bengali": "bn", "Marathi": "mr", "Gujarati": "gu", "Punjabi": "pa",
|
16 |
+
"Odia": "or", "Assamese": "as", "Urdu": "ur", "English (Indian)": "en"
|
17 |
+
}
|
18 |
+
|
19 |
+
def tts_generate(text, language_name):
|
20 |
+
lang = LANGUAGE_OPTIONS[language_name]
|
21 |
+
|
22 |
+
inputs = processor(text=[text], return_tensors="pt", lang=lang)
|
23 |
+
with torch.no_grad():
|
24 |
+
output = model.generate(**inputs)
|
25 |
+
|
26 |
+
audio_arr = processor.decode(output[0], skip_special_tokens=True)
|
27 |
+
|
28 |
+
# Save audio as temporary .wav file
|
29 |
+
temp_path = tempfile.mktemp(suffix=".wav")
|
30 |
+
sf.write(temp_path, audio_arr, 16000)
|
31 |
+
|
32 |
+
return temp_path
|
33 |
+
|
34 |
+
# Gradio Interface
|
35 |
+
interface = gr.Interface(
|
36 |
+
fn=tts_generate,
|
37 |
+
inputs=[
|
38 |
+
gr.Textbox(label="Enter Text"),
|
39 |
+
gr.Dropdown(choices=list(LANGUAGE_OPTIONS.keys()), label="Select Language")
|
40 |
+
],
|
41 |
+
outputs=gr.Audio(label="Generated Audio", type="filepath"),
|
42 |
+
title="Indic Parler TTS - AI4Bharat",
|
43 |
+
description="Enter text and choose a language to generate and download speech audio."
|
44 |
+
)
|
45 |
+
|
46 |
+
interface.launch()
|