mirror of
https://github.com/wassname/IndicoIo-python.git
synced 2026-08-18 11:40:35 +08:00
Batch API support
This commit is contained in:
+10
-9
@@ -13,12 +13,13 @@ 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
|
||||||
|
|
||||||
political = partial(political, config.api_root)
|
apis = ['political', 'posneg', 'sentiment', 'language', 'fer',
|
||||||
posneg = partial(posneg, config.api_root)
|
'facial_features', 'image_features', 'text_tags']
|
||||||
sentiment = partial(sentiment, config.api_root)
|
apis = dict((api, globals().get(api)) for api in apis)
|
||||||
posneg = partial(sentiment, config.api_root)
|
local = {}
|
||||||
language = partial(language, config.api_root)
|
|
||||||
fer = partial(fer, config.api_root)
|
for api in apis:
|
||||||
facial_features = partial(facial_features, config.api_root)
|
globals()[api] = partial(apis[api], config.api_root)
|
||||||
image_features = partial(image_features, config.api_root)
|
globals()['batch_' + api] = partial(apis[api], config.api_root, batch=True)
|
||||||
text_tags = partial(text_tags, config.api_root)
|
local[api] = partial(apis[api], config.local_api_root)
|
||||||
|
local[api] = partial(apis[api], config.local_api_root, batch=True)
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import numpy as np
|
|||||||
|
|
||||||
from indicoio.utils import image_preprocess, api_handler
|
from indicoio.utils import image_preprocess, api_handler
|
||||||
|
|
||||||
def facial_features(api_root, image):
|
def facial_features(api_root, image, batch=False):
|
||||||
"""
|
"""
|
||||||
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.
|
||||||
@@ -27,9 +27,9 @@ def facial_features(api_root, image):
|
|||||||
: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")
|
return api_handler(image, api_root + "facialfeatures", batch=batch)
|
||||||
|
|
||||||
def image_features(api_root, image):
|
def image_features(api_root, image, batch=False):
|
||||||
"""
|
"""
|
||||||
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.
|
||||||
@@ -60,4 +60,4 @@ def image_features(api_root, image):
|
|||||||
:rtype: List containing features
|
:rtype: List containing features
|
||||||
"""
|
"""
|
||||||
image = image_preprocess(image)
|
image = image_preprocess(image)
|
||||||
return api_handler(image, api_root + "imagefeatures")
|
return api_handler(image, api_root + "imagefeatures", batch=batch)
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import requests
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
from indicoio.utils import api_handler
|
from indicoio.utils import api_handler
|
||||||
|
|
||||||
def fer(api_root, image):
|
def fer(api_root, image, batch=False):
|
||||||
"""
|
"""
|
||||||
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
|
||||||
@@ -28,4 +28,4 @@ def fer(api_root, image):
|
|||||||
:rtype: Dictionary containing emotion probability pairs
|
:rtype: Dictionary containing emotion probability pairs
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return api_handler(image, api_root + "fer")
|
return api_handler(image, api_root + "fer", batch=batch)
|
||||||
|
|||||||
@@ -1,22 +0,0 @@
|
|||||||
from functools import partial
|
|
||||||
import indicoio.config as config
|
|
||||||
|
|
||||||
JSON_HEADERS = {'Content-type': 'application/json', 'Accept': 'text/plain'}
|
|
||||||
|
|
||||||
from indicoio.text.sentiment import political, posneg
|
|
||||||
from indicoio.text.sentiment import posneg as sentiment
|
|
||||||
from indicoio.text.lang import language
|
|
||||||
from indicoio.text.tagging import text_tags
|
|
||||||
from indicoio.images.fer import fer
|
|
||||||
from indicoio.images.features import facial_features
|
|
||||||
from indicoio.images.features import image_features
|
|
||||||
|
|
||||||
political = partial(political, config.local_api_root)
|
|
||||||
posneg = partial(posneg, config.local_api_root)
|
|
||||||
sentiment = partial(sentiment, config.local_api_root)
|
|
||||||
posneg = partial(sentiment, config.local_api_root)
|
|
||||||
language = partial(language, config.local_api_root)
|
|
||||||
fer = partial(fer, config.local_api_root)
|
|
||||||
facial_features = partial(facial_features, config.local_api_root)
|
|
||||||
image_features = partial(image_features, config.local_api_root)
|
|
||||||
text_tags = partial(text_tags, config.local_api_root)
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
from indicoio.utils import api_handler
|
from indicoio.utils import api_handler
|
||||||
|
|
||||||
def language(api_root, text):
|
def language(api_root, text, batch=False):
|
||||||
"""
|
"""
|
||||||
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.
|
||||||
@@ -23,4 +23,4 @@ def language(api_root, text):
|
|||||||
:rtype: Dictionary of language probability pairs
|
:rtype: Dictionary of language probability pairs
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return api_handler(text, api_root + "language")
|
return api_handler(text, api_root + "language", batch=batch)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from indicoio import JSON_HEADERS
|
from indicoio import JSON_HEADERS
|
||||||
from indicoio.utils import api_handler
|
from indicoio.utils import api_handler
|
||||||
|
|
||||||
def political(api_root, text):
|
def political(api_root, text, batch=False):
|
||||||
"""
|
"""
|
||||||
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.
|
||||||
|
|
||||||
@@ -27,9 +27,9 @@ def political(api_root, text):
|
|||||||
:rtype: Dictionary of party probability pairs
|
:rtype: Dictionary of party probability pairs
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return api_handler(text, api_root + "political")
|
return api_handler(text, api_root + "political", batch=batch)
|
||||||
|
|
||||||
def posneg(api_root, text):
|
def posneg(api_root, text, batch=False):
|
||||||
"""
|
"""
|
||||||
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.
|
||||||
@@ -50,4 +50,4 @@ def posneg(api_root, text):
|
|||||||
:rtype: Float
|
:rtype: Float
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return api_handler(text, api_root + "sentiment")
|
return api_handler(text, api_root + "sentiment", batch=batch)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
from indicoio.utils import api_handler
|
from indicoio.utils import api_handler
|
||||||
|
|
||||||
def text_tags(api_root, text):
|
def text_tags(api_root, text, batch=False):
|
||||||
"""
|
"""
|
||||||
Given input text, returns a probability distribution over 100 document categories
|
Given input text, returns a probability distribution over 100 document categories
|
||||||
|
|
||||||
@@ -22,4 +22,4 @@ def text_tags(api_root, text):
|
|||||||
:rtype: Dictionary of class probability pairs
|
:rtype: Dictionary of class probability pairs
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return api_handler(text, api_root + "texttags")
|
return api_handler(text, api_root + "texttags", batch=batch)
|
||||||
|
|||||||
@@ -4,8 +4,10 @@ from skimage.transform import resize
|
|||||||
|
|
||||||
from indicoio import JSON_HEADERS
|
from indicoio import JSON_HEADERS
|
||||||
|
|
||||||
def api_handler(arg, url):
|
def api_handler(arg, url, batch=False):
|
||||||
data_dict = json.dumps({'data': arg})
|
data_dict = json.dumps({'data': arg})
|
||||||
|
if batch:
|
||||||
|
url += "/batch"
|
||||||
response = requests.post(url, data=data_dict, headers=JSON_HEADERS).json()
|
response = requests.post(url, data=data_dict, headers=JSON_HEADERS).json()
|
||||||
results = response.get('results', False)
|
results = response.get('results', False)
|
||||||
if not results:
|
if not results:
|
||||||
|
|||||||
Reference in New Issue
Block a user