File size: 1,079 Bytes
2606dba
02a5e73
2606dba
 
 
 
 
 
 
 
 
02a5e73
2606dba
02a5e73
2606dba
 
 
 
02a5e73
2606dba
02a5e73
 
 
 
 
 
 
 
2606dba
 
 
 
02a5e73
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
30
31
32
33
34
35
36
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}"