From 886c4517f6d2ecb3e43f5f2d2c999247e13f9ead Mon Sep 17 00:00:00 2001 From: Arcelibs Date: Thu, 28 Dec 2023 15:30:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3:=E5=8A=A0=E5=85=A5=E5=B0=8D?= =?UTF-8?q?=E7=A9=BA=E9=9F=B3=E8=A8=8A=E7=9A=84=E8=A8=8A=E6=81=AF=E6=8F=90?= =?UTF-8?q?=E7=A4=BA=20=E6=96=B0=E5=A2=9E:=E5=8A=A0=E5=85=A5=E6=8A=93?= =?UTF-8?q?=E5=8F=96KeyError:=20'candidates'=20not=20found=20in=20response?= =?UTF-8?q?.=E7=9A=84=E5=8F=AF=E8=83=BD=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Local-Windows/main.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Local-Windows/main.py b/Local-Windows/main.py index 9fa1dac..efdb86f 100644 --- a/Local-Windows/main.py +++ b/Local-Windows/main.py @@ -31,7 +31,7 @@ def get_deepl_api_key_from_file(file_path='deepl_api_key.txt'): print("未找到 DeepL API 密鑰文件。") return None -# 呼叫DeepL API的函數(備案,當Gemini不能翻譯色色內容時) +# 呼叫DeepL API的函數 def call_deepl_api(input_text, deepL_auth_key, target_lang='ZH'): deepL_url = 'https://api-free.deepl.com/v2/translate' headers = { @@ -94,6 +94,8 @@ def call_gemini_api(input_text): return response_data['candidates'][0]['content']['parts'][0]['text'] else: print("KeyError: 'candidates' not found in response.") + # 保存完整回應 + save_debug_data("gemini_response", response_data) # 可以选择直接使用 DeepL API 的翻译结果 return simplified_chinese_text else: @@ -139,6 +141,16 @@ def transcribe_audio(file_path, language="japanese"): print(f"錯誤: 無法轉錄聲音。詳情:{e}") return None +# 新增的保存KeyError: 'candidates' not found in response 的完整回應 +def save_debug_data(filename, data): + debug_dir = 'debug' + if not os.path.exists(debug_dir): + os.makedirs(debug_dir) + + path = os.path.join(debug_dir, f'{filename}_save_debug_data.txt') + with open(path, 'w', encoding='utf-8') as file: + json.dump(data, file, indent=4, ensure_ascii=False) + # 新增的保存轉錄文的函數 def save_transcription(file_path, text, is_api_response=False): raw_data_dir = 'RawData' if not is_api_response else 'APIResponses' @@ -238,7 +250,7 @@ def main(segment_duration, total_duration): else: print(f"無法翻譯 {segment}。") else: - print(f"無法轉錄片段 {segment}。") + print(f"此片段無音訊故無法轉錄: 第{segment}秒。") os.remove(filename) # 清理:聲音檔案 else: print(f"錄製段落 {segment} 失敗。")