Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor
|
| 3 |
+
import torch
|
| 4 |
+
import tempfile
|
| 5 |
+
|
| 6 |
+
# مدل TTS فارسی
|
| 7 |
+
MODEL_ID = "harveenchadha/vits-fa"
|
| 8 |
+
|
| 9 |
+
processor = AutoProcessor.from_pretrained(MODEL_ID)
|
| 10 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained(MODEL_ID)
|
| 11 |
+
|
| 12 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
+
model = model.to(device)
|
| 14 |
+
|
| 15 |
+
def tts_batch(texts):
|
| 16 |
+
outputs = []
|
| 17 |
+
for idx, text in enumerate(texts):
|
| 18 |
+
if not text.strip():
|
| 19 |
+
outputs.append(None)
|
| 20 |
+
continue
|
| 21 |
+
inputs = processor(text=text, return_tensors="pt").to(device)
|
| 22 |
+
with torch.no_grad():
|
| 23 |
+
speech = model.generate(**inputs)
|
| 24 |
+
# فایل موقتی بساز برای هر خروجی
|
| 25 |
+
temp_wav = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|
| 26 |
+
processor.save_wav(speech, temp_wav.name)
|
| 27 |
+
outputs.append(temp_wav.name)
|
| 28 |
+
return outputs
|
| 29 |
+
|
| 30 |
+
# رابط کاربری با ۵ ورودی متن
|
| 31 |
+
with gr.Blocks(title="Persian Multi-Text TTS") as demo:
|
| 32 |
+
gr.Markdown("## 🎙️ مبدل همزمان متن به گفتار فارسی (۵ ورودی)")
|
| 33 |
+
gr.Markdown("متنهای زیر را پر کن تا پنج فایل صوتی جداگانه بسازد:")
|
| 34 |
+
|
| 35 |
+
with gr.Row():
|
| 36 |
+
text1 = gr.Textbox(label="متن ۱", lines=2)
|
| 37 |
+
text2 = gr.Textbox(label="متن ۲", lines=2)
|
| 38 |
+
text3 = gr.Textbox(label="متن ۳", lines=2)
|
| 39 |
+
text4 = gr.Textbox(label="متن ۴", lines=2)
|
| 40 |
+
text5 = gr.Textbox(label="متن ۵", lines=2)
|
| 41 |
+
|
| 42 |
+
btn = gr.Button("🔊 تبدیل به صدا")
|
| 43 |
+
|
| 44 |
+
with gr.Row():
|
| 45 |
+
out1 = gr.Audio(label="خروجی ۱", type="filepath")
|
| 46 |
+
out2 = gr.Audio(label="خروجی ۲", type="filepath")
|
| 47 |
+
out3 = gr.Audio(label="خروجی ۳", type="filepath")
|
| 48 |
+
out4 = gr.Audio(label="خروجی ۴", type="filepath")
|
| 49 |
+
out5 = gr.Audio(label="خروجی ۵", type="filepath")
|
| 50 |
+
|
| 51 |
+
btn.click(
|
| 52 |
+
fn=tts_batch,
|
| 53 |
+
inputs=[text1, text2, text3, text4, text5],
|
| 54 |
+
outputs=[out1, out2, out3, out4, out5]
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
demo.launch()
|