Rezfars commited on
Commit
3e5441c
·
verified ·
1 Parent(s): 949bd8f

Update app.py

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