Final_Assignment_Template / audio_transcriber.py
dlaima's picture
Create audio_transcriber.py
2606dba verified
raw
history blame
875 Bytes
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}"