Update app.py
Browse files
app.py
CHANGED
@@ -5,45 +5,45 @@ import scipy.io.wavfile as wav
|
|
5 |
import numpy as np
|
6 |
import tempfile
|
7 |
|
8 |
-
#
|
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 |
-
|
18 |
-
|
19 |
-
#
|
20 |
-
inputs = tokenizer(urdu_text, return_tensors="pt", padding=True, truncation=True)
|
21 |
-
|
22 |
-
#
|
23 |
if inputs["input_ids"].size(1) == 0:
|
24 |
-
|
25 |
-
|
26 |
-
#
|
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="
|
42 |
-
outputs=gr.Audio(label="
|
43 |
-
title="
|
44 |
description="یہ ایپلیکیشن آپ کے اردو متن کو مصنوعی آواز میں تبدیل کرتی ہے۔",
|
45 |
theme="default"
|
46 |
)
|
47 |
|
48 |
-
#
|
49 |
iface.launch()
|
|
|
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()
|