Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,46 @@
|
|
| 1 |
-
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
Preload مدلها قبل از رابط کاربری برای جلوگیری از ارور هنگام کلیک روی دکمه
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
if line.startswith("A:"):
|
| 13 |
-
tts_a.tts_to_file(text=speaker_text, file_path=tmp_file.name)
|
| 14 |
-
elif line.startswith("B:"):
|
| 15 |
-
tts_b.tts_to_file(text=speaker_text, file_path=tmp_file.name)
|
| 16 |
-
else:
|
| 17 |
-
tts_a.tts_to_file(text=speaker_text, file_path=tmp_file.name)
|
| 18 |
-
segment = AudioSegment.from_wav(tmp_file.name)
|
| 19 |
-
combined_audio += segment + AudioSegment.silent(duration=pause_ms)
|
| 20 |
-
os.remove(tmp_file.name)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
if name == "main": demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from TTS.api import TTS
|
| 3 |
+
from pydub import AudioSegment
|
| 4 |
+
import tempfile
|
| 5 |
+
import os
|
| 6 |
+
import uuid
|
| 7 |
|
| 8 |
+
# Preload مدلها قبل از رابط کاربری برای جلوگیری از ارور هنگام کلیک روی دکمه
|
| 9 |
+
tts_a = TTS('Kamtera/persian-tts-male-vits') # مدل سبکتر فارسی
|
| 10 |
+
tts_b = TTS('Kamtera/persian-tts-female-glow_tts') # مدل سبکتر انگلیسی (مثال)
|
| 11 |
|
| 12 |
+
def tts_dialogue_offline(text, pause_ms=300):
|
| 13 |
+
lines = [l.strip() for l in text.split("\n") if l.strip()]
|
| 14 |
+
combined_audio = AudioSegment.silent(duration=0)
|
| 15 |
|
| 16 |
+
for line in lines:
|
| 17 |
+
speaker_text = line.split(":", 1)[-1].strip()
|
| 18 |
+
tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|
| 19 |
+
if line.startswith("A:"):
|
| 20 |
+
tts_a.tts_to_file(text=speaker_text, file_path=tmp_file.name)
|
| 21 |
+
elif line.startswith("B:"):
|
| 22 |
+
tts_b.tts_to_file(text=speaker_text, file_path=tmp_file.name)
|
| 23 |
+
else:
|
| 24 |
+
tts_a.tts_to_file(text=speaker_text, file_path=tmp_file.name)
|
| 25 |
+
segment = AudioSegment.from_wav(tmp_file.name)
|
| 26 |
+
combined_audio += segment + AudioSegment.silent(duration=pause_ms)
|
| 27 |
+
os.remove(tmp_file.name)
|
| 28 |
|
| 29 |
+
out_path = tempfile.NamedTemporaryFile(delete=False, suffix=".wav").name
|
| 30 |
+
combined_audio.export(out_path, format="wav")
|
| 31 |
+
return out_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
demo = gr.Interface(
|
| 34 |
+
fn=tts_dialogue_offline,
|
| 35 |
+
inputs=[
|
| 36 |
+
gr.Textbox(label="متن گفتوگو (A و B)", lines=8,
|
| 37 |
+
placeholder="A: سلام، خوبی؟\nB: ممنون، تو چطوری؟"),
|
| 38 |
+
gr.Slider(100, 1000, value=300, step=50, label="فاصله بین دیالوگها (میلیثانیه)"),
|
| 39 |
+
],
|
| 40 |
+
outputs=gr.Audio(label="فایل صوتی ترکیبی نهایی", type="filepath"),
|
| 41 |
+
title="🗣️ گفتوگوی دو نفره فارسی آفلاین",
|
| 42 |
+
description="هر خط گفتوگو را با A: یا B: شروع کنید. خروجی ترکیبی با مدل Coqui TTS تولید میشود.",
|
| 43 |
+
)
|
| 44 |
|
| 45 |
+
if __name__ == "__main__":
|
| 46 |
+
demo.launch()
|
|
|