Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# Load the MMS-TTS Urdu model
|
| 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 |
+
# Function to generate speech from text
|
| 14 |
+
def text_to_speech(urdu_text):
|
| 15 |
+
inputs = tokenizer(urdu_text, return_tensors="pt")
|
| 16 |
+
|
| 17 |
+
with torch.no_grad():
|
| 18 |
+
output = model(**inputs).waveform.numpy()
|
| 19 |
+
|
| 20 |
+
# Save audio as a temporary file
|
| 21 |
+
temp_wav_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|
| 22 |
+
wav.write(temp_wav_file.name, model.config.sampling_rate, output)
|
| 23 |
+
|
| 24 |
+
return temp_wav_file.name # Return file path for playback & download
|
| 25 |
+
|
| 26 |
+
# Gradio interface
|
| 27 |
+
iface = gr.Interface(
|
| 28 |
+
fn=text_to_speech,
|
| 29 |
+
inputs=gr.Textbox(label="Enter Urdu Text", placeholder="یہاں اردو متن درج کریں"),
|
| 30 |
+
outputs=gr.Audio(label="Generated Speech"),
|
| 31 |
+
title="Urdu Text-to-Speech (MMS-TTS)",
|
| 32 |
+
description="یہ ایپلیکیشن آپ کے اردو متن کو مصنوعی آواز میں تبدیل کرتی ہے۔",
|
| 33 |
+
theme="compact"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# Launch the app
|
| 37 |
+
iface.launch()
|