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 2 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)
52 changes: 52 additions & 0 deletions test/test_text_recoginse_emotion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# coding: utf-8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in file name


"""
DeepAffects

OpenAPI spec version: v1
"""


from __future__ import absolute_import

import os
import sys
import unittest

import deepaffects
from deepaffects import Audio
from deepaffects.rest import ApiException
from deepaffects.apis.diarize_api_v2 import DiarizeApiV2
DIR = os.path.dirname(os.path.realpath(__file__))
import uuid


class TestTextEmootion(unittest.TestCase):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in class name

""" Text Emotion unit test stubs """

def setUp(self):
deepaffects.configuration.api_key['apikey'] = os.environ['DEEPAFFECTS_API_KEY']
self.webhook_url = os.environ["DEEPAFFECTS_API_WEBHOOK"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use consistent quotes

self.api = deepaffects.apis.emotion_api.EmotionApi()
self.request_id = str(uuid.uuid4())

def tearDown(self):
pass

def test_async_diarize_audio(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you aren't testing diarize audio here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add more tests to test the cases where the request is malformed. Also check for all the expected keys in output

"""
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


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