From 81499083288495841dbf12eb803bd4d64ed5f4ce Mon Sep 17 00:00:00 2001 From: Madison May Date: Fri, 28 Nov 2014 12:52:04 -0500 Subject: [PATCH] Email + password auth from client --- indicoio/images/features.py | 8 ++++---- indicoio/images/fer.py | 4 ++-- indicoio/text/lang.py | 4 ++-- indicoio/text/sentiment.py | 8 ++++---- indicoio/text/tagging.py | 4 ++-- indicoio/utils/__init__.py | 23 ++++++++++++++++++++--- 6 files changed, 34 insertions(+), 17 deletions(-) diff --git a/indicoio/images/features.py b/indicoio/images/features.py index aca6ab7..776ef8e 100644 --- a/indicoio/images/features.py +++ b/indicoio/images/features.py @@ -5,7 +5,7 @@ import numpy as np from indicoio.utils import image_preprocess, api_handler -def facial_features(api_root, image, batch=False): +def facial_features(api_root, image, batch=False, auth=None): """ 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. @@ -27,9 +27,9 @@ def facial_features(api_root, image, batch=False): :type image: list of lists :rtype: List containing feature responses """ - return api_handler(image, api_root + "facialfeatures", batch=batch) + return api_handler(image, api_root + "facialfeatures", batch=batch, auth=auth) -def image_features(api_root, image, batch=False): +def image_features(api_root, image, batch=False, auth=None): """ 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. @@ -60,4 +60,4 @@ def image_features(api_root, image, batch=False): :rtype: List containing features """ image = image_preprocess(image) - return api_handler(image, api_root + "imagefeatures", batch=batch) + return api_handler(image, api_root + "imagefeatures", batch=batch, auth=auth) diff --git a/indicoio/images/fer.py b/indicoio/images/fer.py index dace6a9..ecbddd6 100644 --- a/indicoio/images/fer.py +++ b/indicoio/images/fer.py @@ -4,7 +4,7 @@ import requests import numpy as np from indicoio.utils import api_handler -def fer(api_root, image, batch=False): +def fer(api_root, image, batch=False, auth=None): """ 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 @@ -28,4 +28,4 @@ def fer(api_root, image, batch=False): :rtype: Dictionary containing emotion probability pairs """ - return api_handler(image, api_root + "fer", batch=batch) + return api_handler(image, api_root + "fer", batch=batch, auth=auth) diff --git a/indicoio/text/lang.py b/indicoio/text/lang.py index af174dd..2e0e65f 100644 --- a/indicoio/text/lang.py +++ b/indicoio/text/lang.py @@ -1,6 +1,6 @@ from indicoio.utils import api_handler -def language(api_root, text, batch=False): +def language(api_root, text, batch=False, auth=None): """ Given input text, returns a probability distribution over 33 possible languages of what language the text was written in. @@ -23,4 +23,4 @@ def language(api_root, text, batch=False): :rtype: Dictionary of language probability pairs """ - return api_handler(text, api_root + "language", batch=batch) + return api_handler(text, api_root + "language", batch=batch, auth=auth) diff --git a/indicoio/text/sentiment.py b/indicoio/text/sentiment.py index 55f2d07..acb88ac 100644 --- a/indicoio/text/sentiment.py +++ b/indicoio/text/sentiment.py @@ -1,7 +1,7 @@ from indicoio import JSON_HEADERS from indicoio.utils import api_handler -def political(api_root, text, batch=False): +def political(api_root, text, batch=False, auth=None): """ Given input text, returns a probability distribution over the political alignment of the speaker. @@ -27,9 +27,9 @@ def political(api_root, text, batch=False): :rtype: Dictionary of party probability pairs """ - return api_handler(text, api_root + "political", batch=batch) + return api_handler(text, api_root + "political", batch=batch, auth=auth) -def posneg(api_root, text, batch=False): +def posneg(api_root, text, batch=False, auth=None): """ 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. @@ -50,4 +50,4 @@ def posneg(api_root, text, batch=False): :rtype: Float """ - return api_handler(text, api_root + "sentiment", batch=batch) + return api_handler(text, api_root + "sentiment", batch=batch, auth=auth) diff --git a/indicoio/text/tagging.py b/indicoio/text/tagging.py index b26738a..a2d8ad6 100644 --- a/indicoio/text/tagging.py +++ b/indicoio/text/tagging.py @@ -1,6 +1,6 @@ from indicoio.utils import api_handler -def text_tags(api_root, text, batch=False): +def text_tags(api_root, text, batch=False, auth=None): """ Given input text, returns a probability distribution over 100 document categories @@ -22,4 +22,4 @@ def text_tags(api_root, text, batch=False): :rtype: Dictionary of class probability pairs """ - return api_handler(text, api_root + "texttags", batch=batch) + return api_handler(text, api_root + "texttags", batch=batch, auth=None) diff --git a/indicoio/utils/__init__.py b/indicoio/utils/__init__.py index 3cd15de..c96296c 100644 --- a/indicoio/utils/__init__.py +++ b/indicoio/utils/__init__.py @@ -1,14 +1,31 @@ -import inspect, json, requests +import inspect, json, getpass, os +import requests import numpy as np from skimage.transform import resize from indicoio import JSON_HEADERS -def api_handler(arg, url, batch=False): +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): data_dict = json.dumps({'data': arg}) if batch: url += "/batch" - response = requests.post(url, data=data_dict, headers=JSON_HEADERS).json() + auth = auth_query() + response = requests.post(url, data=data_dict, headers=JSON_HEADERS, auth=auth).json() results = response.get('results', False) if not results: error = response.get('error')