Merge pull request #55 from IndicoDataSolutions/Chris/multi-api

ADD: Endpoint for multi api requests
This commit is contained in:
Madison May
2015-06-09 15:55:36 -04:00
10 changed files with 201 additions and 25 deletions
+5 -7
View File
@@ -8,7 +8,7 @@ from indicoio import config
B64_PATTERN = re.compile("^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)")
def api_handler(arg, cloud, api, batch=False, api_key=None, **kwargs):
def api_handler(arg, cloud, api, url_params = {"batch":False, "api_key":None}, **kwargs):
data = {'data': arg}
data.update(**kwargs)
json_data = json.dumps(data)
@@ -21,13 +21,11 @@ def api_handler(arg, cloud, api, batch=False, api_key=None, **kwargs):
# default to indico public cloud
host = config.PUBLIC_API_HOST
if not api_key:
api_key = config.api_key
url = config.url_protocol + "//%s/%s" % (host, api)
url = url + "/batch" if batch else url
url += "?key=%s" % api_key
url = url + "/batch" if url_params.get("batch", False) else url
url += "?key=%s" % (url_params.get("api_key", None) or config.api_key)
if "apis" in url_params:
url += "&apis=%s" % ",".join(url_params["apis"])
response = requests.post(url, data=json_data, headers=JSON_HEADERS)
if response.status_code == 503 and cloud != None:
+117
View File
@@ -0,0 +1,117 @@
from indicoio.config import TEXT_APIS, IMAGE_APIS, API_NAMES
from indicoio.utils import api_handler
CLIENT_SERVER_MAP = dict((api, api.strip().replace("_", "").lower()) for api in API_NAMES)
SERVER_CLIENT_MAP = dict((v, k) for k, v in CLIENT_SERVER_MAP.iteritems())
def multi(data, type, apis, available, batch=False, **kwargs):
"""
Helper to make multi requests of different types.
:param data: data to be sent in JSON.
:param type: String type of API request
:param apis: List of apis to use.
:param apis: List of apis available for use.
:type data: str or image
:type type: str or unicode
:type apis: list of str
:type available: list of str
:rtype: Dictionary of api responses
"""
# Client side api name checking - strictly only accept func name api
invalid_apis = [api for api in apis if api not in available]
if invalid_apis:
raise ValueError("%s are not valid %s APIs. Please reference the available APIs below:\n%s"
% (", ".join(invalid_apis), type, ", ".join(available))
)
# Convert client api names to server names before sending request
apis = map(CLIENT_SERVER_MAP.get, apis)
result = api_handler(data, url_params = {"apis":apis, "batch":batch}, **kwargs)
return handle_response(result)
def handle_response(result):
try:
# Parse out the results to a dicionary of api: result
return dict((SERVER_CLIENT_MAP[api], parsed_response(res))
for api, res in result.iteritems())
except KeyError:
for api in result:
if "error" in result[api]:
raise ValueError(result[api]["error"])
raise Exception("Sorry, %s API returned an unexpected response:\n%s" % (api, result[api]))
def predict_text(input_text, apis=TEXT_APIS, cloud=None, batch=False, api_key=None, **kwargs):
"""
Given input text, returns the results of specified text apis. Possible apis
include: [ 'text_tags', 'political', 'sentiment', 'language' ]
Example usage:
.. code-block:: python
>>> import indicoio
>>> text = 'Monday: Delightful with mostly sunny skies. Highs in the low 70s.'
>>> results = indicoio.text(data = text, apis = ["language", "sentiment"])
>>> language_results = results["langauge"]
>>> sentiment_results = results["sentiment"]
:param text: The text to be analyzed.
:param apis: List of apis to use.
:type text: str or unicode
:type apis: list of str
:rtype: Dictionary of api responses
"""
return multi(
api="apis",
data=input_text,
type="text",
available = TEXT_APIS,
cloud=cloud,
batch=batch,
api_key=api_key,
apis=apis,
**kwargs)
def predict_image(image, apis=IMAGE_APIS, cloud=None, batch=False, api_key=None, **kwargs):
"""
Given input image, returns the results of specified image apis. Possible apis
include: ['fer', 'facial_features', 'image_features']
Example usage:
.. code-block:: python
>>> import indicoio
>>> import numpy as np
>>> face = np.zeros((48,48)).tolist()
>>> results = indicoio.image(image = face, apis = ["fer", "facial_features"])
>>> fer = results["fer"]
>>> facial_features = results["facial_features"]
:param text: The text to be analyzed.
:param apis: List of apis to use.
:type text: str or unicode
:type apis: list of str
:rtype: Dictionary of api responses
"""
return multi(
api="apis",
data=image,
type="image",
available=IMAGE_APIS,
cloud=cloud,
batch=batch,
api_key=api_key,
apis=apis,
**kwargs)
def parsed_response(response):
result = response.get('results') or response.get('error', False)
if result:
return result
raise KeyError