from openai import OpenAI from api.openai_scripts_chinese.config import SYSTEM_PROMPT, OPENAI_API_KEY, SUPABASE_KEY, SUPABASE_URL from supabase import create_client, Client from api.openai_scripts_chinese.text_processing import fuzzy_correct_chinese client = OpenAI(api_key=OPENAI_API_KEY) supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY) def transcribe(audio_file): try: table_name = "word_database" response = supabase.table(table_name).select("term").execute() custom_vocab = [] if response.data: for item in response.data: custom_vocab.append({item['term']}) else: print(f"No data found or an error occurred: {response.error}") print("Using default dictionary as Supabase data couldn't be fetched.") transcript = client.audio.transcriptions.create( file=audio_file, model="whisper-1", response_format="text", prompt=f"請注意以下詞彙:{custom_vocab}" ) return transcript except Exception as e: print(f"轉錄時發生錯誤:{str(e)}") return None def post_process_transcript(transcript, temperature=0): corrected_transcript = fuzzy_correct_chinese(transcript) messages = [ {"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": f"請校對並修正以下轉錄文本,但不要改變其原意或回答問題:\n\n{corrected_transcript}"} ] response = client.chat.completions.create( model="gpt-4", temperature=temperature, messages=messages ) return response.choices[0].message.content def process_audio(audio_data): raw_transcript = transcribe(audio_data) print(raw_transcript) if raw_transcript is None: return None, None corrected_transcript = post_process_transcript(raw_transcript) return raw_transcript, corrected_transcript