Update voice_create.py
Browse files- voice_create.py +64 -0
voice_create.py
CHANGED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import zipfile
|
| 4 |
+
import base64
|
| 5 |
+
import os
|
| 6 |
+
import requests
|
| 7 |
+
|
| 8 |
+
def text_to_speech(input_file):
|
| 9 |
+
# APIキーを直接コードに埋め込む(実際の運用では推奨されません)
|
| 10 |
+
api_key = 'AIzaSyAEzK5_n6zKTimD9yoXS-C8O0xN_4LaVBQ' # ここを実際のAPIキーに置き換えてください
|
| 11 |
+
data = pd.read_csv(input_file)
|
| 12 |
+
zip_path = 'output_audio_files.zip'
|
| 13 |
+
|
| 14 |
+
with zipfile.ZipFile(zip_path, 'w') as z:
|
| 15 |
+
for idx, row in data.iterrows():
|
| 16 |
+
parts = row['script'].split("\n")
|
| 17 |
+
ssml_parts = []
|
| 18 |
+
# 交互に発言するAとBの内容を順に処理
|
| 19 |
+
for i, part in enumerate(parts):
|
| 20 |
+
if part.startswith("A:"):
|
| 21 |
+
voice_name = row["voiceA"]
|
| 22 |
+
elif part.startswith("B:"):
|
| 23 |
+
voice_name = row["voiceB"]
|
| 24 |
+
else:
|
| 25 |
+
continue # A:またはB:で始まらない行は無視
|
| 26 |
+
text = part.split(":", 1)[1].strip()
|
| 27 |
+
ssml_parts.append(f'<voice name="{voice_name}"><prosody speed="{row["speed"]}"><p>{text}</p></prosody></voice>')
|
| 28 |
+
print("読み上げ速度", row["speed"])
|
| 29 |
+
|
| 30 |
+
ssml = '<speak>' + ''.join(ssml_parts)
|
| 31 |
+
if pd.notna(row.get('question')) and row['question'] != '':
|
| 32 |
+
ssml += f'<break time="1s"/><voice name="{row["voiceQuestion"]}"><p>Question</p><break time="1s"/><p>{row["question"]}</p></voice>'
|
| 33 |
+
# Choices部分の追加
|
| 34 |
+
if pd.notna(row.get('choices')) and row['choices'] != '':
|
| 35 |
+
choices_list = row['choices'].split('/')
|
| 36 |
+
choices_ssml = '<break time="1s"/>'.join(choices_list)
|
| 37 |
+
ssml += f'<break time="1s"/><voice name="{row["voiceB"]}"><p>{choices_ssml}</p></voice>'
|
| 38 |
+
ssml += '</speak>'
|
| 39 |
+
|
| 40 |
+
# APIリクエスト用のボディ
|
| 41 |
+
body = {
|
| 42 |
+
"input": {"ssml": ssml},
|
| 43 |
+
"voice": {"languageCode": "en-US"}, # 基本的な言語設定(必要に応じて行ごとに変更可能)
|
| 44 |
+
"audioConfig": {"audioEncoding": "MP3"}
|
| 45 |
+
}
|
| 46 |
+
headers = {
|
| 47 |
+
"X-Goog-Api-Key": api_key,
|
| 48 |
+
"Content-Type": "application/json"
|
| 49 |
+
}
|
| 50 |
+
url = "https://texttospeech.googleapis.com/v1/text:synthesize"
|
| 51 |
+
response = requests.post(url, headers=headers, json=body)
|
| 52 |
+
response_data = response.json()
|
| 53 |
+
|
| 54 |
+
# 音声コンテンツの取得とファイル保存
|
| 55 |
+
if 'audioContent' in response_data:
|
| 56 |
+
audio_content = base64.b64decode(response_data['audioContent'])
|
| 57 |
+
file_name = f"{row['id']}.mp3"
|
| 58 |
+
with open(file_name, "wb") as out:
|
| 59 |
+
out.write(audio_content)
|
| 60 |
+
|
| 61 |
+
z.write(file_name)
|
| 62 |
+
os.remove(file_name)
|
| 63 |
+
|
| 64 |
+
return zip_path
|