Final_Assignment_Template / audio_transcriber.py
dlaima's picture
Update audio_transcriber.py
02a5e73 verified
raw
history blame
1.08 kB
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}"