youngtsai commited on
Commit
16e04ca
1 Parent(s): 7fac4f2

speech_config = speechsdk.SpeechConfig(subscription=AZURE_API_KEY, region=AZURE_REGION)

Browse files
Files changed (2) hide show
  1. app.py +49 -22
  2. requirements.txt +1 -1
app.py CHANGED
@@ -1,14 +1,16 @@
1
  import gradio as gr
2
- from gtts import gTTS
3
  import json
4
  import os
5
  import openai
6
  import re
7
- import pyttsx3
 
8
 
9
 
10
  PASSWORD = os.environ['PASSWORD']
11
  OPEN_AI_KEY = os.environ['OPEN_AI_KEY']
 
 
12
 
13
 
14
  def validate_and_correct_chat(data, roles=["A", "B"], rounds=2):
@@ -118,30 +120,55 @@ def main_function(password: str, theme: str, language: str, method: str, rounds:
118
  return chatbot_dialogue, audio_path, file_name
119
 
120
  def dialogue_to_audio(dialogue, role1_gender, role2_gender):
121
- engine = pyttsx3.init()
122
-
123
- # Fetch the list of available voices
124
- voices = engine.getProperty('voices')
125
- print(voices)
126
-
127
- # Get voice IDs for male and female voices (you might need to adjust these based on available voices on your system)
128
- male_voice_id = "com.apple.speech.synthesis.voice.alex" # Example ID for a male voice
129
- female_voice_id = "com.apple.speech.synthesis.voice.victoria" # Example ID for a female voice
130
 
131
- file_path = "temp_audio.mp3"
 
 
 
 
 
 
 
 
 
 
132
 
133
- for i, item in enumerate(dialogue):
134
- gender = role1_gender if i % 2 == 0 else role2_gender
135
- voice_id = male_voice_id if gender == "male" else female_voice_id
136
-
137
- # Set the voice
138
- engine.setProperty('voice', voice_id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
 
140
- # Now, synthesize the speech
141
- engine.save_to_file(item['content'], file_path)
142
- engine.runAndWait()
143
 
144
- return file_path
145
 
146
 
147
 
 
1
  import gradio as gr
 
2
  import json
3
  import os
4
  import openai
5
  import re
6
+ import azure.cognitiveservices.speech as speechsdk
7
+
8
 
9
 
10
  PASSWORD = os.environ['PASSWORD']
11
  OPEN_AI_KEY = os.environ['OPEN_AI_KEY']
12
+ AZURE_REGION = os.environ['AZURE_REGION']
13
+ AZURE_API_KEY = os.environ['AZURE_API_KEY']
14
 
15
 
16
  def validate_and_correct_chat(data, roles=["A", "B"], rounds=2):
 
120
  return chatbot_dialogue, audio_path, file_name
121
 
122
  def dialogue_to_audio(dialogue, role1_gender, role2_gender):
123
+ # Configure Azure Speech Service
124
+ speech_config = speechsdk.SpeechConfig(subscription=AZURE_API_KEY, region=AZURE_REGION)
125
+ filename="dialogue_output.wav"
126
+ audio_config = speechsdk.audio.AudioOutputConfig(filename=filename)
127
+ speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)
 
 
 
 
128
 
129
+ # Map genders to Azure TTS voices (This is for demonstration purposes; you may wish to have more sophisticated voice mapping.)
130
+ voice_map = {
131
+ "male": {
132
+ "中文": "zh-CN-HanHanNeural",
133
+ "英文": "en-US-GuyNeural"
134
+ },
135
+ "female": {
136
+ "中文": "zh-CN-XiaoxiaoNeural",
137
+ "英文": "en-US-JessaNeural"
138
+ }
139
+ }
140
 
141
+ # Convert dialogue list to text
142
+ dialogue_text = ""
143
+ for entry in dialogue:
144
+ role = entry["role"]
145
+ content = entry["content"]
146
+
147
+ # Set voice based on role and language
148
+ if role == role1_gender:
149
+ voice = voice_map[role1_gender][content[-2:]]
150
+ else:
151
+ voice = voice_map[role2_gender][content[-2:]]
152
+
153
+ # Append SSML-formatted content
154
+ dialogue_text += f"<voice name='{voice}'>{content[:-3]}</voice> "
155
+
156
+ ssml = f"""
157
+ <speak version='1.0' xmlns='http://www.w3.org/2001/10/synthesis'>
158
+ {dialogue_text}
159
+ </speak>
160
+ """
161
+
162
+ # Perform synthesis
163
+ result = speech_synthesizer.speak_ssml(ssml)
164
+ if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
165
+ print("Audio synthesized successfully!")
166
+ else:
167
+ print("Error synthesizing audio:", result.reason)
168
 
169
+ # Return the path to the audio file
170
+ return filename
 
171
 
 
172
 
173
 
174
 
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
  gradio
2
  gtts
3
  openai
4
- pyttsx3
 
1
  gradio
2
  gtts
3
  openai
4
+ azure