Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,57 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from transformers import VitsModel, AutoTokenizer
|
4 |
-
import scipy.io.wavfile as wav
|
5 |
-
import numpy as np
|
6 |
-
import tempfile
|
7 |
|
8 |
-
#
|
9 |
-
|
|
|
10 |
model = VitsModel.from_pretrained(model_name)
|
11 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
12 |
|
13 |
-
#
|
14 |
-
def
|
15 |
-
#
|
16 |
-
|
17 |
-
return "براہِ مہربانی، درست اردو متن درج کریں۔"
|
18 |
-
|
19 |
-
# ٹوکنائزیشن
|
20 |
-
inputs = tokenizer(urdu_text, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
21 |
-
|
22 |
-
# ٹوکنائزیشن کے بعد ان پٹ کی جانچ
|
23 |
-
if inputs["input_ids"].size(1) == 0:
|
24 |
-
return "ٹوکنائزیشن کے بعد ان پٹ خالی ہے۔ براہِ مہربانی، متن کی جانچ کریں اور دوبارہ کوشش کریں۔"
|
25 |
-
|
26 |
-
# input_ids کو LongTensor میں تبدیل کریں
|
27 |
-
inputs["input_ids"] = inputs["input_ids"].to(torch.long)
|
28 |
|
|
|
29 |
with torch.no_grad():
|
30 |
-
output = model(**inputs).waveform
|
31 |
-
|
32 |
-
#
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from transformers import VitsModel, AutoTokenizer
|
|
|
|
|
|
|
4 |
|
5 |
+
# 1. Load the model (Nastaliq-based) and tokenizer
|
6 |
+
# This checkpoint is intended for Urdu text in its traditional (Nastaliq) script.
|
7 |
+
model_name = "facebook/mms-tts-urd"
|
8 |
model = VitsModel.from_pretrained(model_name)
|
9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
|
11 |
+
# 2. Define the inference function
|
12 |
+
def generate_urdu_speech(urdu_text):
|
13 |
+
# Tokenize the input text
|
14 |
+
inputs = tokenizer(urdu_text, return_tensors="pt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
# Perform inference with the model
|
17 |
with torch.no_grad():
|
18 |
+
output = model(**inputs).waveform
|
19 |
+
|
20 |
+
# Convert PyTorch tensor to NumPy array
|
21 |
+
waveform = output.squeeze().cpu().numpy()
|
22 |
+
sample_rate = model.config.sampling_rate
|
23 |
+
|
24 |
+
# Gradio’s Audio component expects (sample_rate, audio_data)
|
25 |
+
return (sample_rate, waveform)
|
26 |
+
|
27 |
+
# 3. Build the Gradio interface
|
28 |
+
with gr.Blocks() as demo:
|
29 |
+
gr.Markdown("""
|
30 |
+
# Urdu TTS Demo (Nastaliq Script)
|
31 |
+
|
32 |
+
Enter text in Urdu (Nastaliq) script, and this demo will synthesize speech using the Facebook MMS TTS model for Urdu.
|
33 |
+
""")
|
34 |
+
|
35 |
+
# Text input for Urdu (Nastaliq)
|
36 |
+
text_input = gr.Textbox(
|
37 |
+
label="Enter Urdu text",
|
38 |
+
placeholder="مثال کے طور پر...",
|
39 |
+
lines=3
|
40 |
+
)
|
41 |
+
|
42 |
+
# Audio output
|
43 |
+
audio_output = gr.Audio(label="Generated Urdu Speech", type="numpy")
|
44 |
+
|
45 |
+
# Generate button
|
46 |
+
generate_button = gr.Button("Generate Speech")
|
47 |
+
|
48 |
+
# Wire up the button to the function
|
49 |
+
generate_button.click(
|
50 |
+
fn=generate_urdu_speech,
|
51 |
+
inputs=text_input,
|
52 |
+
outputs=audio_output
|
53 |
+
)
|
54 |
+
|
55 |
+
# 4. Launch the Gradio app
|
56 |
+
if __name__ == "__main__":
|
57 |
+
demo.launch()
|