diff --git a/deepaffects/api_client.py b/deepaffects/api_client.py index 479c7f4..a9677d6 100644 --- a/deepaffects/api_client.py +++ b/deepaffects/api_client.py @@ -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) diff --git a/deepaffects/apis/emotion_api.py b/deepaffects/apis/emotion_api.py index e4fb875..0ef2335 100644 --- a/deepaffects/apis/emotion_api.py +++ b/deepaffects/apis/emotion_api.py @@ -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) diff --git a/test/__init__.py b/test/__init__.py index e69de29..8827667 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -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 \ No newline at end of file diff --git a/test/data/reconstructed.wav b/test/data/reconstructed.wav index fb3c9ec..58ec47a 100644 Binary files a/test/data/reconstructed.wav and b/test/data/reconstructed.wav differ diff --git a/test/test_denoise_api.py b/test/test_denoise_api.py index a6be930..f8b10c0 100644 --- a/test/test_denoise_api.py +++ b/test/test_denoise_api.py @@ -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 """ @@ -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): """ @@ -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__': diff --git a/test/test_diarize_api_v2.py b/test/test_diarize_api_v2.py index 2425bdb..c8c628f 100644 --- a/test/test_diarize_api_v2.py +++ b/test/test_diarize_api_v2.py @@ -12,7 +12,6 @@ import os import sys import unittest - import deepaffects from deepaffects import Audio from deepaffects.rest import ApiException @@ -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() \ No newline at end of file diff --git a/test/test_emotion_api.py b/test/test_emotion_api.py index 932f283..8ec8403 100644 --- a/test/test_emotion_api.py +++ b/test/test_emotion_api.py @@ -15,12 +15,13 @@ 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): @@ -28,18 +29,35 @@ class TestEmotionApi(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.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): """ @@ -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 diff --git a/test/test_text_recognise_emotion.py b/test/test_text_recognise_emotion.py new file mode 100644 index 0000000..778dfbe --- /dev/null +++ b/test/test_text_recognise_emotion.py @@ -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()