import os import requests import openai from smolagents import Tool openai.api_key = os.getenv("OPENAI_API_KEY") class AudioTranscriptionTool(Tool): name = "audio_transcriber" description = "Transcribe a given audio file in mp3 or wav format to text using Whisper." inputs = { "url": { "type": "string", "description": "URL to the audio file (.mp3 or .wav)" } } output_type = "string" def forward(self, url: str) -> str: try: # Download audio filename = "/tmp/audio_input.mp3" response = requests.get(url) with open(filename, "wb") as f: f.write(response.content) # Transcribe with Whisper with open(filename, "rb") as audio_file: transcript = openai.audio.transcriptions.create( model="whisper-1", file=audio_file ) return transcript.text.strip() except Exception as e: return f"Error transcribing audio: {e}"