Streaming API Tutorial

Overview

The streaming API is build for analysis of audio, video, and text streams. By connecting to WebSocket endpoints you can get near real-time feedback on the expressive and emotional content of your data.

If you haven't already, grab your API key using the instructions in the Get Started guide.

Here we will utilize Hume Python SDK first we need to install it.

Install Hume Python SDK

Websocket and streaming features can be installed with:

pip install "hume[stream]"

Now that we have it installed lets get started!

Streaming text with the Hume Python SDK

This example uses our language model to get a sentiment analysis on a children's nursery rhyme.

import asyncio

from hume import HumeStreamClient
from hume.models.config import LanguageConfig

samples = [
    "Mary had a little lamb,",
    "Its fleece was white as snow."
    "Everywhere the child went,"
    "The little lamb was sure to go."
]

async def main():
    client = HumeStreamClient("<your-api-key>")
    config = LanguageConfig()
    async with client.connect([config]) as socket:
        for sample in samples:
            result = await socket.send_text(sample)
            emotions = result["language"]["predictions"][0]["emotions"]
            print(emotions)

asyncio.run(main())

Your result should look something like this:

[{'name': 'Admiration', 'score': 0.06379243731498718}, 
 {'name': 'Adoration', 'score': 0.07222934812307358}, 
 {'name': 'Aesthetic Appreciation', 'score': 0.02808445133268833}, 
 {'name': 'Amusement', 'score': 0.027589013800024986}, 
 ......
 {'name': 'Surprise (positive)', 'score': 0.030542362481355667}, 
 {'name': 'Sympathy', 'score': 0.03246130049228668},
 {'name': 'Tiredness', 'score': 0.03606246039271355}, 
 {'name': 'Triumph', 'score': 0.01235896535217762}]

Congratulations! You made your first Streaming API call using our Python SDK!


What’s Next

Learn more about our Streaming API