Spaces:
Configuration error
Configuration error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -97,8 +97,19 @@ Follow this JSON example structure, MUST be in {language} language:
|
|
| 97 |
temperature=1
|
| 98 |
)
|
| 99 |
try:
|
| 100 |
-
podcast_match = re.search(r'{
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
except Exception as e:
|
| 103 |
if "API key not valid" in str(e):
|
| 104 |
raise gr.Error("Invalid API key. Please provide a valid API key.")
|
|
@@ -106,7 +117,6 @@ Follow this JSON example structure, MUST be in {language} language:
|
|
| 106 |
raise gr.Error("Rate limit exceeded for the API key. Please try again later or provide your own API key.")
|
| 107 |
else:
|
| 108 |
raise gr.Error(f"Failed to generate podcast script: {e}")
|
| 109 |
-
return podcast_match.group(0)
|
| 110 |
|
| 111 |
async def tts_generate(input_text, speaker1, speaker2):
|
| 112 |
speaker1_name = speaker1.split(' - ')[0]
|
|
@@ -114,39 +124,46 @@ async def tts_generate(input_text, speaker1, speaker2):
|
|
| 114 |
speaker1_voice = speaker1.split(' - ')[1]
|
| 115 |
speaker2_voice = speaker2.split(' - ')[1]
|
| 116 |
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
podcast_json = {
|
| 119 |
-
"topic": podcast_dict
|
| 120 |
"podcast": []
|
| 121 |
}
|
| 122 |
speaker_map = {
|
| 123 |
-
"speaker1"
|
| 124 |
-
"speaker2"
|
| 125 |
}
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
| 132 |
|
| 133 |
communicate = edge_tts.Communicate(text, voice)
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
podcast_json["podcast"].append({
|
| 137 |
"speaker": speaker_map.get(speaker, speaker),
|
| 138 |
"line": text
|
| 139 |
})
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
voice_name = speaker1_name if speaker == 1 else speaker2_name
|
| 145 |
-
audio = AudioSegment.from_mp3(f"{voice_name}.mp3")
|
| 146 |
-
combined += audio
|
| 147 |
-
|
| 148 |
-
combined.export("combined.mp3", format="mp3")
|
| 149 |
-
return "combined.mp3"
|
| 150 |
|
| 151 |
async def process_podcast(input_text, language, speaker1, speaker2, api_key):
|
| 152 |
podcast_script = generate_response(input_text, language, speaker1, speaker2, api_key)
|
|
|
|
| 97 |
temperature=1
|
| 98 |
)
|
| 99 |
try:
|
| 100 |
+
podcast_match = re.search(r'{.*}', response.choices[0].message.content, re.DOTALL)
|
| 101 |
+
if podcast_match:
|
| 102 |
+
podcast_json = podcast_match.group(0)
|
| 103 |
+
# 嘗試解析 JSON,如果失敗則進行清理
|
| 104 |
+
try:
|
| 105 |
+
json.loads(podcast_json)
|
| 106 |
+
except json.JSONDecodeError:
|
| 107 |
+
# 清理 JSON 字符串
|
| 108 |
+
podcast_json = re.sub(r',\s*}', '}', podcast_json) # 移除最後一個逗號
|
| 109 |
+
podcast_json = re.sub(r',\s*]', ']', podcast_json) # 移除數組最後一個逗號
|
| 110 |
+
return podcast_json
|
| 111 |
+
else:
|
| 112 |
+
raise gr.Error("Failed to generate podcast script. Please try again.")
|
| 113 |
except Exception as e:
|
| 114 |
if "API key not valid" in str(e):
|
| 115 |
raise gr.Error("Invalid API key. Please provide a valid API key.")
|
|
|
|
| 117 |
raise gr.Error("Rate limit exceeded for the API key. Please try again later or provide your own API key.")
|
| 118 |
else:
|
| 119 |
raise gr.Error(f"Failed to generate podcast script: {e}")
|
|
|
|
| 120 |
|
| 121 |
async def tts_generate(input_text, speaker1, speaker2):
|
| 122 |
speaker1_name = speaker1.split(' - ')[0]
|
|
|
|
| 124 |
speaker1_voice = speaker1.split(' - ')[1]
|
| 125 |
speaker2_voice = speaker2.split(' - ')[1]
|
| 126 |
|
| 127 |
+
try:
|
| 128 |
+
podcast_dict = json.loads(input_text)
|
| 129 |
+
except json.JSONDecodeError:
|
| 130 |
+
# 如果 JSON 解析失敗,嘗試清理輸入
|
| 131 |
+
cleaned_input = re.sub(r',\s*}', '}', input_text)
|
| 132 |
+
cleaned_input = re.sub(r',\s*]', ']', cleaned_input)
|
| 133 |
+
podcast_dict = json.loads(cleaned_input)
|
| 134 |
+
|
| 135 |
podcast_json = {
|
| 136 |
+
"topic": podcast_dict.get("topic", "Unknown Topic"),
|
| 137 |
"podcast": []
|
| 138 |
}
|
| 139 |
speaker_map = {
|
| 140 |
+
1: "speaker1",
|
| 141 |
+
2: "speaker2"
|
| 142 |
}
|
| 143 |
|
| 144 |
+
combined = AudioSegment.empty()
|
| 145 |
+
for line in podcast_dict.get("podcast", []):
|
| 146 |
+
speaker = line.get("speaker")
|
| 147 |
+
text = line.get("line", "")
|
| 148 |
+
voice = speaker1_voice if speaker == 1 else speaker2_voice
|
| 149 |
+
voice_name = speaker1_name if speaker == 1 else speaker2_name
|
| 150 |
|
| 151 |
communicate = edge_tts.Communicate(text, voice)
|
| 152 |
+
audio_file = f"{voice_name}_{uuid.uuid4()}.mp3"
|
| 153 |
+
await communicate.save(audio_file)
|
| 154 |
+
|
| 155 |
+
audio = AudioSegment.from_mp3(audio_file)
|
| 156 |
+
combined += audio
|
| 157 |
+
os.remove(audio_file) # 刪除臨時文件
|
| 158 |
|
| 159 |
podcast_json["podcast"].append({
|
| 160 |
"speaker": speaker_map.get(speaker, speaker),
|
| 161 |
"line": text
|
| 162 |
})
|
| 163 |
|
| 164 |
+
output_file = f"combined_{uuid.uuid4()}.mp3"
|
| 165 |
+
combined.export(output_file, format="mp3")
|
| 166 |
+
return output_file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
async def process_podcast(input_text, language, speaker1, speaker2, api_key):
|
| 169 |
podcast_script = generate_response(input_text, language, speaker1, speaker2, api_key)
|