Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add emotion text emotion api #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions deepaffects/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,14 @@ def deserialize(self, response, response_type):
# handle file downloading
# save response body into a tmp file and return the instance
if response_type == "file":
return self.__deserialize_file(response)

return self.__deserialize_file(response)
# fetch data from response object
try:
data = json.loads(response.data)
except ValueError:
data = response.data
if response_type == "json":
return self.__deserialize_object(data)

return self.__deserialize(data, response_type)

Expand Down
110 changes: 110 additions & 0 deletions deepaffects/apis/emotion_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,113 @@ def sync_recognise_emotion_with_http_info(self, body, **kwargs):
_preload_content=params.get('_preload_content', True),
_request_timeout=params.get('_request_timeout'),
collection_formats=collection_formats)
def sync_text_recognise_emotion(self, body, **kwargs):
"""
Find emotion in text
Extract emotion from text.
This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please define a `callback` function
to be invoked when receiving the response.
>>> def callback_function(response):
>>> pprint(response)
>>>
>>> thread = api.sync_text_recognise_emotion(body, callback=callback_function)

:param callback function: The callback function
for asynchronous request. (optional)
:param Text: Text that needs to be featurized. (required)
:return: list[EmotionScore]
If the method is called asynchronously,
returns the request thread.
"""
kwargs['_return_http_data_only'] = True
if kwargs.get('callback'):
return self.sync_text_recognise_emotion_with_http_info(body, **kwargs)
else:
(data) = self.sync_text_recognise_emotion_with_http_info(body, **kwargs)
return data

def sync_text_recognise_emotion_with_http_info(self, body, **kwargs):
"""
Find emotion in text
Extract emotion from text.
This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please define a `callback` function
to be invoked when receiving the response.
>>> def callback_function(response):
>>> pprint(response)
>>>
>>> thread = api.sync_text_recognise_emotion_with_http_info(body, callback=callback_function)

:param callback function: The callback function
for asynchronous request. (optional)
:param Text body: Text that needs to be featurized. (required)
:return: list[EmotionScore]
If the method is called asynchronously,
returns the request thread.
"""

all_params = ['body']
all_params.append('callback')
all_params.append('_return_http_data_only')
all_params.append('_preload_content')
all_params.append('_request_timeout')

params = locals()
for key, val in iteritems(params['kwargs']):
if key not in all_params:
raise TypeError(
"Got an unexpected keyword argument '%s'"
" to method sync_text_recognise_emotion" % key
)
params[key] = val
del params['kwargs']
# verify the required parameter 'body' is set
if ('body' not in params) or (params['body'] is None):
raise ValueError(
"Missing the required parameter `body` when calling `sync_text_recognise_emotion`")

collection_formats = {}

resource_path = '/text/generic/api/latest/sync/text_recognise_emotion'.replace(
'{format}', 'json')
path_params = {}

query_params = []

header_params = {}

form_params = []
local_var_files = {}

body_params = None
if 'body' in params:
body_params = params['body']
# HTTP header `Accept`
header_params['Accept'] = self.api_client.\
select_header_accept(['application/json'])

# HTTP header `Content-Type`
header_params['Content-Type'] = self.api_client.\
select_header_content_type(['application/json'])

# Authentication setting
auth_settings = ['UserSecurity']

return self.api_client.call_api(resource_path, 'POST',
path_params,
query_params,
header_params,
body=body_params,
post_params=form_params,
files=local_var_files,
response_type='json',
auth_settings=auth_settings,
callback=params.get('callback'),
_return_http_data_only=params.get(
'_return_http_data_only'),
_preload_content=params.get(
'_preload_content', True),
_request_timeout=params.get(
'_request_timeout'),
collection_formats=collection_formats)
17 changes: 17 additions & 0 deletions test/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# coding: utf-8

"""
DeepAffects

OpenAPI Specification of DeepAffects APIs

OpenAPI spec version: 0.1.0

Generated by: https://github.com/swagger-api/swagger-codegen.git
"""


from __future__ import absolute_import

# import apis into sdk package
from .test_base_setup import DIR
Binary file modified test/data/reconstructed.wav
Binary file not shown.
18 changes: 15 additions & 3 deletions test/test_denoise_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
import os
import sys
import unittest

import uuid
import deepaffects
from deepaffects.models.audio import Audio
from .test_base_setup import DIR



class TestDenoiseApi(unittest.TestCase):
""" DenoiseApi unit test stubs """

Expand All @@ -29,9 +30,21 @@ def sdr(clean, reconstructed):
def setUp(self):
deepaffects.configuration.api_key['apikey'] = os.environ['DEEPAFFECTS_API_KEY']
self.api = deepaffects.DenoiseApi()
self.webhook_url = os.environ["DEEPAFFECTS_API_WEBHOOK"]

def tearDown(self):
pass
def test_async_denoise_audio(self):
"""
Test case for sync_denoise_audio

Denoise an audio file
"""
self.request_id = str(uuid.uuid4())
test_noisy_audio = os.path.normpath(os.path.join(DIR, "data/noisy.wav"))
body = Audio.from_file(file_name=test_noisy_audio)
api_response = self.api.async_denoise_audio(body=body, webhook=self.webhook_url, request_id=self.request_id)
assert api_response.request_id, self.request_id

def test_sync_denoise_audio(self):
"""
Expand All @@ -45,8 +58,7 @@ def test_sync_denoise_audio(self):
body = Audio.from_file(file_name=test_noisy_audio)
api_response = self.api.sync_denoise_audio(body=body)
api_response.to_file(test_reconstructed_audio)
self.assertTrue(TestDenoiseApi.sdr(test_clean_audio, test_reconstructed_audio) > 5)
pass
assert TestDenoiseApi.sdr(test_clean_audio, test_reconstructed_audio) > 5


if __name__ == '__main__':
Expand Down
28 changes: 20 additions & 8 deletions test/test_diarize_api_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import os
import sys
import unittest

import deepaffects
from deepaffects import Audio
from deepaffects.rest import ApiException
Expand All @@ -27,23 +26,36 @@ class TestDiarizeApiV2(unittest.TestCase):
def setUp(self):
deepaffects.configuration.api_key['apikey'] = os.environ['DEEPAFFECTS_API_KEY']
self.webhook_url = os.environ["DEEPAFFECTS_API_WEBHOOK"]
self.api = deepaffects.apis.diarize_api_v2.DiarizeApiV2()
self.request_id = str(uuid.uuid4())
self.api = deepaffects.apis.diarize_api_v2.DiarizeApiV2()

def tearDown(self):
pass

def test_async_diarize_audio(self):
def test_async_diarize_audio_with_request_id(self):
"""
Test case for diarize_audio

Diarize an audio file
"""
self.request_id = str(uuid.uuid4())
test_conversation_audio = os.path.normpath(os.path.join(DIR, "data/happy.mp3"))
body = Audio.from_file(file_name=test_conversation_audio)

body = Audio.from_file(file_name=test_conversation_audio)
api_response = self.api.async_diarize_audio(body=body, webhook=self.webhook_url, request_id=self.request_id)
self.assertTrue(api_response.request_id, self.request_id)
print(api_response)
assert api_response.request_id, self.request_id

def test_async_diarize_audio_without_request_id(self):
"""
Test case for diarize_audio

Diarize an audio file
"""
test_conversation_audio = os.path.normpath(os.path.join(DIR, "data/happy.mp3"))
body = Audio.from_file(file_name=test_conversation_audio)
api_response = self.api.async_diarize_audio(body=body, webhook=self.webhook_url)
print(api_response)
assert api_response.request_id


if __name__ == '__main__':
unittest.main()
unittest.main()
27 changes: 23 additions & 4 deletions test/test_emotion_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,49 @@
import unittest

from deepaffects import Audio

import uuid
import deepaffects
from deepaffects.rest import ApiException
from deepaffects.apis.emotion_api import EmotionApi
from deepaffects.models.emotion_score import EmotionScore
from .test_base_setup import DIR, AudioTest
from .test_base_setup import DIR



class TestEmotionApi(unittest.TestCase):
""" EmotionApi unit test stubs """

def setUp(self):
deepaffects.configuration.api_key['apikey'] = os.environ['DEEPAFFECTS_API_KEY']
self.webhook_url = os.environ["DEEPAFFECTS_API_WEBHOOK"]
self.api = deepaffects.apis.emotion_api.EmotionApi()

def tearDown(self):
pass

def test_async_recognise_emotion(self):
def test_async_recognise_emotion_with_request_id(self):
"""
Test case for async_recognise_emotion

Find emotion in an audio file
"""
pass
self.request_id = str(uuid.uuid4())
test_happy_audio = os.path.normpath(os.path.join(DIR, "data/happy.mp3"))
body = Audio.from_file(file_name=test_happy_audio)
api_response = self.api.async_recognise_emotion(body=body, webhook=self.webhook_url, request_id=self.request_id)
print(api_response)
assert api_response.request_id, self.request_id
def test_async_recognise_emotion_without_request_id(self):
"""
Test case for async_recognise_emotion

Find emotion in an audio file
"""
test_happy_audio = os.path.normpath(os.path.join(DIR, "data/happy.mp3"))
body = Audio.from_file(file_name=test_happy_audio)
api_response = self.api.async_recognise_emotion(body=body, webhook=self.webhook_url)
print(api_response)
assert api_response.request_id

def test_sync_recognise_emotion(self):
"""
Expand All @@ -50,6 +68,7 @@ def test_sync_recognise_emotion(self):
test_happy_audio = os.path.normpath(os.path.join(DIR, "data/happy.mp3"))
body = Audio.from_file(file_name=test_happy_audio)
api_response = self.api.sync_recognise_emotion(body=body)
print(api_response)
for obj in api_response:
if obj.emotion == 'Happy':
assert obj.score > 0.8
Expand Down
76 changes: 76 additions & 0 deletions test/test_text_recognise_emotion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# coding: utf-8

"""
DeepAffects

OpenAPI spec version: v1
"""


from __future__ import absolute_import

import os
import sys
import unittest

sys.path.insert(0, '/Users/VivekNimkarde/DeepAffects/deepaffects-python')
import deepaffects
from deepaffects import Audio
from deepaffects.rest import ApiException
from .test_base_setup import DIR
import uuid


class TestTextEmootion(unittest.TestCase):
""" Text Emotion unit test stubs """

def setUp(self):
deepaffects.configuration.api_key['apikey'] = os.environ['DEEPAFFECTS_API_KEY']
self.api = deepaffects.apis.emotion_api.EmotionApi()

def tearDown(self):
pass

def test_sync_diarize_audio_sample_text(self):
"""
Test case for Text Emotion

Diarize text file
"""

body = {
"content": "Awesome"
}
api_response = self.api.sync_text_recognise_emotion(body=body)
print(api_response)
assert api_response['response']['trust']> 0.8
def test_sync_diarize_audio_check_response_field_exists(self):
"""
Test case for Text Emotion

Diarize text file
"""

body = {
"content": "Awesome"
}
api_response = self.api.sync_text_recognise_emotion(body=body)
print(api_response)
assert api_response['response']
def test_sync_diarize_audio_check_version_field_exists(self):
"""
Test case for Text Emotion

Diarize text file
"""

body = {
"content": "Awesome"
}
api_response = self.api.sync_text_recognise_emotion(body=body)
print(api_response)
assert api_response['version']


if __name__ == '__main__':
unittest.main()