Update voice_create.py
Browse files- voice_create.py +12 -6
voice_create.py
CHANGED
|
@@ -4,6 +4,7 @@ import zipfile
|
|
| 4 |
import base64
|
| 5 |
import os
|
| 6 |
import requests
|
|
|
|
| 7 |
|
| 8 |
def text_to_speech(input_file):
|
| 9 |
# APIキーを直接コードに埋め込む(実際の運用では推奨されません)
|
|
@@ -13,21 +14,26 @@ def text_to_speech(input_file):
|
|
| 13 |
|
| 14 |
with zipfile.ZipFile(zip_path, 'w') as z:
|
| 15 |
for idx, row in data.iterrows():
|
| 16 |
-
|
|
|
|
| 17 |
ssml_parts = []
|
|
|
|
| 18 |
# 交互に発言するAとBの内容を順に処理
|
| 19 |
-
for i
|
| 20 |
-
if
|
| 21 |
voice_name = row["voiceA"]
|
| 22 |
-
elif
|
| 23 |
voice_name = row["voiceB"]
|
| 24 |
else:
|
| 25 |
continue # A:またはB:で始まらない行は無視
|
| 26 |
-
text =
|
|
|
|
|
|
|
|
|
|
| 27 |
ssml_parts.append(f'<voice name="{voice_name}"><prosody rate="{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"]}"><prosody rate="{row["speed"]}"><p>Question</p><break time="1s"/><p>{row["question"]}</p></prosody></voice>'
|
| 33 |
# Choices部分の追加
|
|
|
|
| 4 |
import base64
|
| 5 |
import os
|
| 6 |
import requests
|
| 7 |
+
import re
|
| 8 |
|
| 9 |
def text_to_speech(input_file):
|
| 10 |
# APIキーを直接コードに埋め込む(実際の運用では推奨されません)
|
|
|
|
| 14 |
|
| 15 |
with zipfile.ZipFile(zip_path, 'w') as z:
|
| 16 |
for idx, row in data.iterrows():
|
| 17 |
+
# テキストをA:やB:で分割
|
| 18 |
+
parts = re.split(r'(A:|B:)', row['script'])
|
| 19 |
ssml_parts = []
|
| 20 |
+
|
| 21 |
# 交互に発言するAとBの内容を順に処理
|
| 22 |
+
for i in range(1, len(parts), 2):
|
| 23 |
+
if parts[i] == "A:":
|
| 24 |
voice_name = row["voiceA"]
|
| 25 |
+
elif parts[i] == "B:":
|
| 26 |
voice_name = row["voiceB"]
|
| 27 |
else:
|
| 28 |
continue # A:またはB:で始まらない行は無視
|
| 29 |
+
text = parts[i + 1].strip()
|
| 30 |
+
|
| 31 |
+
# テキスト内の改行を1sの間に変換
|
| 32 |
+
text = text.replace("\n", '<break time="1s"/>')
|
| 33 |
ssml_parts.append(f'<voice name="{voice_name}"><prosody rate="{row["speed"]}"><p>{text}</p></prosody></voice>')
|
|
|
|
| 34 |
|
| 35 |
ssml = '<speak>' + ''.join(ssml_parts)
|
| 36 |
+
|
| 37 |
if pd.notna(row.get('question')) and row['question'] != '':
|
| 38 |
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>'
|
| 39 |
# Choices部分の追加
|