gradio-transcript-mcp / transcription.py
Bismay
Initial commit
83a4e82
import torch
from transformers import pipeline
from logging_config import logger
def run_whisper_transcription(wav_file_path: str, device: str):
try:
model_name = "distil-whisper/distil-small.en"
logger.info(f"Initialising Whisper ASR pipeline with model: {model_name}")
logger.info(f"Running pipeline on device: {device}")
asr_pipeline = pipeline(
"automatic-speech-recognition",
model=model_name,
device=0 if device == "cuda" else -1,
return_timestamps=True
)
logger.info("Whisper ASR pipeline initialised.")
logger.info(f"Starting transcription for file: {wav_file_path}")
# Perform transcription
result = asr_pipeline(wav_file_path)
transcription = result.get("text", "")
logger.info("Transcription completed successfully.")
yield transcription # Yield only the transcription string
except Exception as e:
err_msg = f"Error during transcription: {str(e)}"
logger.error(err_msg)
yield err_msg # Yield only the error message string