Skip to content

Commit 729d3f5

Browse files
committed
Add emotion text emotion api
1 parent d2887a1 commit 729d3f5

File tree

3 files changed

+165
-2
lines changed

3 files changed

+165
-2
lines changed

deepaffects/api_client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,14 @@ def deserialize(self, response, response_type):
217217
# handle file downloading
218218
# save response body into a tmp file and return the instance
219219
if response_type == "file":
220-
return self.__deserialize_file(response)
221-
220+
return self.__deserialize_file(response)
222221
# fetch data from response object
223222
try:
224223
data = json.loads(response.data)
225224
except ValueError:
226225
data = response.data
226+
if response_type == "json":
227+
return self.__deserialize_object(data)
227228

228229
return self.__deserialize(data, response_type)
229230

deepaffects/apis/emotion_api.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,113 @@ def sync_recognise_emotion_with_http_info(self, body, **kwargs):
251251
_preload_content=params.get('_preload_content', True),
252252
_request_timeout=params.get('_request_timeout'),
253253
collection_formats=collection_formats)
254+
def sync_text_recognise_emotion(self, body, **kwargs):
255+
"""
256+
Find emotion in text
257+
Extract emotion from text.
258+
This method makes a synchronous HTTP request by default. To make an
259+
asynchronous HTTP request, please define a `callback` function
260+
to be invoked when receiving the response.
261+
>>> def callback_function(response):
262+
>>> pprint(response)
263+
>>>
264+
>>> thread = api.sync_text_recognise_emotion(body, callback=callback_function)
265+
266+
:param callback function: The callback function
267+
for asynchronous request. (optional)
268+
:param Text: Text that needs to be featurized. (required)
269+
:return: list[EmotionScore]
270+
If the method is called asynchronously,
271+
returns the request thread.
272+
"""
273+
kwargs['_return_http_data_only'] = True
274+
if kwargs.get('callback'):
275+
return self.sync_text_recognise_emotion_with_http_info(body, **kwargs)
276+
else:
277+
(data) = self.sync_text_recognise_emotion_with_http_info(body, **kwargs)
278+
return data
279+
280+
def sync_text_recognise_emotion_with_http_info(self, body, **kwargs):
281+
"""
282+
Find emotion in text
283+
Extract emotion from text.
284+
This method makes a synchronous HTTP request by default. To make an
285+
asynchronous HTTP request, please define a `callback` function
286+
to be invoked when receiving the response.
287+
>>> def callback_function(response):
288+
>>> pprint(response)
289+
>>>
290+
>>> thread = api.sync_text_recognise_emotion_with_http_info(body, callback=callback_function)
291+
292+
:param callback function: The callback function
293+
for asynchronous request. (optional)
294+
:param Text body: Text that needs to be featurized. (required)
295+
:return: list[EmotionScore]
296+
If the method is called asynchronously,
297+
returns the request thread.
298+
"""
299+
300+
all_params = ['body']
301+
all_params.append('callback')
302+
all_params.append('_return_http_data_only')
303+
all_params.append('_preload_content')
304+
all_params.append('_request_timeout')
305+
306+
params = locals()
307+
for key, val in iteritems(params['kwargs']):
308+
if key not in all_params:
309+
raise TypeError(
310+
"Got an unexpected keyword argument '%s'"
311+
" to method sync_text_recognise_emotion" % key
312+
)
313+
params[key] = val
314+
del params['kwargs']
315+
# verify the required parameter 'body' is set
316+
if ('body' not in params) or (params['body'] is None):
317+
raise ValueError(
318+
"Missing the required parameter `body` when calling `sync_text_recognise_emotion`")
319+
320+
collection_formats = {}
321+
322+
resource_path = '/text/generic/api/latest/sync/text_recognise_emotion'.replace(
323+
'{format}', 'json')
324+
path_params = {}
325+
326+
query_params = []
327+
328+
header_params = {}
329+
330+
form_params = []
331+
local_var_files = {}
332+
333+
body_params = None
334+
if 'body' in params:
335+
body_params = params['body']
336+
# HTTP header `Accept`
337+
header_params['Accept'] = self.api_client.\
338+
select_header_accept(['application/json'])
339+
340+
# HTTP header `Content-Type`
341+
header_params['Content-Type'] = self.api_client.\
342+
select_header_content_type(['application/json'])
343+
344+
# Authentication setting
345+
auth_settings = ['UserSecurity']
346+
347+
return self.api_client.call_api(resource_path, 'POST',
348+
path_params,
349+
query_params,
350+
header_params,
351+
body=body_params,
352+
post_params=form_params,
353+
files=local_var_files,
354+
response_type='json',
355+
auth_settings=auth_settings,
356+
callback=params.get('callback'),
357+
_return_http_data_only=params.get(
358+
'_return_http_data_only'),
359+
_preload_content=params.get(
360+
'_preload_content', True),
361+
_request_timeout=params.get(
362+
'_request_timeout'),
363+
collection_formats=collection_formats)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# coding: utf-8
2+
3+
"""
4+
DeepAffects
5+
6+
OpenAPI spec version: v1
7+
"""
8+
9+
10+
from __future__ import absolute_import
11+
12+
import os
13+
import sys
14+
import unittest
15+
16+
import deepaffects
17+
from deepaffects import Audio
18+
from deepaffects.rest import ApiException
19+
from deepaffects.apis.diarize_api_v2 import DiarizeApiV2
20+
DIR = os.path.dirname(os.path.realpath(__file__))
21+
import uuid
22+
23+
24+
class TestTextEmootion(unittest.TestCase):
25+
""" Text Emotion unit test stubs """
26+
27+
def setUp(self):
28+
deepaffects.configuration.api_key['apikey'] = os.environ['DEEPAFFECTS_API_KEY']
29+
self.webhook_url = os.environ["DEEPAFFECTS_API_WEBHOOK"]
30+
self.api = deepaffects.apis.emotion_api.EmotionApi()
31+
self.request_id = str(uuid.uuid4())
32+
33+
def tearDown(self):
34+
pass
35+
36+
def test_async_diarize_audio(self):
37+
"""
38+
Test case for Text Emotion
39+
40+
Diarize text file
41+
"""
42+
43+
body = {
44+
"content": "You are so bad"
45+
}
46+
api_response = self.api.sync_text_recognise_emotion(body=body)
47+
print(api_response)
48+
assert api_response['response']['joy']> 0.8
49+
50+
51+
if __name__ == '__main__':
52+
unittest.main()

0 commit comments

Comments
 (0)