|
@@ -2,13 +2,13 @@ import os
|
|
import argparse
|
|
import argparse
|
|
from openai import OpenAI
|
|
from openai import OpenAI
|
|
from dotenv import load_dotenv
|
|
from dotenv import load_dotenv
|
|
-import tiktoken
|
|
|
|
from pypinyin import pinyin, Style
|
|
from pypinyin import pinyin, Style
|
|
import jieba
|
|
import jieba
|
|
from datetime import datetime
|
|
from datetime import datetime
|
|
|
|
+import logging
|
|
|
|
|
|
load_dotenv('environment.env')
|
|
load_dotenv('environment.env')
|
|
-client = OpenAI()
|
|
|
|
|
|
+client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
|
|
|
|
|
|
system_prompt = """你是一位專業的轉錄校對助理,專門處理有關溫室氣體、碳排放和碳管理的對話轉錄。
|
|
system_prompt = """你是一位專業的轉錄校對助理,專門處理有關溫室氣體、碳排放和碳管理的對話轉錄。
|
|
你的任務是:
|
|
你的任務是:
|
|
@@ -21,16 +21,26 @@ system_prompt = """你是一位專業的轉錄校對助理,專門處理有關
|
|
|
|
|
|
請只根據提供的原文進行必要的更正,不要添加或刪除任何實質性內容。在修正時,請特別注意上下文,確保修正後的詞語符合整句話的語境。"""
|
|
請只根據提供的原文進行必要的更正,不要添加或刪除任何實質性內容。在修正時,請特別注意上下文,確保修正後的詞語符合整句話的語境。"""
|
|
|
|
|
|
|
|
+def setup_logger():
|
|
|
|
+ logging.basicConfig(level=logging.DEBUG,
|
|
|
|
+ format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
|
|
+ return logging.getLogger(__name__)
|
|
|
|
+
|
|
|
|
+logger = setup_logger()
|
|
|
|
+
|
|
def transcribe(audio_file):
|
|
def transcribe(audio_file):
|
|
try:
|
|
try:
|
|
- transcript = client.audio.transcriptions.create(
|
|
|
|
- file=audio_file,
|
|
|
|
- model="whisper-1",
|
|
|
|
- response_format="text"
|
|
|
|
- )
|
|
|
|
- return transcript
|
|
|
|
|
|
+ logger.info(f"Attempting to transcribe file: {audio_file}")
|
|
|
|
+ with open(audio_file, "rb") as file:
|
|
|
|
+ response = client.audio.transcriptions.create(
|
|
|
|
+ file=file,
|
|
|
|
+ model="whisper-1",
|
|
|
|
+ response_format="text"
|
|
|
|
+ )
|
|
|
|
+ logger.info("Transcription successful")
|
|
|
|
+ return response
|
|
except Exception as e:
|
|
except Exception as e:
|
|
- print(f"轉錄時發生錯誤:{str(e)}")
|
|
|
|
|
|
+ logger.error(f"Transcription failed: {str(e)}", exc_info=True)
|
|
return None
|
|
return None
|
|
|
|
|
|
def save_output(file_name, raw_transcript, corrected_transcript):
|
|
def save_output(file_name, raw_transcript, corrected_transcript):
|
|
@@ -74,6 +84,7 @@ def process_audio_file(file_path):
|
|
|
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
print(f"處理文件 {os.path.basename(file_path)} 時發生錯誤:{str(e)}")
|
|
print(f"處理文件 {os.path.basename(file_path)} 時發生錯誤:{str(e)}")
|
|
|
|
+ return transcript
|
|
|
|
|
|
|
|
|
|
def process_folder(folder_path):
|
|
def process_folder(folder_path):
|
|
@@ -165,27 +176,43 @@ def post_process_transcript(transcript, temperature=0):
|
|
messages=messages
|
|
messages=messages
|
|
)
|
|
)
|
|
|
|
|
|
- return response.choices[0].message.content
|
|
|
|
|
|
+ # return response.choices[0].message.content
|
|
|
|
+ return transcript
|
|
|
|
|
|
-
|
|
|
|
-def main():
|
|
|
|
|
|
+# 處理單個音頻,使用transcript端點發送音頻給API
|
|
|
|
+if __name__ == "__main__":
|
|
|
|
+ import argparse
|
|
parser = argparse.ArgumentParser(description="處理音頻文件使用 Whisper")
|
|
parser = argparse.ArgumentParser(description="處理音頻文件使用 Whisper")
|
|
parser.add_argument("--file", help="要處理的單個音頻文件的路徑")
|
|
parser.add_argument("--file", help="要處理的單個音頻文件的路徑")
|
|
- parser.add_argument("--folder", default="data", help="包含音頻文件的文件夾路徑(默認:data)")
|
|
|
|
args = parser.parse_args()
|
|
args = parser.parse_args()
|
|
|
|
|
|
if args.file:
|
|
if args.file:
|
|
- if os.path.isfile(args.file):
|
|
|
|
- process_audio_file(args.file)
|
|
|
|
- else:
|
|
|
|
- print(f"錯誤:文件 '{args.file}' 不存在。")
|
|
|
|
- elif args.folder:
|
|
|
|
- if os.path.isdir(args.folder):
|
|
|
|
- process_folder(args.folder)
|
|
|
|
- else:
|
|
|
|
- print(f"錯誤:文件夾 '{args.folder}' 不存在。")
|
|
|
|
- else:
|
|
|
|
- print("錯誤:請指定一個文件(--file)或文件夾(--folder)來處理。")
|
|
|
|
-
|
|
|
|
-if __name__ == "__main__":
|
|
|
|
- main()
|
|
|
|
|
|
+ with open(args.file, "rb") as audio_file:
|
|
|
|
+ transcript = transcribe(audio_file)
|
|
|
|
+ if transcript:
|
|
|
|
+ corrected = post_process_transcript(transcript)
|
|
|
|
+ print("Original:", transcript)
|
|
|
|
+ print("Corrected:", corrected)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+# def main():
|
|
|
|
+# parser = argparse.ArgumentParser(description="處理音頻文件使用 Whisper")
|
|
|
|
+# parser.add_argument("--file", help="要處理的單個音頻文件的路徑")
|
|
|
|
+# parser.add_argument("--folder", default="data", help="包含音頻文件的文件夾路徑(默認:data)")
|
|
|
|
+# args = parser.parse_args()
|
|
|
|
+
|
|
|
|
+# if args.file:
|
|
|
|
+# if os.path.isfile(args.file):
|
|
|
|
+# process_audio_file(args.file)
|
|
|
|
+# else:
|
|
|
|
+# print(f"錯誤:文件 '{args.file}' 不存在。")
|
|
|
|
+# elif args.folder:
|
|
|
|
+# if os.path.isdir(args.folder):
|
|
|
|
+# process_folder(args.folder)
|
|
|
|
+# else:
|
|
|
|
+# print(f"錯誤:文件夾 '{args.folder}' 不存在。")
|
|
|
|
+# else:
|
|
|
|
+# print("錯誤:請指定一個文件(--file)或文件夾(--folder)來處理。")
|
|
|
|
+
|
|
|
|
+# if __name__ == "__main__":
|
|
|
|
+# main()
|