Update app.py
Browse files
app.py
CHANGED
@@ -12,13 +12,22 @@ tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
12 |
|
13 |
# Function to generate speech from text
|
14 |
def text_to_speech(urdu_text):
|
15 |
-
|
|
|
|
|
16 |
|
17 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
inputs["input_ids"] = inputs["input_ids"].to(torch.long)
|
19 |
|
20 |
with torch.no_grad():
|
21 |
-
output = model(**inputs).waveform.numpy()
|
22 |
|
23 |
# Save audio as a temporary file
|
24 |
temp_wav_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|
|
|
12 |
|
13 |
# Function to generate speech from text
|
14 |
def text_to_speech(urdu_text):
|
15 |
+
# Input Validation
|
16 |
+
if not urdu_text.strip():
|
17 |
+
raise ValueError("Input text is empty. Please enter valid Urdu text.")
|
18 |
|
19 |
+
# Tokenize input text
|
20 |
+
inputs = tokenizer(urdu_text, return_tensors="pt", padding=True, truncation=True)
|
21 |
+
|
22 |
+
# Check if input_ids is empty
|
23 |
+
if inputs["input_ids"].size(1) == 0:
|
24 |
+
raise ValueError("Tokenization resulted in empty input. Please check the input text.")
|
25 |
+
|
26 |
+
# Ensure input_ids are 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 |
# Save audio as a temporary file
|
33 |
temp_wav_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|