NeuralSpace VoiceAI Python Client
Installation
pip install voice-ai
Authentication
Set your NeuralSpace API Key to the environment variable NS_API_KEY:
export NS_API_KEY=YOUR_API_KEY
Alternatively, you can also provide your API Key as a parameter when initializing VoiceAI:
import neuralspace as ns
vai = ns.VoiceAI(api_key='YOUR_API_KEY')
Quickstart
File Transcription
import neuralspace as ns
vai = ns.VoiceAI()
# or,
# vai = ns.VoiceAI(api_key='YOUR_API_KEY')
# Setup job configuration
config = {
"file_transcription": {
"language_id": "en",
"mode": "advanced",
"number_formatting": "words",
},
}
# Create a new file transcription job
job_id = vai.transcribe(file='path/to/audio.wav', config=config)
print(job_id)
# Check the job's status
result = vai.get_job_status(job_id)
print(result)
Streaming Real-Time Transcription
import json
import threading
from queue import Queue
import pyaudio
import neuralspace as ns
q = Queue()
# callback for pyaudio to fill up the queue
def listen(in_data, frame_count, time_info, status):
q.put(in_data)
return (None, pyaudio.paContinue)
# transfer from queue to websocket
def send_audio(q, ws):
while True:
data = q.get()
ws.send_binary(data)
# initialize VoiceAI
vai = ns.VoiceAI()
pa = pyaudio.PyAudio()
# open websocket connection
with vai.stream('en') as ws:
# start pyaudio stream
stream = pa.open(
rate=16000,
channels=1,
format=pyaudio.paInt16,
frames_per_buffer=4096,
input=True,
output=False,
stream_callback=listen,
)
# start sending audio bytes on a new thread
t = threading.Thread(target=send_audio, args=(q, ws))
t.start()
print('Listening...')
# start receiving results on the current thread
while True:
resp = ws.recv()
resp = json.loads(resp)
text = resp['text']
# optional output formatting; new lines on every 'full' utterance
if resp['full']:
print('\r' + ' ' * 120, end='', flush=True)
print(f'\r{text}', flush=True)
else:
if len(text) > 120:
text = f'...{text[-115:]}'
print(f'\r{text}', end='', flush=True)
More Features
Language Detection and Speaker Diarization
To enable language detection and speaker diarization, update the config as below:
config = {
"file_transcription": {
"language_id": "en",
"mode": "advanced",
"number_formatting": "words",
},
"language_detect": {},
"speaker_diarization": {},
}
Job Config
Instead of providing config as a dict, you can provide it as a str, pathlib.Path or a file-like object.
job_id = vai.transcribe(
file='path/to/audio.wav',
config='{"file_transcription": {"language_id": "en", "mode": "advanced", "number_formatting": "words"}}',
)
# or,
job_id = vai.transcribe(
file='path/to/audio.wav',
config='path/to/config.json',
)
# or,
with open('path/to/config.json') as fp:
job_id = vai.transcribe(
file='path/to/audio.wav',
config=fp
)
Wait for completion
You can also poll for the status and wait until the job completes:
result = vai.poll_until_complete(job_id)
print(result.data.result.transcription.transcript)
Note: This will block the calling thread until the job is complete.
Callbacks
You can also provide a callback function when creating the job.
It will be called with the result once the job completes.
def callback(result):
print(f'job completed: {result.data.jobId}')
print(result.data.result.transcription.transcript)
job_id = vai.transcribe(file='path/to/audio.wav', config=config, on_complete=callback)
Note: transcribe() will return the job_id as soon as the job is scheduled, and the provided callback will be called on a new thread.
The calling thread will not be blocked in this case.
Metadata
Release files for voice-ai 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| voice-ai-0.0.2.tar.gz | 6.9 kB | Without provenance | Details for voice-ai-0.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| voice_ai-0.0.2-py3-none-any.whl | Python 3 | none | any | Details for voice_ai-0.0.2-py3-none-any.whl |
Total release size: 14.0 kB
Release files / voice-ai-0.0.2.tar.gz
| Download URL | voice-ai-0.0.2.tar.gz |
|---|---|
| Size | 6.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c0c7762809c7e984a9e4466a0cbb1c57b66b55c33e53429929bad07c3a378978
|
|
BLAKE2b-256 checksum How to use checksums |
7b3c0d0771793498a38e583f3281c667c292afd220c09cdef2b08a2016b68609
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|
Release files / voice_ai-0.0.2-py3-none-any.whl
| Download URL | voice_ai-0.0.2-py3-none-any.whl |
|---|---|
| Size | 7.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f8c0136b320507e20fd2e6d3eec4c1c5fd5af17c913009da9d1867af6c180e1b
|
|
BLAKE2b-256 checksum How to use checksums |
e11b8fb503bba520bdec08be0c0a3291618c129e1fd9ae7958243d00cd51dac3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|
Security for voice-ai 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.