| import gradio as gr |
| import pandas as pd |
| import zipfile |
| import base64 |
| import os |
| import requests |
| import re |
|
|
| def remove_quotes(text): |
| |
| return text.replace('"', '') |
|
|
| def text_to_speech(input_file,selected_option): |
| |
| api_key = 'AIzaSyAEzK5_n6zKTimD9yoXS-C8O0xN_4LaVBQ' |
| data = pd.read_csv(input_file) |
| zip_path = 'output_audio_files.zip' |
|
|
| with zipfile.ZipFile(zip_path, 'w') as z: |
| for idx, row in data.iterrows(): |
| |
| script = row.get('script', '') |
| if not isinstance(script, str): |
| script = str(script) |
| |
| |
| parts = re.split(r'(A:|B:)', script) |
| print(parts) |
| ssml_parts = [] |
| print(parts) |
| |
| for i in range(1, len(parts), 2): |
| if parts[i] == "A:": |
| voice_name = row["voiceA"] |
| print("A") |
| elif parts[i] == "B:": |
| voice_name = row["voiceB"] |
| else: |
| print("空白") |
| continue |
| |
| text = parts[i + 1].strip() |
| text = remove_quotes(text) |
| print("テキスト",text) |
|
|
|
|
| |
| text = text.replace("a.m.", 'AM') |
| text = text.replace("p.m.", 'PM') |
| text = text.replace("U.S.", 'US') |
| text = text.replace("U.K.", 'UK') |
| text = text.replace("Mr.", 'Mister') |
| text = text.replace("Ms.", 'MIZ') |
| text = text.replace("Mrs.", 'Misiz') |
| text = text.replace("Dr.", 'Doctor') |
| text = text.replace("Mt.", 'Mount') |
|
|
|
|
| |
| text = text.replace("\n", '<break time="1s"/>') |
| text = text.replace(".", '.<break time="500ms"/>') |
|
|
| if selected_option == "ブレイクタイム有": |
| |
| if row["eikenn"] in ["5級","4級","3級","準2級","2級","準1級"]: |
| text = text.replace(",", '<break time="50ms"/>') |
| print("タグ処理") |
| else: |
| pass |
| |
| ssml_parts.append(f'<voice name="{voice_name}"><prosody rate="{row["speed"]}"><p>{text}</p></prosody></voice>') |
| print(ssml_parts) |
| ssml = '<speak>' + ''.join(ssml_parts) |
| print(ssml) |
| |
| if pd.notna(row.get('question')) and row['question'] != '': |
| ssml += f'<break time="1s"/><voice name="{row["voiceQuestion"]}"><prosody rate="{row["speed"]}"><p>Question</p><break time="1s"/><p>{row["question"]}</p></prosody></voice>' |
| |
| if pd.notna(row.get('choices')) and row['choices'] != '': |
| choices_list = row['choices'].split('/') |
| choices_ssml = '<break time="1s"/>'.join(choices_list) |
| ssml += f'<break time="1s"/><voice name="{row["voiceB"]}"><prosody rate="{row["speed"]}"><p>{choices_ssml}</p></prosody></voice>' |
| ssml += '</speak>' |
|
|
| print(ssml) |
| |
| body = { |
| "input": {"ssml": ssml}, |
| "voice": {"languageCode": "en-US"}, |
| "audioConfig": {"audioEncoding": "MP3"} |
| } |
| headers = { |
| "X-Goog-Api-Key": api_key, |
| "Content-Type": "application/json" |
| } |
| url = "https://texttospeech.googleapis.com/v1/text:synthesize" |
| response = requests.post(url, headers=headers, json=body) |
| print("レスポンス",response) |
| response_data = response.json() |
| print("レスポンスデータ",response_data) |
|
|
| |
| if 'audioContent' in response_data: |
| audio_content = base64.b64decode(response_data['audioContent']) |
| file_name = f"{row['id']}.mp3" |
| with open(file_name, "wb") as out: |
| out.write(audio_content) |
| |
| z.write(file_name) |
| os.remove(file_name) |
| else: |
| print("ファイル不備") |
|
|
| return zip_path |
|
|