CoolFace
Apppublic

MOSES3377/ai-interview-app

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
stt_utils.py45 linesDownload Raw Back to root
1import os2from openai import OpenAI3import logging4 5# Configure logging6logging.basicConfig(level=logging.INFO)7 8# Initialize OpenAI client from environment variable9try:10    client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))11except Exception as e:12    logging.error(f"Failed to initialize OpenAI client for STT: {e}")13    client = None14 15def transcribe_audio(audio_filepath: str) -> str:16    """17    Transcribes audio from a given file path using the OpenAI Whisper API.18 19    Args:20        audio_filepath (str): The path to the audio file to be transcribed.21 22    Returns:23        str: The transcribed text. Returns an empty string on failure.24    """25    if not client:26        logging.error("OpenAI client not initialized. Transcription failed.")27        return ""28        29    if not os.path.exists(audio_filepath):30        logging.error(f"Audio file not found at: {audio_filepath}")31        return ""32 33    try:34        with open(audio_filepath, "rb") as audio_file:35            # Call the Whisper API for transcription36            transcription = client.audio.transcriptions.create(37                model="whisper-1",38                file=audio_file39            )40        logging.info("Audio transcribed successfully.")41        return transcription.text42    except Exception as e:43        logging.error(f"An error occurred during audio transcription: {e}")44        return ""45