wit-ai / pywit

Python library for Wit.ai
Other
1.45k stars 359 forks source link

Getting either Error 400 or KeyError #144

Closed JVTEAM closed 4 years ago

JVTEAM commented 4 years ago

Do you want to request a feature, report a bug, or ask a question about pywit? Bug What is the current behavior? If I install from source I get inputWords = str(resp['_text']) KeyError: '_text' If I install from pip I get wit.wit.WitError: Wit responded with status: 400 (Bad Request)

I ran the same code on another machine that has worked in the past and it worked fine. If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. import subprocess import time import re from wit import Wit

the wit.ai API key (this is a fake one you will need to sign up for your own at wit.ai)

client_wit = Wit('MYCODE')

def listen(): inputWords = "" while (re.search('[a-zA-Z]', inputWords) is None):

using subprocess to call the sox recording software with a configuration to trim silence from the recording and stop recording when the speaker has finished

    #subprocess.call(['rec rec.wav rate 32k silence 1 0.1 5% 1 1.0 5%'], shell=True)
    subprocess.call(['arecord --device=pulse -d 6 -f cd -t wav rec.wav'], shell=True)
    resp = None

use the wit.ai class to interface with the API and send off the wav file from above for STT functions

    with open('rec.wav', 'rb') as f:
      resp = client_wit.speech(f, None, {'Content-Type': 'audio/wav'})
    #parse the response given to get the text sent back which will then become the words the bot uses
    inputWords = str(resp['_text'])
    #inputWords = str(resp['text'])
    #print the input words to the screen (debug/testing purposes)

return inputWords

if called direct then run the function

if name == 'main': print(listen())

What is the expected behavior? It will get a response back from wit.ai for my chatbot to respond to.

If applicable, what is the App ID where you are experiencing this issue? If you do not provide this, we cannot help. 2025978014172865

EviGL commented 4 years ago

I'm also getting 400 (Bad Request) after uploading wav file using just the example code:

client = Wit("mykey")
resp = client.message('what is the weather in London?') //works fine
print('Yay, got Wit.ai response: ' + str(resp)) 

resp = None
with open("Recording.wav", 'rb') as f:
    resp = client.speech(f, {'Content-Type': 'audio/wav'}) //raises an error 400
print('Yay, got Wit.ai response: ' + str(resp))

app id 266939828014790

patapizza commented 4 years ago

Hi @JVTEAM, the latest code is using the latest API version - check out the updated documentation.

@EviGL I was able to run a successful speech request using:

client = Wit(access_token=access_token)
with open('speech.wav', 'rb') as f:
    resp = client.speech(f, {'Content-Type': 'audio/wav'})
print('Yay, got Wit.ai response: ' + str(resp))

Is the file properly encoded? My file has 16k sample rate, monochannel, 256k bit rate, 16-bit signed integer PCM sample encoding.

JVTEAM commented 4 years ago

It failed on every device I was using it for. Switched to Google speech input.

patapizza commented 4 years ago

The new key is text, not _text.