youngtsai commited on
Commit
8d75709
1 Parent(s): 89c580c

engine = pyttsx3.init()

Browse files
Files changed (2) hide show
  1. app.py +21 -10
  2. requirements.txt +2 -1
app.py CHANGED
@@ -116,22 +116,33 @@ def main_function(password: str, theme: str, language: str, method: str, rounds:
116
 
117
  return chatbot_dialogue, audio_path, file_name
118
 
119
-
120
  def dialogue_to_audio(dialogue, role1_gender, role2_gender):
121
- # Assuming the dialogue follows the format of alternating roles.
122
- text = ""
123
- for i, item in enumerate(dialogue):
124
- gender = role1_gender if i % 2 == 0 else role2_gender
125
- voice_attribute = "male" if gender == "male" else "female"
126
- # For simplicity, we'll just prepend the voice attribute. In a real-world scenario, you'd use this to select different TTS voices.
127
- text += f"{voice_attribute} voice: {item['content']} "
 
128
 
129
- tts = gTTS(text=text, lang='zh-tw')
130
  file_path = "temp_audio.mp3"
131
- tts.save(file_path)
 
 
 
 
 
 
 
 
 
 
 
132
  return file_path
133
 
134
 
 
135
  if __name__ == "__main__":
136
  gr.Interface(
137
  main_function,
 
116
 
117
  return chatbot_dialogue, audio_path, file_name
118
 
 
119
  def dialogue_to_audio(dialogue, role1_gender, role2_gender):
120
+ engine = pyttsx3.init()
121
+
122
+ # Fetch the list of available voices
123
+ voices = engine.getProperty('voices')
124
+
125
+ # Get voice IDs for male and female voices (you might need to adjust these based on available voices on your system)
126
+ male_voice_id = "com.apple.speech.synthesis.voice.alex" # Example ID for a male voice
127
+ female_voice_id = "com.apple.speech.synthesis.voice.victoria" # Example ID for a female voice
128
 
 
129
  file_path = "temp_audio.mp3"
130
+
131
+ for i, item in enumerate(dialogue):
132
+ gender = role1_gender if i % 2 == 0 else role2_gender
133
+ voice_id = male_voice_id if gender == "male" else female_voice_id
134
+
135
+ # Set the voice
136
+ engine.setProperty('voice', voice_id)
137
+
138
+ # Now, synthesize the speech
139
+ engine.save_to_file(item['content'], file_path)
140
+ engine.runAndWait()
141
+
142
  return file_path
143
 
144
 
145
+
146
  if __name__ == "__main__":
147
  gr.Interface(
148
  main_function,
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  gradio
2
  gtts
3
- openai
 
 
1
  gradio
2
  gtts
3
+ openai
4
+ pyttsx3