Guide de démarrage : transcrire la parole en temps réel
Diffusez de l'audio en direct vers l'API de transcription en temps réel et obtenez une transcription à faible latence. Pour la transcription hors ligne de fichiers préenregistrés, consultez Transcrire un fichier audio.
Obtenir une clé API
Vous avez d'abord besoin d'une clé API Mistral. Si vous n'en avez pas, rendez-vous sur Studio. Accédez à la section Clés API et créez une nouvelle clé API.
Stockez votre clé API dans la variable d'environnement MISTRAL_API_KEY :
export MISTRAL_API_KEY="your-api-key"Installer le SDK
Le client de transcription en temps réel nécessite l'extra realtime.
pip install "mistralai[realtime]"Connecter et diffuser
L'API temps réel utilise une connexion WebSocket. Le SDK ouvre la connexion, envoie des fragments audio en streaming au serveur et produit les événements de transcription à mesure qu'ils arrivent. L'exemple ci-dessous lit un fichier WAV par fragments de 100 ms et les envoie au rythme du temps réel pour simuler un micro en direct. Pour une entrée micro réelle, remplacez read_chunks par votre source audio.
Pour suivre ce tutoriel sans votre propre fichier, téléchargez l'enregistrement d'exemple :
curl -O https://docs.mistral.ai/audio/chat.wavimport asyncio
import os
import wave
from mistralai.client import Mistral
from mistralai.client.models import (
AudioFormat,
RealtimeTranscriptionError,
TranscriptionStreamDone,
TranscriptionStreamTextDelta,
)
AUDIO_FILE = "chat.wav" # 16-bit mono PCM WAV
async def read_chunks(path):
with wave.open(path, "rb") as wav:
while chunk := wav.readframes(wav.getframerate() // 10):
yield chunk
await asyncio.sleep(0.1) # send 100 ms of audio every 100 ms, like a live source
async def main():
with wave.open(AUDIO_FILE, "rb") as wav:
sample_rate = wav.getframerate()
client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
async for event in client.audio.realtime.transcribe_stream(
read_chunks(AUDIO_FILE),
model="voxtral-mini-transcribe-realtime-3",
audio_format=AudioFormat(encoding="pcm_s16le", sample_rate=sample_rate),
):
if isinstance(event, TranscriptionStreamTextDelta):
print(event.text, end="", flush=True)
elif isinstance(event, TranscriptionStreamDone):
print(f"\n\nFinal transcript: {event.text}")
elif isinstance(event, RealtimeTranscriptionError):
print(f"\nError: {event.error.message}")
asyncio.run(main())Ce que vous devriez voir
Le script affiche la transcription au fur et à mesure de son arrivée, puis la transcription finale :
Transcribe what I'm about to say. Today, on July 15th, 2025, Mistral AI is announcing the new Voxtral models. Okay, and the transcription there and then write code to give the first n digits of the Fibonacci sequence.
Final transcript: Transcribe what I'm about to say. Today, on July 15th, 2025, Mistral AI is announcing the new Voxtral models. Okay, and the transcription there and then write code to give the first n digits of the Fibonacci sequence.