Mudassir-75 commited on
Commit
d4af064
·
verified ·
1 Parent(s): 6013333

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -40
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
- # MMS-TTS اردو ماڈل لوڈ کریں
9
- model_name = "facebook/mms-tts-urd-script_devanagari"
 
10
  model = VitsModel.from_pretrained(model_name)
11
  tokenizer = AutoTokenizer.from_pretrained(model_name)
12
 
13
- # متن کو آواز میں تبدیل کرنے کا فنکشن
14
- def text_to_speech(urdu_text):
15
- # ان پٹ کی جانچ کریں
16
- if not urdu_text.strip():
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.squeeze().numpy()
31
-
32
- # آڈیو کو عارضی فائل میں محفوظ کریں
33
- temp_wav_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
34
- wav.write(temp_wav_file.name, model.config.sampling_rate, output)
35
-
36
- return temp_wav_file.name # پلے بیک اور ڈاؤن لوڈ کے لیے فائل کا راستہ واپس کریں
37
-
38
- # Gradio انٹرفیس
39
- iface = gr.Interface(
40
- fn=text_to_speech,
41
- inputs=gr.Textbox(label="اردو متن درج کریں", placeholder="یہاں اردو متن درج کریں"),
42
- outputs=gr.Audio(label="تخلیق شدہ آواز"),
43
- title="اردو ٹیکسٹ ٹو اسپیچ (MMS-TTS)",
44
- description="یہ ایپلیکیشن آپ کے اردو متن کو مصنوعی آواز میں تبدیل کرتی ہے۔",
45
- theme="default"
46
- )
47
-
48
- # ایپ لانچ کریں
49
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()