diff --git a/indicoio/__init__.py b/indicoio/__init__.py index 0921836..eaa98b1 100644 --- a/indicoio/__init__.py +++ b/indicoio/__init__.py @@ -1,5 +1,4 @@ from functools import partial -import indicoio.config as config JSON_HEADERS = {'Content-type': 'application/json', 'Accept': 'text/plain'} @@ -13,14 +12,10 @@ from indicoio.images.fer import fer from indicoio.images.features import facial_features from indicoio.images.features import image_features -apis = ['political', 'posneg', 'sentiment', 'language', 'fer', +apis = ['political', 'posneg', 'sentiment', 'language', 'fer', 'facial_features', 'image_features', 'text_tags'] apis = dict((api, globals().get(api)) for api in apis) -class Namespace(object): pass -local = Namespace() for api in apis: - globals()[api] = partial(apis[api], config.api_root) - globals()['batch_' + api] = partial(apis[api], config.api_root, batch=True) - setattr(local, api, partial(apis[api], config.local_api_root)) - setattr(local, 'batch_' + api, partial(apis[api], config.local_api_root, batch=True)) + globals()[api] = partial(apis[api]) + globals()['batch_' + api] = partial(apis[api], batch=True) diff --git a/indicoio/config.py b/indicoio/config.py index 150d0f9..fa38e85 100644 --- a/indicoio/config.py +++ b/indicoio/config.py @@ -1,2 +1,3 @@ -local_api_root = "http://localhost:9438/" -api_root = "http://apiv1.indico.io/" \ No newline at end of file +import os + +api_root = os.getenv("INDICO_PRIVATE_CLOUD_URL") or "http://apiv1.indico.io/" diff --git a/indicoio/images/features.py b/indicoio/images/features.py index 8ed7903..737309e 100644 --- a/indicoio/images/features.py +++ b/indicoio/images/features.py @@ -4,12 +4,13 @@ import requests import numpy as np from indicoio.utils import image_preprocess, api_handler +import indicoio.config as config -def facial_features(api_root, image, batch=False, auth=None, **kwargs): +def facial_features(image, url_root=config.api_root, 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. - Input should be in a list of list format, resizing will be attempted internally but for best + Input should be in a list of list format, resizing will be attempted internally but for best performance, images should be already sized at 48x48 pixels. Example usage: @@ -27,18 +28,18 @@ def facial_features(api_root, image, batch=False, auth=None, **kwargs): :type image: list of lists :rtype: List containing feature responses """ - return api_handler(image, api_root + "facialfeatures", batch=batch, auth=auth, **kwargs) + return api_handler(image, url_root + "facialfeatures", batch=batch, auth=auth, **kwargs) -def image_features(api_root, image, batch=False, auth=None, **kwargs): +def image_features(image, url_root=config.api_root, batch=False, auth=None, **kwargs): """ - Given an input image, returns a 2048 dimensional sparse feature vector explaining that 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. * Input can be either grayscale or rgb color and should either be a numpy array or nested list format. * Input data should be either uint8 0-255 range values or floating point between 0 and 1. * Large images (i.e. 1024x768+) are much bigger than needed, resizing will be done internally to 64x64 if needed. * For ideal performance, images should be square aspect ratio but non-square aspect ratios are supported as well. - + Example usage: .. code-block:: python @@ -60,4 +61,4 @@ def image_features(api_root, image, batch=False, auth=None, **kwargs): :rtype: List containing features """ image = image_preprocess(image, batch=batch) - return api_handler(image, api_root + "imagefeatures", batch=batch, auth=auth, **kwargs) + return api_handler(image, url_root + "imagefeatures", batch=batch, auth=auth, **kwargs) diff --git a/indicoio/images/fer.py b/indicoio/images/fer.py index 2248fe8..2b9527f 100644 --- a/indicoio/images/fer.py +++ b/indicoio/images/fer.py @@ -2,12 +2,14 @@ import json import requests import numpy as np -from indicoio.utils import api_handler -def fer(api_root, image, batch=False, auth=None, **kwargs): +from indicoio.utils import api_handler +import indicoio.config as config + +def fer(image, url_root=config.api_root, 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 + Input should be in a list of list format, resizing will be attempted internally but for best performance, images should be already sized at 48x48 pixels.. Example usage: @@ -19,13 +21,13 @@ def fer(api_root, image, batch=False, auth=None, **kwargs): >>> face = np.zeros((48,48)).tolist() >>> emotions = fer(face) >>> emotions - {u'Angry': 0.6340586827229989, u'Sad': 0.1764309536057839, - u'Neutral': 0.05582989039191157, u'Surprise': 0.0072685938275375344, + {u'Angry': 0.6340586827229989, u'Sad': 0.1764309536057839, + u'Neutral': 0.05582989039191157, u'Surprise': 0.0072685938275375344, u'Fear': 0.08523385724298838, u'Happy': 0.04117802220878012} :param image: The image to be analyzed. :type image: list of lists :rtype: Dictionary containing emotion probability pairs """ - - return api_handler(image, api_root + "fer", batch=batch, auth=auth, **kwargs) + + return api_handler(image, url_root + "fer", batch=batch, auth=auth, **kwargs) diff --git a/indicoio/text/lang.py b/indicoio/text/lang.py index 51bd339..8746c7c 100644 --- a/indicoio/text/lang.py +++ b/indicoio/text/lang.py @@ -1,8 +1,9 @@ from indicoio.utils import api_handler +import indicoio.config as config -def language(api_root, text, batch=False, auth=None, **kwargs): +def language(text, url_root=config.api_root, batch=False, auth=None, **kwargs): """ - Given input text, returns a probability distribution over 33 possible + Given input text, returns a probability distribution over 33 possible languages of what language the text was written in. Example usage: @@ -22,5 +23,5 @@ def language(api_root, text, batch=False, auth=None, **kwargs): :type text: str or unicode :rtype: Dictionary of language probability pairs """ - - return api_handler(text, api_root + "language", batch=batch, auth=auth, **kwargs) + + return api_handler(text, url_root + "language", batch=batch, auth=auth, **kwargs) diff --git a/indicoio/text/sentiment.py b/indicoio/text/sentiment.py index 387bb46..73e29af 100644 --- a/indicoio/text/sentiment.py +++ b/indicoio/text/sentiment.py @@ -1,7 +1,8 @@ from indicoio import JSON_HEADERS from indicoio.utils import api_handler +import indicoio.config as config -def political(api_root, text, batch=False, auth=None, **kwargs): +def political(text, url_root=config.api_root, batch=False, auth=None, **kwargs): """ Given input text, returns a probability distribution over the political alignment of the speaker. @@ -15,7 +16,7 @@ def political(api_root, text, batch=False, auth=None, **kwargs): Hopefully, driverless cars will chance economics from ownership to fee for service.' >>> affiliation = political(text) >>> affiliation - {u'Libertarian': 0.4923755446986322, u'Green': 0.2974443102818122, + {u'Libertarian': 0.4923755446986322, u'Green': 0.2974443102818122, u'Liberal': 0.13730032938784784, u'Conservative': 0.07287981563170784} >>> least_like = affiliation.keys()[np.argmin(affiliation.values())] >>> most_like = affiliation.keys()[np.argmax(affiliation.values())] @@ -27,9 +28,9 @@ def political(api_root, text, batch=False, auth=None, **kwargs): :rtype: Dictionary of party probability pairs """ - return api_handler(text, api_root + "political", batch=batch, auth=auth, **kwargs) + return api_handler(text, url_root + "political", batch=batch, auth=auth, **kwargs) -def posneg(api_root, text, batch=False, auth=None, **kwargs): +def posneg(text, url_root=config.api_root, 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. @@ -49,5 +50,5 @@ def posneg(api_root, text, batch=False, auth=None, **kwargs): :type text: str or unicode :rtype: Float """ - - return api_handler(text, api_root + "sentiment", batch=batch, auth=auth, **kwargs) + + return api_handler(text, url_root + "sentiment", batch=batch, auth=auth, **kwargs) diff --git a/indicoio/text/tagging.py b/indicoio/text/tagging.py index 6fabd7b..f820018 100644 --- a/indicoio/text/tagging.py +++ b/indicoio/text/tagging.py @@ -1,6 +1,7 @@ from indicoio.utils import api_handler +import indicoio.config as config -def text_tags(api_root, text, batch=False, auth=None, **kwargs): +def text_tags(text, url_root=config.api_root, batch=False, auth=None, **kwargs): """ Given input text, returns a probability distribution over 100 document categories @@ -21,5 +22,5 @@ def text_tags(api_root, text, batch=False, auth=None, **kwargs): :type text: str or unicode :rtype: Dictionary of class probability pairs """ - - return api_handler(text, api_root + "texttags", batch=batch, auth=auth, **kwargs) + + return api_handler(text, url_root + "texttags", batch=batch, auth=auth, **kwargs)