Download tools/transcribe_audio.py from dmfelder/DF_Final_Assignment_Template: direct link, hf CLI and curl.
- Browser
- Download file 943 Bytes
-
https://huggingface.co/spaces/dmfelder/DF_Final_Assignment_Template/resolve/main/tools/transcribe_audio.py
- Command line
-
hf download hf://spaces/dmfelder/DF_Final_Assignment_Template/tools/transcribe_audio.py
-
curl -L -o transcribe_audio.py https://huggingface.co/spaces/dmfelder/DF_Final_Assignment_Template/resolve/main/tools/transcribe_audio.py
943 Bytes
| from langchain_core.tools import tool | |
| from loguru import logger | |
| import tempfile | |
| import whisper | |
| from tools.load_file import load_file | |
| 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 "" | |