Rezfars commited on
Commit
d3d6012
·
verified ·
1 Parent(s): e1987b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -53
app.py CHANGED
@@ -1,58 +1,20 @@
1
- import gradio as gr
2
- import pyttsx3
3
- from pydub import AudioSegment
4
- import tempfile
5
- import os
6
 
7
- def tts_dialogue(text, speed_a, tone_a, speed_b, tone_b):
8
- lines = [l.strip() for l in text.split("\n") if l.strip()]
9
- engine = pyttsx3.init()
10
- voices = engine.getProperty("voices")
11
- voice_a = voices[0].id if len(voices) > 0 else None
12
- voice_b = voices[1].id if len(voices) > 1 else voices[0].id
13
 
14
- combined_audio = AudioSegment.silent(duration=0)
 
 
 
 
 
 
 
15
 
16
- for line in lines:
17
- if line.startswith("A:"):
18
- voice = voice_a
19
- rate = 150 + int(speed_a)
20
- elif line.startswith("B:"):
21
- voice = voice_b
22
- rate = 150 + int(speed_b)
23
- else:
24
- voice = voice_a
25
- rate = 150
26
 
27
- text_to_speak = line.split(":", 1)[-1].strip()
28
- engine.setProperty("voice", voice)
29
- engine.setProperty("rate", rate)
30
 
31
- tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
32
- engine.save_to_file(text_to_speak, tmp_file.name)
33
- engine.runAndWait()
34
- segment = AudioSegment.from_wav(tmp_file.name)
35
- combined_audio += segment + AudioSegment.silent(duration=300)
36
- os.remove(tmp_file.name)
37
-
38
- out_path = tempfile.NamedTemporaryFile(delete=False, suffix=".wav").name
39
- combined_audio.export(out_path, format="wav")
40
- return out_path
41
-
42
-
43
- demo = gr.Interface(
44
- fn=tts_dialogue,
45
- inputs=[
46
- gr.Textbox(label="متن گفت‌وگو (A و B)", lines=8, placeholder="A: سلام، خوبی؟\nB: ممنون، تو چطوری؟"),
47
- gr.Slider(-50, 50, value=0, step=5, label="سرعت صدا A"),
48
- gr.Slider(-50, 50, value=0, step=5, label="لحن صدا A (pitch)"),
49
- gr.Slider(-50, 50, value=0, step=5, label="سرعت صدا B"),
50
- gr.Slider(-50, 50, value=0, step=5, label="لحن صدا B (pitch)"),
51
- ],
52
- outputs=gr.Audio(label="فایل صوتی ترکیبی نهایی", type="filepath"),
53
- title="🗣️ گفت‌وگوی دو نفره فارسی بدون API",
54
- description="هر خط گفت‌وگو را با A: یا B: شروع کنید. خروجی ترکیبی با دو صدا تولید می‌شود."
55
- )
56
-
57
- if __name__ == "__main__":
58
- demo.launch()
 
1
+ import gradio as gr from gtts import gTTS from pydub import AudioSegment import tempfile import os
 
 
 
 
2
 
3
+ def tts_dialogue_gtts(text, pause_ms=300): """ تولید فایل صوتی ترکیبی از گفت‌وگوی دو نفره فارسی با gTTS. هر خط با A: یا B: شروع شود. :param text: متن گفت‌وگو :param pause_ms: فاصله بین دیالوگ‌ها به میلی‌ثانیه :return: مسیر فایل WAV ترکیبی """ lines = [l.strip() for l in text.split("\n") if l.strip()] combined_audio = AudioSegment.silent(duration=0)
 
 
 
 
 
4
 
5
+ for line in lines:
6
+ speaker_text = line.split(":", 1)[-1].strip()
7
+ tts = gTTS(text=speaker_text, lang="fa")
8
+ tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
9
+ tts.save(tmp.name)
10
+ segment = AudioSegment.from_mp3(tmp.name)
11
+ combined_audio += segment + AudioSegment.silent(duration=pause_ms)
12
+ os.remove(tmp.name)
13
 
14
+ out_path = tempfile.NamedTemporaryFile(delete=False, suffix=".wav").name
15
+ combined_audio.export(out_path, format="wav")
16
+ return out_path
 
 
 
 
 
 
 
17
 
18
+ demo = gr.Interface( fn=tts_dialogue_gtts, 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="🗣️ گفت‌وگوی دو نفره فارسی بدون API", description="هر خط گفت‌وگو را با A: یا B: شروع کنید. خروجی ترکیبی با gTTS تولید می‌شود.", )
 
 
19
 
20
+ if name == "main": demo.launch()