NVIDIA Riva Client

2.25.1 · active · verified Thu Apr 16

The `nvidia-riva-client` library provides Python client APIs for interacting with NVIDIA Riva speech AI services, including Automatic Speech Recognition (ASR), Text-to-Speech (TTS), and Neural Machine Translation (NMT). It enables developers to integrate advanced conversational AI capabilities into their applications. The current version is 2.25.1, with releases typically tied to major Riva platform updates, leading to a moderately frequent release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up the Riva client, connect to a Riva server, and perform a simple Automatic Speech Recognition (ASR) task on a local WAV file. Ensure the `RIVA_URI` environment variable is set to your Riva server's address (e.g., `localhost:50051`). The script creates a dummy audio file if one isn't present for demonstration.

import os
import riva.client
import time
import wave

# Configure Riva server connection
# Ensure RIVA_URI is set in your environment (e.g., 'localhost:50051' or a remote address)
# For authenticated connections, set RIVA_API_KEY if needed.
riva_uri = os.environ.get('RIVA_URI', 'localhost:50051')

# Simple WAV file for ASR (create a dummy one if not present)
dummy_audio_file = 'dummy_audio.wav'
if not os.path.exists(dummy_audio_file):
    with wave.open(dummy_audio_file, 'wb') as wf:
        wf.setnchannels(1)
        wf.setsampwidth(2)
        wf.setframerate(16000)
        wf.writeframes(b'\x00' * 16000 * 2) # 1 second of silence
    print(f"Created a dummy audio file: {dummy_audio_file}")

try:
    # Establish authentication (if needed, otherwise Auth() is sufficient)
    auth = riva.client.Auth(uri=riva_uri)

    # Initialize ASR client
    asr_client = riva.client.ASRClient(auth)

    # Configure ASR recognition
    config = riva.client.RecognitionConfig(
        encoding=riva.client.AudioEncoding.LINEAR_PCM, # or FLAC, MULAW, etc.
        sample_rate_hertz=16000,
        language_code="en-US",
        max_alternatives=1,
        enable_automatic_punctuation=True,
    )

    # Perform ASR on a local audio file
    print(f"Transcribing {dummy_audio_file} from Riva server at {riva_uri}...")
    response = asr_client.recognize_file(dummy_audio_file, config)

    # Print results
    if response.results:
        for result in response.results:
            if result.alternatives:
                print(f"Transcription: {result.alternatives[0].transcript}")
            else:
                print("No alternatives found for this segment.")
    else:
        print("No speech recognized in the audio.")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure the Riva server is running and accessible at the specified RIVA_URI.")

view raw JSON →