youngtsai commited on
Commit
d4dc5f0
·
1 Parent(s): 635f86b

delete audio

Browse files
Files changed (1) hide show
  1. app.py +3 -70
app.py CHANGED
@@ -97,7 +97,7 @@ def generate_dialogue(rounds, method, role1, role2, theme, language):
97
 
98
  def main_function(password: str, theme: str, language: str, method: str, rounds: int, role1: str, role1_gender: str, role2: str, role2_gender: str):
99
  if password != os.environ.get("PASSWORD", ""):
100
- return "错误的密码,请重新输入。", "", ""
101
 
102
  structured_dialogue = generate_dialogue(rounds, method, role1, role2, theme, language)
103
 
@@ -110,7 +110,7 @@ def main_function(password: str, theme: str, language: str, method: str, rounds:
110
 
111
  chatbot_dialogue.append((role1_content, role2_content))
112
 
113
- audio_path = dialogue_to_audio(structured_dialogue, role1_gender, role2_gender)
114
  json_output = json.dumps({"dialogue": structured_dialogue}, ensure_ascii=False, indent=4)
115
 
116
  # 儲存對話為 JSON 文件
@@ -118,73 +118,7 @@ def main_function(password: str, theme: str, language: str, method: str, rounds:
118
  with open(file_name, "w", encoding="utf-8") as f:
119
  f.write(json_output)
120
 
121
- return chatbot_dialogue, audio_path, file_name
122
-
123
-
124
- def detect_language(text):
125
- """
126
- Simple function to detect if text is more likely English or Chinese.
127
- """
128
- for char in text:
129
- if '\u4e00' <= char <= '\u9fff': # range for Chinese characters
130
- return "zh"
131
- return "en"
132
-
133
- def dialogue_to_audio(dialogue, role1_gender, role2_gender):
134
- """
135
- Converts the given dialogue into an audio file using Azure's Text-to-Speech service.
136
-
137
- Parameters:
138
- - dialogue (list): List of dictionaries containing the structured dialogue.
139
- - role1_gender (str): Gender of role1. Can be "male" or "female".
140
- - role2_gender (str): Gender of role2. Can be "male" or "female".
141
-
142
- Returns:
143
- - str: File path to the generated audio file.
144
- """
145
- # Set up Azure Speech SDK
146
- speech_config = SpeechConfig(subscription=AZURE_API_KEY, region=AZURE_REGION)
147
-
148
- voices = {
149
- "en": {
150
- "male": "en-US-GuyNeural",
151
- "female": "en-US-JessaNeural"
152
- },
153
- "zh": {
154
- "male": "zh-TW-YunNeural", # Taiwanese male neural voice
155
- "female": "zh-TW-HsiaoYuNeural" # Taiwanese female neural voice
156
- }
157
- }
158
-
159
- ssml = '<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xml:lang="zh-CN">'
160
- for item in dialogue:
161
- role = item['role']
162
- content = item['content']
163
- language = detect_language(content)
164
- gender = role1_gender if role == 'role1' else role2_gender
165
- voice = voices[language][gender]
166
- ssml += f'<voice name="{voice}">{content}</voice>'
167
- ssml += '</speak>'
168
-
169
- # Create an audio configuration that points to an audio file.
170
- audio_file = "output_audio.wav"
171
- audio_output = AudioConfig(filename=audio_file)
172
-
173
- # Create a speech synthesizer using the given settings
174
- synthesizer = SpeechSynthesizer(speech_config=speech_config, audio_config=audio_output)
175
-
176
- # Synthesize the text
177
- result = synthesizer.speak_ssml(ssml)
178
-
179
- # Check result
180
- if result.reason == result.Reason.SynthesizingAudioCompleted:
181
- print(f"Speech synthesized to [{result.audio_file}] for text [{ssml}]")
182
- else:
183
- print(f"Could not synthesize the text, reason: {result.reason}")
184
-
185
- return audio_file
186
-
187
-
188
 
189
 
190
  if __name__ == "__main__":
@@ -203,7 +137,6 @@ if __name__ == "__main__":
203
  ],
204
  [
205
  gr.components.Chatbot(label="生成的對話"),
206
- gr.components.Audio(type="filepath", label="對話朗讀"),
207
  gr.components.File(label="下載對話 JSON 文件")
208
  ]
209
  ).launch()
 
97
 
98
  def main_function(password: str, theme: str, language: str, method: str, rounds: int, role1: str, role1_gender: str, role2: str, role2_gender: str):
99
  if password != os.environ.get("PASSWORD", ""):
100
+ return "错误的密码,请重新输入。", ""
101
 
102
  structured_dialogue = generate_dialogue(rounds, method, role1, role2, theme, language)
103
 
 
110
 
111
  chatbot_dialogue.append((role1_content, role2_content))
112
 
113
+ # audio_path = dialogue_to_audio(structured_dialogue, role1_gender, role2_gender)
114
  json_output = json.dumps({"dialogue": structured_dialogue}, ensure_ascii=False, indent=4)
115
 
116
  # 儲存對話為 JSON 文件
 
118
  with open(file_name, "w", encoding="utf-8") as f:
119
  f.write(json_output)
120
 
121
+ return chatbot_dialogue, file_name
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
 
123
 
124
  if __name__ == "__main__":
 
137
  ],
138
  [
139
  gr.components.Chatbot(label="生成的對話"),
 
140
  gr.components.File(label="下載對話 JSON 文件")
141
  ]
142
  ).launch()