Require api keys

This commit is contained in:
Madison May
2015-04-07 19:08:40 -04:00
parent 7cde7a7fdb
commit f7c263dc9c
16 changed files with 182 additions and 253 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.5.1',) * 4
Version, version, __version__, VERSION = ('0.5.2',) * 4
from indicoio.text.sentiment import political, posneg
from indicoio.text.sentiment import posneg as sentiment
+12 -10
View File
@@ -16,7 +16,7 @@ class Settings(ConfigParser.ConfigParser):
for fd in self.files:
try:
self.readfp(fd)
except AttributeError:
except AttributeError:
self.read(fd)
self.auth_settings = self.get_section('auth')
@@ -33,22 +33,24 @@ class Settings(ConfigParser.ConfigParser):
def cloud(self):
return (
os.getenv("INDICO_CLOUD") or
self.private_cloud_settings.get('cloud') or
os.getenv("INDICO_CLOUD") or
self.private_cloud_settings.get('cloud') or
None
)
def auth(self):
def api_key(self):
return (
os.getenv("INDICO_USERNAME") or self.auth_settings.get('username'),
os.getenv("INDICO_PASSWORD") or self.auth_settings.get('password')
os.getenv("INDICO_API_KEY") or
self.auth_settings.get('api_key') or
None
)
SETTINGS = Settings(files=[
os.path.expanduser("~/.indicorc"),
os.path.join(os.getcwd(), '.indicorc')
])
AUTH = SETTINGS.auth()
CLOUD = SETTINGS.cloud()
PUBLIC_API_HOST = 'apiv1.indico.io'
api_key = SETTINGS.api_key()
cloud = SETTINGS.cloud()
PUBLIC_API_HOST = 'apiv2.indico.io'
url_protocol = "https:"
View File
+5 -6
View File
@@ -4,7 +4,7 @@ import numpy as np
from indicoio.utils import image_preprocess, api_handler, is_url
import indicoio.config as config
def facial_features(image, cloud=config.CLOUD, batch=False, auth=None, **kwargs):
def facial_features(image, cloud=None, batch=False, api_key=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.
@@ -26,9 +26,9 @@ def facial_features(image, cloud=config.CLOUD, batch=False, auth=None, **kwargs)
:type image: list of lists
:rtype: List containing feature responses
"""
return api_handler(image, cloud=cloud, api="facialfeatures", batch=batch, auth=auth, **kwargs)
return api_handler(image, cloud=cloud, api="facialfeatures", batch=batch, api_key=api_key, **kwargs)
def image_features(image, cloud=config.CLOUD, batch=False, auth=None, **kwargs):
def image_features(image, cloud=None, batch=False, api_key=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.
@@ -58,6 +58,5 @@ def image_features(image, cloud=config.CLOUD, batch=False, auth=None, **kwargs):
:type image: numpy.ndarray
:rtype: List containing features
"""
if not is_url(image, batch=batch):
image = image_preprocess(image, batch=batch)
return api_handler(image, cloud=cloud, api="imagefeatures", batch=batch, auth=auth, **kwargs)
image = image_preprocess(image, batch=batch)
return api_handler(image, cloud=cloud, api="imagefeatures", batch=batch, api_key=api_key, **kwargs)
+2 -2
View File
@@ -4,7 +4,7 @@ import numpy as np
from indicoio.utils import api_handler
import indicoio.config as config
def fer(image, cloud=config.CLOUD, batch=False, auth=None, **kwargs):
def fer(image, cloud=None, batch=False, api_key=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
@@ -28,4 +28,4 @@ def fer(image, cloud=config.CLOUD, batch=False, auth=None, **kwargs):
:rtype: Dictionary containing emotion probability pairs
"""
return api_handler(image, cloud=cloud, api="fer", batch=batch, auth=auth, **kwargs)
return api_handler(image, cloud=cloud, api="fer", batch=batch, api_key=api_key, **kwargs)
View File
+2 -2
View File
@@ -1,7 +1,7 @@
from indicoio.utils import api_handler
import indicoio.config as config
def language(text, cloud=config.CLOUD, batch=False, auth=None, **kwargs):
def language(text, cloud=None, batch=False, api_key=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, cloud=config.CLOUD, batch=False, auth=None, **kwargs):
:rtype: Dictionary of language probability pairs
"""
return api_handler(text, cloud=cloud, api="language", batch=batch, auth=auth, **kwargs)
return api_handler(text, cloud=cloud, api="language", batch=batch, api_key=api_key, **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, cloud=config.CLOUD, batch=False, auth=None, **kwargs):
def political(text, cloud=None, batch=False, api_key=None, **kwargs):
"""
Given input text, returns a probability distribution over the political alignment of the speaker.
@@ -28,9 +28,9 @@ def political(text, cloud=config.CLOUD, batch=False, auth=None, **kwargs):
:rtype: Dictionary of party probability pairs
"""
return api_handler(text, cloud=cloud, api="political", batch=batch, auth=auth, **kwargs)
return api_handler(text, cloud=cloud, api="political", batch=batch, api_key=api_key, **kwargs)
def posneg(text, cloud=config.CLOUD, batch=False, auth=None, **kwargs):
def posneg(text, cloud=None, batch=False, api_key=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, cloud=config.CLOUD, batch=False, auth=None, **kwargs):
:rtype: Float
"""
return api_handler(text, cloud=cloud, api="sentiment", batch=batch, auth=auth, **kwargs)
return api_handler(text, cloud=cloud, api="sentiment", batch=batch, api_key=api_key, **kwargs)
+2 -2
View File
@@ -1,7 +1,7 @@
from indicoio.utils import api_handler
import indicoio.config as config
def text_tags(text, cloud=config.CLOUD, batch=False, auth=None, **kwargs):
def text_tags(text, cloud=None, batch=False, api_key=None, **kwargs):
"""
Given input text, returns a probability distribution over 100 document categories
@@ -23,4 +23,4 @@ def text_tags(text, cloud=config.CLOUD, batch=False, auth=None, **kwargs):
:rtype: Dictionary of class probability pairs
"""
return api_handler(text, cloud=cloud, api="texttags", batch=batch, auth=auth, **kwargs)
return api_handler(text, cloud=cloud, api="texttags", batch=batch, api_key=api_key, **kwargs)
+10 -8
View File
@@ -6,11 +6,12 @@ from skimage.transform import resize
from indicoio import JSON_HEADERS
from indicoio import config
def api_handler(arg, cloud, api, batch=False, auth=None, **kwargs):
def api_handler(arg, cloud, api, batch=False, api_key=None, **kwargs):
data = {'data': arg}
data.update(**kwargs)
json_data = json.dumps(data)
if not cloud:
cloud=config.cloud
if cloud:
host = "%s.indico.domains" % cloud
@@ -18,14 +19,15 @@ def api_handler(arg, cloud, api, batch=False, auth=None, **kwargs):
# default to indico public cloud
host = config.PUBLIC_API_HOST
url = "http://%s/%s" % (host, api)
if batch:
url += "/batch"
if not api_key:
api_key = config.api_key
if not auth:
auth = config.AUTH
url = config.url_protocol + "//%s/%s" % (host, api)
url = url + "/batch" if batch else url
url += "?key=%s" % api_key
response = requests.post(url, data=json_data, headers=JSON_HEADERS, auth=auth)
response = requests.post(url, data=json_data, headers=JSON_HEADERS)
if response.status_code == 503 and cloud != None:
raise Exception("Private cloud '%s' does not include api '%s'" % (cloud, api))