|
@@ -5,6 +5,7 @@ from dotenv import load_dotenv
|
|
|
import tiktoken
|
|
|
from pypinyin import pinyin, Style
|
|
|
import jieba
|
|
|
+from datetime import datetime
|
|
|
|
|
|
load_dotenv('environment.env')
|
|
|
client = OpenAI()
|
|
@@ -31,6 +32,23 @@ def transcribe(audio_file):
|
|
|
except Exception as e:
|
|
|
print(f"轉錄時發生錯誤:{str(e)}")
|
|
|
return None
|
|
|
+
|
|
|
+def save_output(file_name, raw_transcript, corrected_transcript):
|
|
|
+ output_dir = "output"
|
|
|
+ if not os.path.exists(output_dir):
|
|
|
+ os.makedirs(output_dir)
|
|
|
+
|
|
|
+ output_file = os.path.join(output_dir, "transcription_results.txt")
|
|
|
+
|
|
|
+ with open(output_file, "a", encoding="utf-8") as f:
|
|
|
+ f.write(f"\n{'='*50}\n")
|
|
|
+ f.write(f"文件名: {file_name}\n")
|
|
|
+ f.write(f"處理時間: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n")
|
|
|
+ f.write("原始轉錄:\n")
|
|
|
+ f.write(f"{raw_transcript}\n\n")
|
|
|
+ f.write("修正後的轉錄:\n")
|
|
|
+ f.write(f"{corrected_transcript}\n")
|
|
|
+
|
|
|
|
|
|
def process_audio_file(file_path):
|
|
|
try:
|
|
@@ -51,9 +69,13 @@ def process_audio_file(file_path):
|
|
|
print("\n修正後的轉錄:")
|
|
|
print(corrected_transcript)
|
|
|
|
|
|
+ # 保存輸出結果
|
|
|
+ save_output(os.path.basename(file_path), raw_transcript, corrected_transcript)
|
|
|
+
|
|
|
except Exception as e:
|
|
|
print(f"處理文件 {os.path.basename(file_path)} 時發生錯誤:{str(e)}")
|
|
|
|
|
|
+
|
|
|
def process_folder(folder_path):
|
|
|
processed_files = 0
|
|
|
|
|
@@ -106,6 +128,7 @@ error_correction = {
|
|
|
"看": "碳"
|
|
|
}
|
|
|
|
|
|
+
|
|
|
def fuzzy_correct_chinese(text, correct_terms):
|
|
|
words = jieba.cut(text)
|
|
|
corrected_words = []
|
|
@@ -121,6 +144,7 @@ def fuzzy_correct_chinese(text, correct_terms):
|
|
|
corrected_words.append(word)
|
|
|
return ' '.join(corrected_words)
|
|
|
|
|
|
+
|
|
|
def post_process_transcript(transcript, temperature=0):
|
|
|
correct_terms = ["碳", "溫室氣體", "碳排放", "排放", "碳管理", "管理", "碳盤查", "盤查", "碳權交易", "碳費",
|
|
|
"碳權", "碳足跡", "足跡", "淨零排放", "零排放", "排放", "淨零",
|
|
@@ -143,6 +167,7 @@ def post_process_transcript(transcript, temperature=0):
|
|
|
|
|
|
return response.choices[0].message.content
|
|
|
|
|
|
+
|
|
|
def main():
|
|
|
parser = argparse.ArgumentParser(description="處理音頻文件使用 Whisper")
|
|
|
parser.add_argument("--file", help="要處理的單個音頻文件的路徑")
|