File size: 1,030 Bytes
44ff471
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
from smolagents import tool
import os

@tool
def transcribe_audio(file_path: str) -> str:
    """
    Transcribes an audio file using faster-whisper.

    Args:
        file_path: The path to the audio file.

    Returns:
        The transcribed text, or an error message if transcription fails.
    """
    # Use faster-whisper if available
    try:
        from faster_whisper import WhisperModel
        import torch

        if torch.cuda.is_available():
            device = "cuda"
        else:
            device = "cpu"

        model = WhisperModel("base", device=device)
        segments, info = model.transcribe(file_path)
        text_parts = []
        for seg in segments:
            try:
                text_parts.append(seg.text)
            except Exception:
                pass
        text = " ".join(text_parts).strip()
        return text or "[ASR result is empty]"
    except Exception as e:
        return f"[ASR is not available] Please install `faster-whisper` to enable audio recognition. Error: {e}"