Spaces:
Sleeping
Sleeping
| from smolagents import tool | |
| import os | |
| 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}" | |