SEERNET / deepaffects-python

Python library for DeepAffects API
https://developers.deepaffects.com
MIT License
11 stars 9 forks source link

Bad request EmotionApi #37

Closed JRomainG closed 5 years ago

JRomainG commented 5 years ago

I'm trying to use the EmotionApi's sync_recognise_emotion with wav files, but get a BAD REQUEST error.

Note that I encountered the issue #18, but fixed it by creating my own copy of audio_from_file:

def audio_from_file(file_name, language_code="en-US"):
    media_info = MediaInfo.parse(file_name)
    codec = media_info.tracks[0].__dict__["audio_codecs"]
    sampling_rate = media_info.tracks[1].__dict__["sampling_rate"]

    with open(file_name, "rb") as fin:
        audio_content = fin.read()

    audio = deepaffects.Audio(encoding=codec, sample_rate=sampling_rate, language_code=language_code,
                      content=base64.b64encode(audio_content).decode("utf-8"))
    return audio

Using the created Audio instance, I try to call the API's emotion recognizer:

deepaffects.configuration.api_key["apikey"] = API_KEY
api_instance = deepaffects.EmotionApi()

body = audio_from_file(file_name="/path/to/file.wav")

try: 
    # Find emotion in an audio file
    api_response = api_instance.sync_recognise_emotion(body)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling EmotionApi->sync_recognise_emotion: %s\n" % e)

An ApiException is raised with the following info:

Exception when calling EmotionApi->sync_recognise_emotion: (400)
Reason: BAD REQUEST
HTTP response headers: HTTPHeaderDict({'Content-Type': 'text/html; charset=utf-8', 'Content-Length': '98', 'Connection': 'keep-alive', 'Server': 'gunicorn/19.7.1', 'Date': 'Mon, 01 Jul 2019 12:42:47 GMT', 'ContentType': 'application/json', 'Access-Control-Allow-Origin': '*', 'X-Kong-Upstream-Latency': '3482', 'X-Kong-Proxy-Latency': '162', 'Via': 'kong/0.13.1'})
HTTP response body: {"fault": {"fault_string": "BAD REQUEST", "detail": {"error_code": "com.deepaffects.BadRequest"}}}

It seems related to #35, but as I'm simply using the Python API, I don't think the solution mentioned there applies to my case. Do you have any clue if I'm doing anything wrong? I also tried different WAV files but to no avail.

pip show deepaffects shows Version: 1.4.1

caluap commented 5 years ago

Hello @JRock007, I've been struggling with the exact same issue. Have you had any success with it?

sushant-hiray commented 5 years ago

Hello @caluap

We're currently in process of refactoring the library:

Can you try out the following snippet:

import requests
import base64

url = "https://proxy.api.deepaffects.com/audio/generic/api/v2/sync/recognise_emotion"

querystring = {"apikey":"<API_KEY>"}

payload = {
    "encoding": "Wave",
    "languageCode": "en-US"
}

# The api accepts data as base64 encoded content
# passing payload as content:
with open(audio_file_name, 'rb') as fin:
    audio_content = fin.read()
payload["content"] = base64.b64encode(audio_content).decode('utf-8')

headers = {
    'Content-Type': "application/json",
}

response = requests.post(url, json=payload, headers=headers, params=querystring)

print(response.text)
JRomainG commented 5 years ago

Your code indeed solves this issue for me. Thanks for your help!

rowhitswami commented 5 years ago

@JRock007 @sushant-hiray Can you provide API endpoints for diarization API?

JRomainG commented 5 years ago

@rowhitswami You should probably check the docs at https://docs.deepaffects.com/docs/speaker-diarization-api.html to get more info on how the diarization API works

caluap commented 5 years ago

@sushant-hiray it also worked here. Thanks a lot!