mirror of
https://github.com/wassname/IndicoIo-python.git
synced 2026-08-11 11:12:45 +08:00
Added api error handeling and updated docs
This commit is contained in:
@@ -31,8 +31,11 @@ def facial_features(image):
|
|||||||
|
|
||||||
data_dict = json.dumps({"face": image})
|
data_dict = json.dumps({"face": image})
|
||||||
response = requests.post("http://api.indico.io/facialfeatures", data=data_dict, headers=JSON_HEADERS)
|
response = requests.post("http://api.indico.io/facialfeatures", data=data_dict, headers=JSON_HEADERS)
|
||||||
response_dict = json.loads(response.content)
|
response_dict = response.json()
|
||||||
return response_dict['response']
|
if 'response' not in response_dict:
|
||||||
|
raise ValueError(response_dict.values()[0])
|
||||||
|
else:
|
||||||
|
return response_dict['response']
|
||||||
|
|
||||||
def image_features(image):
|
def image_features(image):
|
||||||
"""
|
"""
|
||||||
@@ -67,5 +70,8 @@ def image_features(image):
|
|||||||
image = image_preprocess(image)
|
image = image_preprocess(image)
|
||||||
data_dict = json.dumps({"image": image})
|
data_dict = json.dumps({"image": image})
|
||||||
response = requests.post("http://api.indico.io/imagefeatures", data=data_dict, headers=JSON_HEADERS)
|
response = requests.post("http://api.indico.io/imagefeatures", data=data_dict, headers=JSON_HEADERS)
|
||||||
response_dict = json.loads(response.content)
|
response_dict = response.json()
|
||||||
return response_dict['Features']
|
if 'Features' not in response_dict:
|
||||||
|
raise ValueError(response_dict.values()[0])
|
||||||
|
else:
|
||||||
|
return response_dict['Features']
|
||||||
@@ -30,4 +30,8 @@ def fer(image):
|
|||||||
|
|
||||||
data_dict = json.dumps({"face": image})
|
data_dict = json.dumps({"face": image})
|
||||||
response = requests.post("http://api.indico.io/fer", data=data_dict, headers=JSON_HEADERS)
|
response = requests.post("http://api.indico.io/fer", data=data_dict, headers=JSON_HEADERS)
|
||||||
return json.loads(response.content)
|
response_dict = response.json()
|
||||||
|
if len(response_dict) < 2:
|
||||||
|
raise ValueError(response_dict.values()[0])
|
||||||
|
else:
|
||||||
|
return response_dict
|
||||||
|
|||||||
@@ -28,4 +28,8 @@ def language(text):
|
|||||||
|
|
||||||
data_dict = json.dumps({'text': text})
|
data_dict = json.dumps({'text': text})
|
||||||
response = requests.post("http://api.indico.io/language", data=data_dict, headers=JSON_HEADERS)
|
response = requests.post("http://api.indico.io/language", data=data_dict, headers=JSON_HEADERS)
|
||||||
return json.loads(response.content)
|
response_dict = response.json()
|
||||||
|
if len(response_dict) < 2:
|
||||||
|
raise ValueError(response_dict.values()[0])
|
||||||
|
else:
|
||||||
|
return response_dict
|
||||||
@@ -32,13 +32,17 @@ def political(text):
|
|||||||
|
|
||||||
data_dict = json.dumps({'text': text})
|
data_dict = json.dumps({'text': text})
|
||||||
response = requests.post("http://api.indico.io/political", data=data_dict, headers=JSON_HEADERS)
|
response = requests.post("http://api.indico.io/political", data=data_dict, headers=JSON_HEADERS)
|
||||||
return json.loads(response.content)
|
response_dict = response.json()
|
||||||
|
if len(response_dict) < 2:
|
||||||
|
raise ValueError(response_dict.values()[0])
|
||||||
|
else:
|
||||||
|
return response_dict
|
||||||
|
|
||||||
def posneg(text):
|
def posneg(text):
|
||||||
"""
|
"""
|
||||||
Given input text, returns a scalar estimate of the sentiment of that text.
|
Given input text, returns a scalar estimate of the sentiment of that text.
|
||||||
Values are roughly in the range 0 to 1 with 0.5 indicating neutral sentiment.
|
Values are roughly in the range 0 to 1 with 0.5 indicating neutral sentiment.
|
||||||
Likewise, 0 would suggest very negative sentiment and 1 would suggest very positive sentiment.
|
For reference, 0 suggests very negative sentiment and 1 suggests very positive sentiment.
|
||||||
|
|
||||||
Example usage:
|
Example usage:
|
||||||
|
|
||||||
@@ -48,13 +52,17 @@ def posneg(text):
|
|||||||
>>> text = 'Thanks everyone for the birthday wishes!! It was a crazy few days ><'
|
>>> text = 'Thanks everyone for the birthday wishes!! It was a crazy few days ><'
|
||||||
>>> sentiment = sentiment(text)
|
>>> sentiment = sentiment(text)
|
||||||
>>> sentiment
|
>>> sentiment
|
||||||
{u'Sentiment': 0.6946439339979863}
|
0.6946439339979863
|
||||||
|
|
||||||
:param text: The text to be analyzed.
|
:param text: The text to be analyzed.
|
||||||
:type text: str or unicode
|
:type text: str or unicode
|
||||||
:rtype: Dictionary containing Sentiment key with a float value
|
:rtype: Float
|
||||||
"""
|
"""
|
||||||
|
|
||||||
data_dict = json.dumps({'text': text})
|
data_dict = json.dumps({'text': text})
|
||||||
response = requests.post("http://api.indico.io/sentiment", data=data_dict, headers=JSON_HEADERS)
|
response = requests.post("http://api.indico.io/sentiment", data=data_dict, headers=JSON_HEADERS)
|
||||||
return json.loads(response.content)
|
response_dict = response.json()
|
||||||
|
if 'Sentiment' not in response_dict:
|
||||||
|
raise ValueError(response_dict.values()[0])
|
||||||
|
else:
|
||||||
|
return response_dict['Sentiment']
|
||||||
|
|||||||
@@ -93,6 +93,8 @@ def image_preprocess(image):
|
|||||||
"""
|
"""
|
||||||
if isinstance(image,list):
|
if isinstance(image,list):
|
||||||
image = np.asarray(image)
|
image = np.asarray(image)
|
||||||
|
if type(image).__module__ != np.__name__:
|
||||||
|
raise ValueError('Image was not of type numpy.ndarray or list.')
|
||||||
if image.max() > 1:
|
if image.max() > 1:
|
||||||
image = image/255.
|
image = image/255.
|
||||||
if len(image.shape) == 2:
|
if len(image.shape) == 2:
|
||||||
|
|||||||
Reference in New Issue
Block a user