Passing in a root_url variable and pulling config from environment variables

This commit is contained in:
Anne Carlson
2015-02-27 16:32:32 -05:00
committed by Madison May
parent a1621e8531
commit 6b9251f4f5
7 changed files with 39 additions and 37 deletions
+3 -8
View File
@@ -1,5 +1,4 @@
from functools import partial from functools import partial
import indicoio.config as config
JSON_HEADERS = {'Content-type': 'application/json', 'Accept': 'text/plain'} 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 facial_features
from indicoio.images.features import image_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'] 'facial_features', 'image_features', 'text_tags']
apis = dict((api, globals().get(api)) for api in apis) apis = dict((api, globals().get(api)) for api in apis)
class Namespace(object): pass
local = Namespace()
for api in apis: for api in apis:
globals()[api] = partial(apis[api], config.api_root) globals()[api] = partial(apis[api])
globals()['batch_' + api] = partial(apis[api], config.api_root, batch=True) globals()['batch_' + api] = partial(apis[api], 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))
+3 -2
View File
@@ -1,2 +1,3 @@
local_api_root = "http://localhost:9438/" import os
api_root = "http://apiv1.indico.io/"
api_root = os.getenv("INDICO_PRIVATE_CLOUD_URL") or "http://apiv1.indico.io/"
+8 -7
View File
@@ -4,12 +4,13 @@ import requests
import numpy as np import numpy as np
from indicoio.utils import image_preprocess, api_handler 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. 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. 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. performance, images should be already sized at 48x48 pixels.
Example usage: Example usage:
@@ -27,18 +28,18 @@ def facial_features(api_root, image, batch=False, auth=None, **kwargs):
:type image: list of lists :type image: list of lists
:rtype: List containing feature responses :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. 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 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. * 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. * 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. * For ideal performance, images should be square aspect ratio but non-square aspect ratios are supported as well.
Example usage: Example usage:
.. code-block:: python .. code-block:: python
@@ -60,4 +61,4 @@ def image_features(api_root, image, batch=False, auth=None, **kwargs):
:rtype: List containing features :rtype: List containing features
""" """
image = image_preprocess(image, batch=batch) 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)
+9 -7
View File
@@ -2,12 +2,14 @@ import json
import requests import requests
import numpy as np 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. 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.. performance, images should be already sized at 48x48 pixels..
Example usage: Example usage:
@@ -19,13 +21,13 @@ def fer(api_root, image, batch=False, auth=None, **kwargs):
>>> face = np.zeros((48,48)).tolist() >>> face = np.zeros((48,48)).tolist()
>>> emotions = fer(face) >>> emotions = fer(face)
>>> emotions >>> emotions
{u'Angry': 0.6340586827229989, u'Sad': 0.1764309536057839, {u'Angry': 0.6340586827229989, u'Sad': 0.1764309536057839,
u'Neutral': 0.05582989039191157, u'Surprise': 0.0072685938275375344, u'Neutral': 0.05582989039191157, u'Surprise': 0.0072685938275375344,
u'Fear': 0.08523385724298838, u'Happy': 0.04117802220878012} u'Fear': 0.08523385724298838, u'Happy': 0.04117802220878012}
:param image: The image to be analyzed. :param image: The image to be analyzed.
:type image: list of lists :type image: list of lists
:rtype: Dictionary containing emotion probability pairs :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)
+5 -4
View File
@@ -1,8 +1,9 @@
from indicoio.utils import api_handler 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. languages of what language the text was written in.
Example usage: Example usage:
@@ -22,5 +23,5 @@ def language(api_root, text, batch=False, auth=None, **kwargs):
:type text: str or unicode :type text: str or unicode
:rtype: Dictionary of language probability pairs :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)
+7 -6
View File
@@ -1,7 +1,8 @@
from indicoio import JSON_HEADERS from indicoio import JSON_HEADERS
from indicoio.utils import api_handler 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. 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.' Hopefully, driverless cars will chance economics from ownership to fee for service.'
>>> affiliation = political(text) >>> affiliation = political(text)
>>> affiliation >>> 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} u'Liberal': 0.13730032938784784, u'Conservative': 0.07287981563170784}
>>> least_like = affiliation.keys()[np.argmin(affiliation.values())] >>> least_like = affiliation.keys()[np.argmin(affiliation.values())]
>>> most_like = affiliation.keys()[np.argmax(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 :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. 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.
@@ -49,5 +50,5 @@ def posneg(api_root, text, batch=False, auth=None, **kwargs):
:type text: str or unicode :type text: str or unicode
:rtype: Float :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)
+4 -3
View File
@@ -1,6 +1,7 @@
from indicoio.utils import api_handler 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 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 :type text: str or unicode
:rtype: Dictionary of class probability pairs :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)