Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,55 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import
|
4 |
-
import
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
"
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoModel, AutoTokenizer
|
3 |
import gradio as gr
|
4 |
+
import soundfile as sf
|
5 |
+
import numpy as np
|
6 |
+
import tempfile
|
7 |
+
|
8 |
+
# Load model and tokenizer
|
9 |
+
device = "cpu" # or "cuda" if available
|
10 |
+
model = AutoModel.from_pretrained("ai4bharat/vits_rasa_13", trust_remote_code=True).to(device)
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained("ai4bharat/vits_rasa_13", trust_remote_code=True)
|
12 |
+
|
13 |
+
# Mapping: language -> speaker_id
|
14 |
+
LANG_SPEAKER_MAP = {
|
15 |
+
"asm": 0, "ben": 2, "brx": 4, "doi": 6,
|
16 |
+
"kan": 8, "mai": 10, "mal": 11,
|
17 |
+
"mar": 13, "nep": 14, "pan": 16,
|
18 |
+
"san": 17, "tam": 18, "tel": 19,
|
19 |
+
"hin": 13 # use Marathi Male voice for Hindi (close)
|
20 |
+
}
|
21 |
+
|
22 |
+
# Mapping: Style (fixed default)
|
23 |
+
DEFAULT_STYLE_ID = 0 # ALEXA
|
24 |
+
|
25 |
+
def tts_from_json(json_input):
|
26 |
+
try:
|
27 |
+
text = json_input["text"]
|
28 |
+
lang = json_input["language"].lower()
|
29 |
+
|
30 |
+
speaker_id = LANG_SPEAKER_MAP.get(lang)
|
31 |
+
if speaker_id is None:
|
32 |
+
return f"Language '{lang}' not supported."
|
33 |
+
|
34 |
+
inputs = tokenizer(text=text, return_tensors="pt").to(device)
|
35 |
+
outputs = model(inputs['input_ids'], speaker_id=speaker_id, emotion_id=DEFAULT_STYLE_ID)
|
36 |
+
|
37 |
+
waveform = outputs.waveform.squeeze().cpu().numpy()
|
38 |
+
sample_rate = model.config.sampling_rate
|
39 |
+
|
40 |
+
# Save to temp file for Gradio playback
|
41 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f:
|
42 |
+
sf.write(f.name, waveform, sample_rate)
|
43 |
+
return sample_rate, waveform
|
44 |
+
except Exception as e:
|
45 |
+
return f"Error: {str(e)}"
|
46 |
+
|
47 |
+
iface = gr.Interface(
|
48 |
+
fn=tts_from_json,
|
49 |
+
inputs=gr.JSON(label="Input JSON: {'text': '...', 'language': 'mar/hin/san'}"),
|
50 |
+
outputs=gr.Audio(label="Generated Audio"),
|
51 |
+
title="VITS TTS for Indian Languages (Marathi, Hindi, Sanskrit)",
|
52 |
+
description="Uses ai4bharat/vits_rasa_13. Supports Marathi, Hindi, and Sanskrit."
|
53 |
+
)
|
54 |
+
|
55 |
+
iface.launch()
|