Add local api support, fix test case

This commit is contained in:
Madison May
2014-09-24 19:51:31 -04:00
parent 6898e1ea07
commit a3d4d85412
8 changed files with 46 additions and 15 deletions
+11 -1
View File
@@ -1,3 +1,6 @@
from functools import partial
from utils import config
JSON_HEADERS = {'Content-type': 'application/json', 'Accept': 'text/plain'}
Version, version, __version__, VERSION = ('0.4.4',) * 4
@@ -7,4 +10,11 @@ from text.sentiment import posneg as sentiment
from text.lang import language
from images.fer import fer
from images.features import facial_features
from images.features import image_features
from images.features import image_features
political = partial(political, config.api_root)
sentiment = partial(sentiment, config.api_root)
language = partial(language, config.api_root)
fer = partial(fer, config.api_root)
facial_features = partial(facial_features, config.api_root)
image_features = partial(image_features, config.api_root)
+4 -4
View File
@@ -6,7 +6,7 @@ import numpy as np
from indicoio import JSON_HEADERS
from indicoio.utils import image_preprocess
def facial_features(image):
def facial_features(api_root, image):
"""
Given an grayscale input image of a face, returns a 48 dimensional feature vector explaining that face.
Useful as a form of feature engineering for face oriented tasks.
@@ -30,14 +30,14 @@ def facial_features(image):
"""
data_dict = json.dumps({"face": image})
response = requests.post("http://api.indico.io/facialfeatures", data=data_dict, headers=JSON_HEADERS)
response = requests.post(api_root + "facialfeatures", data=data_dict, headers=JSON_HEADERS)
response_dict = response.json()
if 'response' not in response_dict:
raise ValueError(response_dict.values()[0])
else:
return response_dict['response']
def image_features(image):
def image_features(api_root, image):
"""
Given an input image, returns a 2048 dimensional sparse feature vector explaining that image.
Useful as a form of feature engineering for image oriented tasks.
@@ -69,7 +69,7 @@ def image_features(image):
"""
image = image_preprocess(image)
data_dict = json.dumps({"image": image})
response = requests.post("http://api.indico.io/imagefeatures", data=data_dict, headers=JSON_HEADERS)
response = requests.post(api_root + "imagefeatures", data=data_dict, headers=JSON_HEADERS)
response_dict = response.json()
if 'Features' not in response_dict:
raise ValueError(response_dict.values()[0])
+2 -2
View File
@@ -4,7 +4,7 @@ import requests
import numpy as np
from indicoio import JSON_HEADERS
def fer(image):
def fer(api_root, image):
"""
Given a grayscale input image of a face, returns a probability distribution over emotional state.
Input should be in a list of list format, resizing will be attempted internally but for best
@@ -29,7 +29,7 @@ def fer(image):
"""
data_dict = json.dumps({"face": image})
response = requests.post("http://api.indico.io/fer", data=data_dict, headers=JSON_HEADERS)
response = requests.post(api_root + "fer", data=data_dict, headers=JSON_HEADERS)
response_dict = response.json()
if len(response_dict) < 2:
raise ValueError(response_dict.values()[0])
+20
View File
@@ -0,0 +1,20 @@
from functools import partial
from indicoio.utils import config
JSON_HEADERS = {'Content-type': 'application/json', 'Accept': 'text/plain'}
Version, version, __version__, VERSION = ('0.4.3',) * 4
from indicoio.text.sentiment import political, posneg
from indicoio.text.sentiment import posneg as sentiment
from indicoio.text.lang import language
from indicoio.images.fer import fer
from indicoio.images.features import facial_features
from indicoio.images.features import image_features
political = partial(political, config.local_api_root)
sentiment = partial(sentiment, config.local_api_root)
language = partial(language, config.local_api_root)
fer = partial(fer, config.local_api_root)
facial_features = partial(facial_features, config.local_api_root)
image_features = partial(image_features, config.local_api_root)
+2 -2
View File
@@ -3,7 +3,7 @@ import json
from indicoio import JSON_HEADERS
def language(text):
def language(api_root, text):
"""
Given input text, returns a probability distribution over 33 possible
languages of what language the text was written in.
@@ -27,7 +27,7 @@ def language(text):
"""
data_dict = json.dumps({'text': text})
response = requests.post("http://api.indico.io/language", data=data_dict, headers=JSON_HEADERS)
response = requests.post(api_root + "language", data=data_dict, headers=JSON_HEADERS)
response_dict = response.json()
if len(response_dict) < 2:
raise ValueError(response_dict.values()[0])
+4 -4
View File
@@ -4,7 +4,7 @@ import json
from indicoio import JSON_HEADERS
from indicoio.utils import normalize
def political(text):
def political(api_root, text):
"""
Given input text, returns a probability distribution over the political alignment of the speaker.
@@ -31,14 +31,14 @@ def political(text):
"""
data_dict = json.dumps({'text': text})
response = requests.post("http://api.indico.io/political", data=data_dict, headers=JSON_HEADERS)
response = requests.post(api_root + "political", data=data_dict, headers=JSON_HEADERS)
response_dict = response.json()
if len(response_dict) < 2:
raise ValueError(response_dict.values()[0])
else:
return response_dict
def posneg(text):
def posneg(api_root, 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.
@@ -60,7 +60,7 @@ def posneg(text):
"""
data_dict = json.dumps({'text': text})
response = requests.post("http://api.indico.io/sentiment", data=data_dict, headers=JSON_HEADERS)
response = requests.post(api_root + "sentiment", data=data_dict, headers=JSON_HEADERS)
response_dict = response.json()
if 'Sentiment' not in response_dict:
raise ValueError(response_dict.values()[0])
+2
View File
@@ -0,0 +1,2 @@
local_api_root = "http://localhost:9438/"
api_root = "http://www.indico.io/api/"
+1 -2
View File
@@ -20,8 +20,7 @@ class FullAPIRun(unittest.TestCase):
test_string = "Worst song ever."
response = sentiment(test_string)
self.assertTrue(isinstance(response, dict))
self.assertEqual(posneg_set, set(response.keys()))
self.assertTrue(isinstance(response, float))
def test_good_fer(self):
fer_set = set(['Angry', 'Sad', 'Neutral', 'Surprise', 'Fear', 'Happy'])