from langchain_core.tools import tool import os import whisper @tool def transcribe_audio(file_path: str) -> str: """ Transcribes an audio file using OpenAI Whisper and returns the transcribed text. Args: file_path (str): Path to the audio file. """ if not os.path.exists(file_path): return f"Error: Audio file {file_path} not found." try: # Attempt transcription with Whisper # Using "base" model for a balance of speed and accuracy. # Other models: "tiny", "small", "medium", "large", "large-v2", "large-v3" # Consider making the model choice configurable if needed. model = whisper.load_model("base") result = model.transcribe(file_path, fp16=False) # fp16=False can improve compatibility/stability on some systems transcription = result["text"] if transcription.strip(): # Check if transcription is not empty or just whitespace return f"Audio transcription: {transcription}" else: return "Audio transcribed, but no text was detected." except Exception as e_whisper: # Catching a general exception, but more specific ones can be added # (e.g., for model loading errors, unsupported file formats by Whisper) return f"Error during audio transcription: {str(e_whisper)}"