test2 / async_ai_voice.py
dynstat's picture
Upload 2 files
f4da5b0 verified
Raw History Blame Contribute Delete
957 Bytes
from transformers import pipeline
from typing import Dict, Any
# Initialize the transcription pipeline
# transcriber = pipeline("automatic-speech-recognition", model="distil-whisper/distil-large-v3")
transcriber = pipeline("automatic-speech-recognition", model="distil-whisper/distil-small.en")
async def send_to_voice_AI(audio_data: bytes) -> Dict[str, Any]:
"""
Transcribes audio data using the Hugging Face Whisper model.
Args:
audio_data (bytes): The raw audio data to be transcribed.
Returns:
Dict[str, Any]: A dictionary containing the transcription result or error message.
"""
print("Transcribing audio")
try:
# Transcribe the audio
result = transcriber(audio_data)
return {"text": result["text"]}
except Exception as e:
print(f"Error during transcription: {str(e)}")
return {"text": "Sorry, an error occurred during transcription."}