Prerequisites

  • A valid API key
  • Endpoint URL for Pulse API
  • Python 3.7+ OR JavaScript
Valence provides Python and JavaScript SDKs, though you can also make direct HTTPS requests to the DiscreteAPI. The AsynchAPI can only be accessed through the SDK.

DiscreteAPI Examples

Below are two examples of direct usage of our DiscreteAPI. There are two data input options: [multipart/form-data] and [application/json] . [multipart/form-data] allows a user to directly upload an audio file and [application/json] allows a user to process their data before sending it to the API. File upload
import requests

# Define the API endpoint and the API key
API_LINK = 'https://api.getvalenceai.com/emotionprediction'
API_KEY = 'YOUR_API_KEY'

# Define the file path to upload
file_path = "YOUR_FILE"

# Open the file in binary mode
with open(file_path, 'rb') as file:
    # Prepare the files parameter with the file object
    files = {
		'file': file
	}
    # Prepare the headers with the API key
    headers = {
	'x-api-key': API_KEY
	}
    # Make the POST request to upload the file
    response = requests.post(API_LINK, files=files, headers=headers)
    
    # Check the response status
    if response.status_code == 200:
        print("Response:", response.json())
    else:
        print(f"Failed to upload file, {response.status_code}, {response.text}")
Send processed data
import requests
import librosa
import json
import numpy as np

API_LINK = 'https://api.getvalenceai.com/emotionprediction'
API_KEY = 'YOUR_API_KEY'

headers = {
    'Content-Type': 'application/json',
    'x-api-key': API_KEY
}

fp = 'YOUR_FILE.wav'
X, sample_rate = librosa.load(fp, sr=44100, duration=4.5, offset=0.5)
event = {'payload': [X[:198450].tolist()]}
jevent = json.dumps(event)

response = requests.post(API_LINK, json=jevent, headers=headers)
print(response.status_code)
print(response.headers)
print(response.json())
All data sent to the DiscreteAPI is data-in-transit. It is not stored on Valence’s systems.

AsynchAPI Examples

Below are examples of usage for our AsynchAPI. This API has to be used in conjunction with the SDK and requires direct file upload. In these examples, the API key is included as an environment variable.
from valenceai import ValenceClient

client = ValenceClient(show_progress=True)

# Upload the audio file
request_id = client.asynch.upload("YOUR_FILE.wav")

# Get emotions from uploaded audio
result = client.asynch.emotions(request_id)
print(result)
All files sent to the AsynchAPI are briefly saved on Valence’s systems while being processed. Our database is cleared every 24 hours. More information on data privacy can be found here.