Spaces:
Build error
Build error
Updated app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,34 @@
|
|
|
|
1 |
import torch
|
2 |
from parler_tts import ParlerTTSForConditionalGeneration
|
3 |
from transformers import AutoTokenizer
|
4 |
import soundfile as sf
|
5 |
-
from google.generativeai import GenerativeModel
|
6 |
import gradio as gr
|
7 |
|
8 |
-
#
|
9 |
-
|
|
|
|
|
10 |
|
11 |
-
#
|
|
|
12 |
tts_model = ParlerTTSForConditionalGeneration.from_pretrained("ai4bharat/indic-parler-tts").to(device)
|
13 |
tts_tokenizer = AutoTokenizer.from_pretrained("ai4bharat/indic-parler-tts")
|
14 |
desc_tokenizer = AutoTokenizer.from_pretrained(tts_model.config.text_encoder._name_or_path)
|
15 |
|
16 |
-
# 2. Load Gemini (API key via Secrets)
|
17 |
-
gemini = GenerativeModel('gemini-pro')
|
18 |
-
|
19 |
def hinglish_to_devnagri(text):
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
24 |
|
25 |
def generate_speech(text):
|
26 |
-
# Convert to Devnagri
|
27 |
hindi_text = hinglish_to_devnagri(text)
|
28 |
|
29 |
-
# Generate speech
|
30 |
desc = "एक महिला वक्ता स्पष्ट हिंदी में बोल रही हैं"
|
31 |
desc_inputs = desc_tokenizer(desc, return_tensors="pt").to(device)
|
32 |
text_inputs = tts_tokenizer(hindi_text, return_tensors="pt").to(device)
|
@@ -38,20 +40,19 @@ def generate_speech(text):
|
|
38 |
prompt_attention_mask=text_inputs.attention_mask
|
39 |
)
|
40 |
|
41 |
-
# Save as WAV
|
42 |
sf.write("output.wav", audio.cpu().numpy().squeeze(), tts_model.config.sampling_rate)
|
43 |
return "output.wav", hindi_text
|
44 |
|
45 |
# Gradio UI
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
gr.
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
)
|
56 |
|
57 |
-
|
|
|
1 |
+
import os
|
2 |
import torch
|
3 |
from parler_tts import ParlerTTSForConditionalGeneration
|
4 |
from transformers import AutoTokenizer
|
5 |
import soundfile as sf
|
6 |
+
from google.generativeai import GenerativeModel, configure
|
7 |
import gradio as gr
|
8 |
|
9 |
+
# 1. Load Gemini (API key from environment variables)
|
10 |
+
GEMINI_KEY = os.environ.get('GEMINI_API_KEY') # Will read from HF Secrets
|
11 |
+
configure(api_key=GEMINI_KEY) # Initialize Gemini
|
12 |
+
gemini = GenerativeModel('gemini-pro')
|
13 |
|
14 |
+
# 2. Load Indic-TTS
|
15 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
16 |
tts_model = ParlerTTSForConditionalGeneration.from_pretrained("ai4bharat/indic-parler-tts").to(device)
|
17 |
tts_tokenizer = AutoTokenizer.from_pretrained("ai4bharat/indic-parler-tts")
|
18 |
desc_tokenizer = AutoTokenizer.from_pretrained(tts_model.config.text_encoder._name_or_path)
|
19 |
|
|
|
|
|
|
|
20 |
def hinglish_to_devnagri(text):
|
21 |
+
try:
|
22 |
+
response = gemini.generate_content(
|
23 |
+
f"Convert this Hinglish to Devnagri Hindi while complex words are translated like (ex: compiler -> कंपाइलर) :\n{text}\n\nOutput only the translation."
|
24 |
+
)
|
25 |
+
return response.text
|
26 |
+
except Exception as e:
|
27 |
+
raise gr.Error(f"Gemini error: {str(e)}")
|
28 |
|
29 |
def generate_speech(text):
|
|
|
30 |
hindi_text = hinglish_to_devnagri(text)
|
31 |
|
|
|
32 |
desc = "एक महिला वक्ता स्पष्ट हिंदी में बोल रही हैं"
|
33 |
desc_inputs = desc_tokenizer(desc, return_tensors="pt").to(device)
|
34 |
text_inputs = tts_tokenizer(hindi_text, return_tensors="pt").to(device)
|
|
|
40 |
prompt_attention_mask=text_inputs.attention_mask
|
41 |
)
|
42 |
|
|
|
43 |
sf.write("output.wav", audio.cpu().numpy().squeeze(), tts_model.config.sampling_rate)
|
44 |
return "output.wav", hindi_text
|
45 |
|
46 |
# Gradio UI
|
47 |
+
with gr.Blocks() as app:
|
48 |
+
gr.Markdown("## 🚀 Hinglish-to-Speech (Gemini + Indic-TTS)")
|
49 |
+
with gr.Row():
|
50 |
+
inp = gr.Textbox(label="Enter Hinglish Text", placeholder="Aaj mood nahi hai...")
|
51 |
+
btn = gr.Button("Generate")
|
52 |
+
with gr.Row():
|
53 |
+
audio_out = gr.Audio(label="Speech Output")
|
54 |
+
text_out = gr.Textbox(label="Devnagri Translation")
|
55 |
+
|
56 |
+
btn.click(fn=generate_speech, inputs=inp, outputs=[audio_out, text_out])
|
57 |
|
58 |
+
app.launch()
|