Download async_ai_voice.py from dynstat/test2: direct link, hf CLI and curl.
- Browser
- Download file 957 Bytes
-
https://huggingface.co/spaces/dynstat/test2/resolve/main/async_ai_voice.py
- Command line
-
hf download hf://spaces/dynstat/test2/async_ai_voice.py
-
curl -L -o async_ai_voice.py https://huggingface.co/spaces/dynstat/test2/resolve/main/async_ai_voice.py
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."} |