Package: voice-ai
NeuralSpace VoiceAI Python ClientInstallationpipinstallvoice-aiAuthenticationSet your NeuralSpace API Key to the environment variableNS_API_KEY:exportNS_API_KEY=YOUR_API_KEYAlternatively, you can also provide your API Key as a parameter when initializingVoiceAI:importneuralspaceasnsvai=ns.VoiceAI(api_key='YOUR_API_KEY')QuickstartFile Transcriptionimportneuralspaceasnsvai=ns.VoiceAI()# or,# vai = ns.VoiceAI(api_key='YOUR_API_KEY')# Setup job configurationconfig={"file_transcription":{"language_id":"en","mode":"advanced","number_formatting":"words",},}# Create a new file transcription jobjob_id=vai.transcribe(file='path/to/audio.wav',config=config)print(job_id)# Check the job's statusresult=vai.get_job_status(job_id)print(result)Streaming Real-Time TranscriptionimportjsonimportthreadingfromqueueimportQueueimportpyaudioimportneuralspaceasnsq=Queue()# callback for pyaudio to fill up the queuedeflisten(in_data,frame_count,time_info,status):q.put(in_data)return(None,pyaudio.paContinue)# transfer from queue to websocketdefsend_audio(q,ws):whileTrue:data=q.get()ws.send_binary(data)# initialize VoiceAIvai=ns.VoiceAI()pa=pyaudio.PyAudio()# open websocket connectionwithvai.stream('en')asws:# start pyaudio streamstream=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 threadt=threading.Thread(target=send_audio,args=(q,ws))t.start()print('Listening...')# start receiving results on the current threadwhileTrue:resp=ws.recv()resp=json.loads(resp)text=resp['text']# optional output formatting; new lines on every 'full' utteranceifresp['full']:print('\r'+' '*120,end='',flush=True)print(f'\r{text}',flush=True)else:iflen(text)>120:text=f'...{text[-115:]}'print(f'\r{text}',end='',flush=True)More FeaturesLanguage Detection and Speaker DiarizationTo 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 ConfigInstead of providing config as adict, you can provide it as astr,pathlib.Pathor 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,withopen('path/to/config.json')asfp:job_id=vai.transcribe(file='path/to/audio.wav',config=fp)Wait for completionYou 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.CallbacksYou can also provide a callback function when creating the job.It will be called with the result once the job completes.defcallback(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 thejob_idas 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.