Skip to content

Commit

Permalink
修正:加入對空音訊的訊息提示
Browse files Browse the repository at this point in the history
新增:加入抓取KeyError: 'candidates' not found in response.的可能性
  • Loading branch information
Arcelibs committed Dec 28, 2023
1 parent 4dde02f commit 886c451
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Local-Windows/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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} 失敗。")
Expand Down

0 comments on commit 886c451

Please sign in to comment.