|  | @@ -0,0 +1,89 @@
 | 
	
		
			
				|  |  | +# 供資策會課程使用
 | 
	
		
			
				|  |  | +import os
 | 
	
		
			
				|  |  | +import argparse
 | 
	
		
			
				|  |  | +from openai import OpenAI
 | 
	
		
			
				|  |  | +from dotenv import load_dotenv
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +load_dotenv('environment.env')
 | 
	
		
			
				|  |  | +client = OpenAI()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +system_prompt = """你是一位專業的轉錄校對助理,專門處理有關溫室氣體、碳排放和碳管理的對話轉錄。
 | 
	
		
			
				|  |  | +你的任務是:
 | 
	
		
			
				|  |  | +1. 確保以下專業術語的準確性:溫室氣體、碳排放、碳管理、碳盤查、碳權交易、碳足跡、淨零排放、碳權。
 | 
	
		
			
				|  |  | +2. 在必要時添加適當的標點符號,如句號、逗號,並使用正確的大小寫。
 | 
	
		
			
				|  |  | +3. 使用台灣的繁體中文,確保語言表達符合台灣的用語習慣。
 | 
	
		
			
				|  |  | +4. 只更正明顯的錯誤或改善可讀性,不要改變原文的意思或結構。
 | 
	
		
			
				|  |  | +5. 不要回答問題、解釋概念或添加任何不在原文中的信息。
 | 
	
		
			
				|  |  | +6. 如果原文是一個問句,保持它的問句形式,不要提供答案。
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +請只根據提供的原文進行必要的更正,不要添加或刪除任何實質性內容。"""
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +def transcribe(audio_file):
 | 
	
		
			
				|  |  | +    try:
 | 
	
		
			
				|  |  | +        transcript = client.audio.transcriptions.create(
 | 
	
		
			
				|  |  | +            file=audio_file,
 | 
	
		
			
				|  |  | +            model="whisper-1",
 | 
	
		
			
				|  |  | +            response_format="text"
 | 
	
		
			
				|  |  | +        )
 | 
	
		
			
				|  |  | +        return transcript
 | 
	
		
			
				|  |  | +    except Exception as e:
 | 
	
		
			
				|  |  | +        print(f"轉錄時發生錯誤:{str(e)}")
 | 
	
		
			
				|  |  | +        return None
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +def post_process_transcript(transcript):
 | 
	
		
			
				|  |  | +    messages = [
 | 
	
		
			
				|  |  | +        {"role": "system", "content": system_prompt},
 | 
	
		
			
				|  |  | +        {"role": "user", "content": f"請校對並修正以下轉錄文本,但不要改變其原意或回答問題:\n\n{transcript}"}
 | 
	
		
			
				|  |  | +    ]
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    response = client.chat.completions.create(
 | 
	
		
			
				|  |  | +        model="gpt-3.5-turbo",
 | 
	
		
			
				|  |  | +        temperature=0,
 | 
	
		
			
				|  |  | +        messages=messages
 | 
	
		
			
				|  |  | +    )
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    return response.choices[0].message.content
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +def process_audio_file(file_path):
 | 
	
		
			
				|  |  | +    try:
 | 
	
		
			
				|  |  | +        with open(file_path, "rb") as audio_file:
 | 
	
		
			
				|  |  | +            print(f"\n處理文件:{os.path.basename(file_path)}")
 | 
	
		
			
				|  |  | +            
 | 
	
		
			
				|  |  | +            raw_transcript = transcribe(audio_file)
 | 
	
		
			
				|  |  | +            if raw_transcript is None:
 | 
	
		
			
				|  |  | +                return
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            print("\n原始轉錄:")
 | 
	
		
			
				|  |  | +            print(raw_transcript)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            corrected_transcript = post_process_transcript(raw_transcript)
 | 
	
		
			
				|  |  | +            print("\n修正後的轉錄:")
 | 
	
		
			
				|  |  | +            print(corrected_transcript)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    except Exception as e:
 | 
	
		
			
				|  |  | +        print(f"處理文件 {os.path.basename(file_path)} 時發生錯誤:{str(e)}")
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +def process_folder(folder_path):
 | 
	
		
			
				|  |  | +    processed_files = 0
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    for filename in os.listdir(folder_path):
 | 
	
		
			
				|  |  | +        if filename.endswith((".mp3", ".wav", ".m4a")):  
 | 
	
		
			
				|  |  | +            file_path = os.path.join(folder_path, filename)
 | 
	
		
			
				|  |  | +            process_audio_file(file_path)
 | 
	
		
			
				|  |  | +            processed_files += 1
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    print("\n=== 總結 ===")
 | 
	
		
			
				|  |  | +    print(f"處理的文件數:{processed_files}")
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +def main():
 | 
	
		
			
				|  |  | +    parser = argparse.ArgumentParser(description="處理音頻文件使用 Whisper 和 GPT-3.5-turbo")
 | 
	
		
			
				|  |  | +    parser.add_argument("--folder", default="data", help="包含音頻文件的文件夾路徑(默認:data)")
 | 
	
		
			
				|  |  | +    args = parser.parse_args()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    if os.path.isdir(args.folder):
 | 
	
		
			
				|  |  | +        process_folder(args.folder)
 | 
	
		
			
				|  |  | +    else:
 | 
	
		
			
				|  |  | +        print(f"錯誤:文件夾 '{args.folder}' 不存在。")
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +if __name__ == "__main__":
 | 
	
		
			
				|  |  | +    main()
 |