File size: 4,983 Bytes
3738fa1 27639ff dc1dccb 27639ff a74ff25 2cb3de4 a77fb3e 27639ff 2cb3de4 dc1dccb 2cb3de4 9be2e21 27639ff ced474d 27639ff dc1dccb 27639ff ced474d dc1dccb 27639ff ced474d 27639ff ced474d dc1dccb a74ff25 ced474d 00dbe7c f169d3a 483a2a9 bd5ea9b 04ff3d5 f169d3a d4a261b 483a2a9 dc1dccb 367a6ee 00dbe7c a77fb3e 03bf79a f80690e 4b896bb 27639ff 4b896bb dc1dccb 27639ff 72933f0 27639ff 72933f0 27639ff 0380c08 27639ff 0380c08 27639ff 0380c08 27639ff 2270957 27639ff | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | 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キーを直接コードに埋め込む(実際の運用では推奨されません)
api_key = 'AIzaSyAEzK5_n6zKTimD9yoXS-C8O0xN_4LaVBQ' # ここを実際のAPIキーに置き換えてください
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列が文字列か確認し、文字列でない場合は空文字に置き換える
script = row.get('script', '')
if not isinstance(script, str):
script = str(script)
# テキストをA:やB:で分割
parts = re.split(r'(A:|B:)', script)
print(parts)
ssml_parts = []
print(parts)
# 交互に発言するAとBの内容を順に処理
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 # A:またはB:で始まらない行は無視
text = parts[i + 1].strip()
text = remove_quotes(text)
print("テキスト",text)
# 1sに変換する前に除外するコード
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')
# テキスト内の改行を1sの間に変換
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>'
# Choices部分の追加
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)
# APIリクエスト用のボディ
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
|