Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,35 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
tts_a = TTS(
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
demo = gr.Interface(
|
| 34 |
-
|
| 35 |
-
|
| 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()
|
|
|
|
| 1 |
+
import gradio as gr from TTS.api import TTS from pydub import AudioSegment import tempfile import os import uuid
|
| 2 |
+
|
| 3 |
+
مدلهای زن و مرد فارسی
|
| 4 |
+
|
| 5 |
+
voice_a = 'Kamtera/persian-tts-female-glow_tts' voice_b = 'Kamtera/persian-tts-male-vits'
|
| 6 |
+
|
| 7 |
+
Preload مدلها قبل از رابط کاربری
|
| 8 |
+
|
| 9 |
+
tts_a = TTS(voice_a) tts_b = TTS(voice_b)
|
| 10 |
+
|
| 11 |
+
def tts_dialogue_offline(text, pause_ms=300): # پاکسازی متن از کاراکترهای غیرمجاز text = text.replace('\u200c','')
|
| 12 |
+
|
| 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( fn=tts_dialogue_offline, inputs=[ gr.Textbox(label="متن گفتوگو (A و B)", lines=8, placeholder="A: سلام، خوبی؟\nB: ممنون، تو چطوری؟"), gr.Slider(100, 1000, value=300, step=50, label="فاصله بین دیالوگها (میلیثانیه)"), ], outputs=gr.Audio(label="فایل صوتی ترکیبی نهایی", type="filepath"), title="🗣️ گفتوگوی دو نفره فارسی آفلاین", description="هر خط گفتوگو را با A: یا B: شروع کنید. خروجی ترکیبی با دو صدای فارسی تولید میشود.", )
|
| 34 |
+
|
| 35 |
+
if name == "main": demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|