DF_Final_Assignment_Template / tools /transcribe_audio.py
dmfelder's picture
Update tools/transcribe_audio.py
ac03288 verified
Raw History Blame Contribute Delete
943 Bytes
from langchain_core.tools import tool
from loguru import logger
import tempfile
import whisper
from tools.load_file import load_file
@tool("transcribe_audio", parse_docstring=True)
def transcribe_audio(file_name: str) -> str:
"""
Transcribes audio from a given file.
Args:
file_name (str): The name of the audio file to transcribe.
Returns:
str: The transcribed text from the audio file.
"""
logger.info(f"use transcribe_audio_tool with param: {file_name}")
model_name = "base"
try:
audio_bytes = load_file(file_name)
with tempfile.NamedTemporaryFile() as tmp:
tmp.write(audio_bytes.getvalue())
tmp.flush()
model = whisper.load_model(model_name)
result = model.transcribe(tmp.name, fp16=False)
return result["text"]
except Exception as e:
print(f"Error transcribing audio: {str(e)}")
return ""