Cleaner api handler interface + extended tests

This commit is contained in:
Madison May
2015-02-27 16:33:22 -05:00
parent 57f91a138b
commit 110abaf7a6
16 changed files with 258 additions and 93 deletions
+1 -1
View File
@@ -2,7 +2,7 @@ from functools import partial
JSON_HEADERS = {'Content-type': 'application/json', 'Accept': 'text/plain'}
Version, version, __version__, VERSION = ('0.4.15',) * 4
Version, version, __version__, VERSION = ('0.5.0',) * 4
from indicoio.text.sentiment import political, posneg
from indicoio.text.sentiment import posneg as sentiment
+46 -21
View File
@@ -1,29 +1,54 @@
import os
from StringIO import StringIO
import ConfigParser
settings = ConfigParser.ConfigParser()
class Settings(ConfigParser.ConfigParser):
settings_paths = [
def __init__(self, *args, **kwargs):
"""
files: filepaths or open file objects
"""
self.files = kwargs.pop('files')
ConfigParser.ConfigParser.__init__(self, *args, **kwargs)
for fd in self.files:
try:
self.readfp(fd)
except AttributeError:
self.read(fd)
self.auth_settings = self.get_section('auth')
self.private_cloud_settings = self.get_section('private_cloud')
def get_section(self, section):
"""
Retrieve a ConfigParser section as a dictionary, default to {}
"""
try:
return dict(self.items(section))
except ConfigParser.NoSectionError:
return {}
def cloud(self):
return (
os.getenv("INDICO_CLOUD") or
self.private_cloud_settings.get('cloud') or
None
)
def auth(self):
return (
os.getenv("INDICO_USERNAME") or self.auth_settings.get('username'),
os.getenv("INDICO_PASSWORD") or self.auth_settings.get('password')
)
settings = Settings(files=[
os.path.expanduser("~/.indicorc"),
os.path.join(os.getcwd(), '.indicorc')
]
])
settings.read(settings_paths)
def get_section(parser, section):
try:
return dict(parser.items(section))
except ConfigParser.NoSectionError:
return {}
auth_settings = get_section(settings, 'auth')
private_cloud_settings = get_section(settings, 'private_cloud')
api_root = (
os.getenv("INDICO_PRIVATE_CLOUD_URL") or
private_cloud_settings.get('url_root') or
"http://apiv1.indico.io/"
)
auth = (auth_settings.get('username'), auth_settings.get('password'))
auth = settings.auth()
cloud = settings.cloud()
public_api_host = 'apiv1.indico.io'
+4 -4
View File
@@ -6,7 +6,7 @@ import numpy as np
from indicoio.utils import image_preprocess, api_handler
import indicoio.config as config
def facial_features(image, url_root=config.api_root, batch=False, auth=None, **kwargs):
def facial_features(image, cloud=config.cloud, batch=False, auth=None, **kwargs):
"""
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.
@@ -28,9 +28,9 @@ def facial_features(image, url_root=config.api_root, batch=False, auth=None, **k
:type image: list of lists
:rtype: List containing feature responses
"""
return api_handler(image, url_root + "facialfeatures", batch=batch, auth=auth, **kwargs)
return api_handler(image, cloud=cloud, api="facialfeatures", batch=batch, auth=auth, **kwargs)
def image_features(image, url_root=config.api_root, batch=False, auth=None, **kwargs):
def image_features(image, cloud=config.cloud, batch=False, auth=None, **kwargs):
"""
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.
@@ -61,4 +61,4 @@ def image_features(image, url_root=config.api_root, batch=False, auth=None, **kw
:rtype: List containing features
"""
image = image_preprocess(image, batch=batch)
return api_handler(image, url_root + "imagefeatures", batch=batch, auth=auth, **kwargs)
return api_handler(image, cloud=cloud, api="imagefeatures", batch=batch, auth=auth, **kwargs)
+2 -2
View File
@@ -6,7 +6,7 @@ import numpy as np
from indicoio.utils import api_handler
import indicoio.config as config
def fer(image, url_root=config.api_root, batch=False, auth=None, **kwargs):
def fer(image, cloud=config.cloud, batch=False, auth=None, **kwargs):
"""
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
@@ -30,4 +30,4 @@ def fer(image, url_root=config.api_root, batch=False, auth=None, **kwargs):
:rtype: Dictionary containing emotion probability pairs
"""
return api_handler(image, url_root + "fer", batch=batch, auth=auth, **kwargs)
return api_handler(image, cloud=cloud, api="fer", batch=batch, auth=auth, **kwargs)
+2 -2
View File
@@ -1,7 +1,7 @@
from indicoio.utils import api_handler
import indicoio.config as config
def language(text, url_root=config.api_root, batch=False, auth=None, **kwargs):
def language(text, cloud=config.cloud, batch=False, auth=None, **kwargs):
"""
Given input text, returns a probability distribution over 33 possible
languages of what language the text was written in.
@@ -24,4 +24,4 @@ def language(text, url_root=config.api_root, batch=False, auth=None, **kwargs):
:rtype: Dictionary of language probability pairs
"""
return api_handler(text, url_root + "language", batch=batch, auth=auth, **kwargs)
return api_handler(text, cloud=cloud, api="language", batch=batch, auth=auth, **kwargs)
+4 -4
View File
@@ -2,7 +2,7 @@ from indicoio import JSON_HEADERS
from indicoio.utils import api_handler
import indicoio.config as config
def political(text, url_root=config.api_root, batch=False, auth=None, **kwargs):
def political(text, cloud=config.cloud, batch=False, auth=None, **kwargs):
"""
Given input text, returns a probability distribution over the political alignment of the speaker.
@@ -28,9 +28,9 @@ def political(text, url_root=config.api_root, batch=False, auth=None, **kwargs):
:rtype: Dictionary of party probability pairs
"""
return api_handler(text, url_root + "political", batch=batch, auth=auth, **kwargs)
return api_handler(text, cloud=cloud, api="political", batch=batch, auth=auth, **kwargs)
def posneg(text, url_root=config.api_root, batch=False, auth=None, **kwargs):
def posneg(text, cloud=config.cloud, batch=False, auth=None, **kwargs):
"""
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.
@@ -51,4 +51,4 @@ def posneg(text, url_root=config.api_root, batch=False, auth=None, **kwargs):
:rtype: Float
"""
return api_handler(text, url_root + "sentiment", batch=batch, auth=auth, **kwargs)
return api_handler(text, cloud=cloud, api="sentiment", batch=batch, auth=auth, **kwargs)
+2 -2
View File
@@ -1,7 +1,7 @@
from indicoio.utils import api_handler
import indicoio.config as config
def text_tags(text, url_root=config.api_root, batch=False, auth=None, **kwargs):
def text_tags(text, cloud=config.cloud, batch=False, auth=None, **kwargs):
"""
Given input text, returns a probability distribution over 100 document categories
@@ -23,4 +23,4 @@ def text_tags(text, url_root=config.api_root, batch=False, auth=None, **kwargs):
:rtype: Dictionary of class probability pairs
"""
return api_handler(text, url_root + "texttags", batch=batch, auth=auth, **kwargs)
return api_handler(text, cloud=cloud, api="texttags", batch=batch, auth=auth, **kwargs)
+13 -15
View File
@@ -4,26 +4,22 @@ import numpy as np
from skimage.transform import resize
from indicoio import JSON_HEADERS
from indicoio import config
def auth_query():
email = os.environ.get("INDICO_EMAIL")
password = os.environ.get("INDICO_PASSWORD")
# store settings
if not email:
email = raw_input("Email: ")
os.environ["INDICO_EMAIL"] = email
if not password:
password = getpass.getpass("Password: ")
os.environ["INDICO_PASSWORD"] = password
return (email, password)
def api_handler(arg, url, batch=False, auth=None, **kwargs):
def api_handler(arg, cloud, api, batch=False, auth=None, **kwargs):
data = {'data': arg}
data.update(**kwargs)
json_data = json.dumps(data)
if cloud:
host = "%s.indico.domains"
else:
# default to indico public cloud
host = config.public_api_host
url = "http://%s/%s" % (host, api)
if batch:
url += "/batch"
@@ -34,6 +30,7 @@ def api_handler(arg, url, batch=False, auth=None, **kwargs):
raise ValueError(error)
return results
class TypeCheck(object):
"""
Decorator that performs a typecheck on the input to a function
@@ -118,6 +115,7 @@ def normalize(array, distribution=1, norm_range=(0, 1), **kwargs):
return dict(zip(keys, norm_array))
return norm_array
def image_preprocess(image, batch=False):
"""
Takes an image and prepares it for sending to the api including