File size: 875 Bytes
2606dba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import os
import openai
from smolagents import Tool

# Load OpenAI API key from environment
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 = {
        "file_path": {
            "type": "string",
            "description": "Path to the audio file (must be .mp3 or .wav)"
        }
    }
    output_type = "string"

    def forward(self, file_path: str) -> str:
        try:
            with open(file_path, "rb") as audio_file:
                transcript = openai.audio.transcriptions.create(
                    model="whisper-1",
                    file=audio_file
                )
            return transcript.text
        except Exception as e:
            return f"Error transcribing audio: {e}"