mirror of
https://github.com/wassname/IndicoIo-python.git
synced 2026-07-08 00:11:02 +08:00
FIX: check if data is a list to determine if batch or single request
This commit is contained in:
committed by
Madison May
parent
9bea224d94
commit
1d42d6defe
Binary file not shown.
|
After Width: | Height: | Size: 2.4 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 6.9 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 88 B |
Binary file not shown.
|
After Width: | Height: | Size: 4.1 KiB |
+42
-51
@@ -31,74 +31,74 @@ class BatchAPIRun(unittest.TestCase):
|
||||
|
||||
def test_batch_texttags(self):
|
||||
test_data = ["On Monday, president Barack Obama will be..."]
|
||||
response = batch_text_tags(test_data, api_key=self.api_key)
|
||||
response = text_tags(test_data, api_key=self.api_key)
|
||||
self.assertTrue(isinstance(response, list))
|
||||
|
||||
def test_batch_keywords(self):
|
||||
test_data = ["A working api is key to the success of our young company"]
|
||||
words = [set(text.lower().split()) for text in test_data]
|
||||
response = batch_keywords(test_data, api_key=self.api_key)
|
||||
response = keywords(test_data, api_key=self.api_key)
|
||||
self.assertTrue(isinstance(response, list))
|
||||
self.assertTrue(set(response[0].keys()).issubset(words[0]))
|
||||
|
||||
def test_batch_posneg(self):
|
||||
test_data = ['Worst song ever', 'Best song ever']
|
||||
response = batch_sentiment(test_data, api_key=self.api_key)
|
||||
response = sentiment(test_data, api_key=self.api_key)
|
||||
self.assertTrue(isinstance(response, list))
|
||||
self.assertTrue(response[0] < 0.5)
|
||||
|
||||
def test_batch_sentiment_hq(self):
|
||||
test_data = ['Worst song ever', 'Best song ever']
|
||||
response = batch_sentiment_hq(test_data, api_key=self.api_key)
|
||||
response = sentiment_hq(test_data, api_key=self.api_key)
|
||||
self.assertTrue(isinstance(response, list))
|
||||
self.assertTrue(response[0] < 0.5)
|
||||
|
||||
def test_batch_political(self):
|
||||
test_data = ["Guns don't kill people, people kill people."]
|
||||
response = batch_political(test_data, api_key=self.api_key)
|
||||
response = political(test_data, api_key=self.api_key)
|
||||
self.assertTrue(isinstance(response, list))
|
||||
|
||||
def test_batch_fer(self):
|
||||
test_data = [generate_array((48,48))]
|
||||
response = batch_fer(test_data, api_key=self.api_key)
|
||||
test_data = [os.path.normpath(os.path.join(DIR, "data/48by48.png"))]
|
||||
response = fer(test_data, api_key=self.api_key)
|
||||
self.assertTrue(isinstance(response, list))
|
||||
self.assertTrue(isinstance(response[0], dict))
|
||||
|
||||
def test_batch_content_filtering(self):
|
||||
test_data = [generate_array((48,48))]
|
||||
response = batch_content_filtering(test_data, api_key=self.api_key)
|
||||
test_data = [os.path.normpath(os.path.join(DIR, "data/48by48.png"))]
|
||||
response = content_filtering(test_data, api_key=self.api_key)
|
||||
self.assertTrue(isinstance(response, list))
|
||||
self.assertTrue(isinstance(response[0], float))
|
||||
|
||||
def test_batch_fer_bad_b64(self):
|
||||
test_data = ["$bad#FI jeaf9(#0"]
|
||||
self.assertRaises(IndicoError, batch_fer, test_data, api_key=self.api_key)
|
||||
self.assertRaises(IndicoError, fer, test_data, api_key=self.api_key)
|
||||
|
||||
def test_batch_fer_good_b64(self):
|
||||
test_data = ["iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAg5JREFUeNrEV4uNgzAMpegGyAgZgQ3KBscIjMAGx03QEdqbgG5AOwG3AWwAnSCXqLZkuUkwhfYsvaLm5xc7sZ1dIhdtUVjsLZRFTvp+LSaLq8UZ/s+KMSbZCcY5RV9E4QQKHG7QtgeCGv4PFt8WpzkCcztu3TiL0eJgkQmsVFn0MK+LzYkRKEGpG1GDyZdKRdaolhAoJewXnJsO1jtKCFDlChZAFxyJj2PnBRU20KZg7oMlOAENijpi8hwmGkKkZW2GzONtVLA/DxHAhTO2I7MCVBSQ6nGDlEBJDhyVYiUBHXBxzQm0wE4FzPYsGs856dA9SAAP2oENzFYqR6iAFQpHIAUzO/nxnOgthF/lM3w/3U8KYXTwxG/1IgIulF+wPQUXDMl75UoJZIHstRWpaGb8IGYqwBoKlG/lgpzoUEBoj50p8QtVrmHgaaXyC/H3BFC+e9kGFlCB0CtBF7FifQ8D9zjQQHj0pdOM3F1pUBoFKdxtqkMClScHJCSDlSxhHSNRT5K+FaZnHglrz+AGoxZLKNLYH6s3CkkuyJlp58wviZ4PuSCWDXl5hmjZtxcSCGbDUD3gK7EMOZBLCETrgVBF5K0lI5bIZ0wfrYh8NWHIAiNTPHpuTOKpCes1VTFaiNaFdGwPfdmaqlj6LmjJbgoSSfUW74K3voz+/W0oIeB7HWu2s+dfx3N+eLX8CTAAwUmKjK/dHS4AAAAASUVORK5CYII="]
|
||||
response = batch_fer(test_data, api_key=self.api_key)
|
||||
response = fer(test_data, api_key=self.api_key)
|
||||
self.assertTrue(isinstance(response, list))
|
||||
self.assertTrue(isinstance(response[0], dict))
|
||||
|
||||
def test_batch_fer_filepath(self):
|
||||
test_data = [os.path.normpath(os.path.join(DIR, "data/fear.png"))]
|
||||
response = batch_fer(test_data, api_key=self.api_key)
|
||||
response = fer(test_data, api_key=self.api_key)
|
||||
self.assertTrue(isinstance(response, list))
|
||||
self.assertTrue(isinstance(response[0], dict))
|
||||
|
||||
def test_batch_fer_pil_image(self):
|
||||
test_data = [Image.open(os.path.normpath(os.path.join(DIR, "data/fear.png")))]
|
||||
response = batch_fer(test_data, api_key=self.api_key)
|
||||
response = fer(test_data, api_key=self.api_key)
|
||||
self.assertTrue(isinstance(response, list))
|
||||
self.assertTrue(isinstance(response[0], dict))
|
||||
|
||||
def test_batch_fer_nonexistant_filepath(self):
|
||||
test_data = ["data/unhappy.png"]
|
||||
self.assertRaises(IndicoError, batch_fer, test_data, api_key=self.api_key)
|
||||
self.assertRaises(IndicoError, fer, test_data, api_key=self.api_key)
|
||||
|
||||
def test_batch_facial_features(self):
|
||||
test_data = [generate_array((48,48))]
|
||||
response = batch_facial_features(test_data, api_key=self.api_key)
|
||||
test_data = [os.path.normpath(os.path.join(DIR, "data/48by48.png"))]
|
||||
response = facial_features(test_data, api_key=self.api_key)
|
||||
self.assertTrue(isinstance(response, list))
|
||||
self.assertTrue(isinstance(response[0], list))
|
||||
self.assertEqual(len(response[0]), 48)
|
||||
@@ -117,22 +117,22 @@ class BatchAPIRun(unittest.TestCase):
|
||||
# have decided how we are dealing with them
|
||||
|
||||
def test_batch_image_features_greyscale(self):
|
||||
test_data = [generate_array((48,48))]
|
||||
response = batch_image_features(test_data, api_key=self.api_key)
|
||||
test_data = [os.path.normpath(os.path.join(DIR, "data/48by48.png"))]
|
||||
response = image_features(test_data, api_key=self.api_key)
|
||||
self.assertTrue(isinstance(response, list))
|
||||
self.assertTrue(isinstance(response[0], list))
|
||||
self.assertEqual(len(response[0]), 2048)
|
||||
|
||||
def test_batch_image_features_rgb(self):
|
||||
test_data = [generate_array((48,48))]
|
||||
response = batch_image_features(test_data, api_key=self.api_key)
|
||||
test_data = [os.path.normpath(os.path.join(DIR, "data/48by48rgb.png"))]
|
||||
response = image_features(test_data, api_key=self.api_key)
|
||||
self.assertTrue(isinstance(response, list))
|
||||
self.assertTrue(isinstance(response[0], list))
|
||||
self.assertEqual(len(response[0]), 2048)
|
||||
|
||||
def test_batch_language(self):
|
||||
test_data = ['clearly an english sentence']
|
||||
response = batch_language(test_data, api_key=self.api_key)
|
||||
response = language(test_data, api_key=self.api_key)
|
||||
self.assertTrue(isinstance(response, list))
|
||||
self.assertTrue(response[0]['English'] > 0.25)
|
||||
|
||||
@@ -140,14 +140,15 @@ class BatchAPIRun(unittest.TestCase):
|
||||
batch = ["London Underground's boss Mike Brown warned that the strike ..."]
|
||||
expected_entities = ("London Underground", "Mike Brown")
|
||||
expected_keys = set(["categories", "confidence"])
|
||||
entities = batch_named_entities(batch)[0]
|
||||
entities = named_entities(batch)[0]
|
||||
for entity in expected_entities:
|
||||
assert entity in expected_entities
|
||||
assert not (set(entities[entity]) - expected_keys)
|
||||
|
||||
def test_batch_multi_api_image(self):
|
||||
test_data = [generate_array((48,48)), generate_int_array((48,48))]
|
||||
response = batch_predict_image(test_data, apis=config.IMAGE_APIS, api_key=self.api_key)
|
||||
test_data = [os.path.normpath(os.path.join(DIR, "data/48by48.png")),
|
||||
os.path.normpath(os.path.join(DIR, "data/48by48.png"))]
|
||||
response = predict_image(test_data, apis=config.IMAGE_APIS, api_key=self.api_key)
|
||||
|
||||
self.assertTrue(isinstance(response, dict))
|
||||
self.assertTrue(set(response.keys()) == set(config.IMAGE_APIS))
|
||||
@@ -155,21 +156,21 @@ class BatchAPIRun(unittest.TestCase):
|
||||
|
||||
def test_batch_multi_api_text(self):
|
||||
test_data = ['clearly an english sentence']
|
||||
response = batch_predict_text(test_data, apis=config.TEXT_APIS, api_key=self.api_key)
|
||||
response = predict_text(test_data, apis=config.TEXT_APIS, api_key=self.api_key)
|
||||
|
||||
self.assertTrue(isinstance(response, dict))
|
||||
self.assertTrue(set(response.keys()) == set(config.TEXT_APIS))
|
||||
|
||||
def test_default_multi_api_text(self):
|
||||
test_data = ['clearly an english sentence']
|
||||
response = batch_predict_text(test_data, api_key=self.api_key)
|
||||
response = predict_text(test_data, api_key=self.api_key)
|
||||
|
||||
self.assertTrue(isinstance(response, dict))
|
||||
self.assertTrue(set(response.keys()) == set(config.TEXT_APIS))
|
||||
|
||||
def test_multi_api_bad_api(self):
|
||||
self.assertRaises(IndicoError,
|
||||
batch_predict_text,
|
||||
predict_text,
|
||||
"this shouldn't work",
|
||||
apis=["sentiment", "somethingbad"])
|
||||
|
||||
@@ -180,14 +181,14 @@ class BatchAPIRun(unittest.TestCase):
|
||||
apis=["fer", "sentiment", "facial_features"])
|
||||
def test_batch_multi_bad_mixed_api(self):
|
||||
self.assertRaises(IndicoError,
|
||||
batch_predict_text,
|
||||
predict_text,
|
||||
["this shouldn't work"],
|
||||
apis=["fer", "sentiment", "facial_features"])
|
||||
|
||||
def test_batch_set_cloud(self):
|
||||
test_data = ['clearly an english sentence']
|
||||
self.assertRaises(ConnectionError,
|
||||
batch_language,
|
||||
language,
|
||||
test_data,
|
||||
api_key=self.api_key,
|
||||
cloud='invalid/cloud')
|
||||
@@ -290,7 +291,7 @@ class FullAPIRun(unittest.TestCase):
|
||||
|
||||
def test_good_fer(self):
|
||||
fer_set = set(['Angry', 'Sad', 'Neutral', 'Surprise', 'Fear', 'Happy'])
|
||||
test_face = generate_array((48,48))
|
||||
test_face = os.path.normpath(os.path.join(DIR, "data/48by48.png"))
|
||||
response = fer(test_face)
|
||||
|
||||
self.assertTrue(isinstance(response, dict))
|
||||
@@ -298,14 +299,14 @@ class FullAPIRun(unittest.TestCase):
|
||||
|
||||
def test_good_int_array_fer(self):
|
||||
fer_set = set(['Angry', 'Sad', 'Neutral', 'Surprise', 'Fear', 'Happy'])
|
||||
test_face = generate_int_array((48,48))
|
||||
test_face = os.path.normpath(os.path.join(DIR, "data/48by48.png"))
|
||||
response = fer(test_face)
|
||||
|
||||
self.assertTrue(isinstance(response, dict))
|
||||
self.assertEqual(fer_set, set(response.keys()))
|
||||
|
||||
def test_happy_fer(self):
|
||||
test_face = self.load_image("data/happy.png", as_grey=True)
|
||||
test_face = os.path.normpath(os.path.join(DIR, "data/happy.png"))
|
||||
response = fer(test_face)
|
||||
self.assertTrue(isinstance(response, dict))
|
||||
self.assertTrue(response['Happy'] > 0.5)
|
||||
@@ -317,26 +318,26 @@ class FullAPIRun(unittest.TestCase):
|
||||
self.assertTrue(response['Happy'] > 0.5)
|
||||
|
||||
def test_fear_fer(self):
|
||||
test_face = self.load_image("data/fear.png", as_grey=True)
|
||||
test_face = os.path.normpath(os.path.join(DIR, "data/fear.png"))
|
||||
response = fer(test_face)
|
||||
self.assertTrue(isinstance(response, dict))
|
||||
self.assertTrue(response['Fear'] > 0.25)
|
||||
|
||||
def test_bad_fer(self):
|
||||
fer_set = set(['Angry', 'Sad', 'Neutral', 'Surprise', 'Fear', 'Happy'])
|
||||
test_face = generate_array((56, 56))
|
||||
test_face = os.path.normpath(os.path.join(DIR, "data/64by64.png"))
|
||||
response = fer(test_face)
|
||||
|
||||
self.assertTrue(isinstance(response, dict))
|
||||
self.assertEqual(fer_set, set(response.keys()))
|
||||
|
||||
def test_safe_content_filtering(self):
|
||||
test_face = self.load_image("data/happy.png", as_grey=True)
|
||||
test_face = os.path.normpath(os.path.join(DIR, "data/happy.png"))
|
||||
response = content_filtering(test_face)
|
||||
self.assertTrue(response < 0.5)
|
||||
|
||||
def test_good_facial_features(self):
|
||||
test_face = generate_array((48,48))
|
||||
test_face = os.path.normpath(os.path.join(DIR, "data/48by48.png"))
|
||||
response = facial_features(test_face)
|
||||
|
||||
self.assertTrue(isinstance(response, list))
|
||||
@@ -344,7 +345,7 @@ class FullAPIRun(unittest.TestCase):
|
||||
self.check_range(response)
|
||||
|
||||
def test_rgba_int_array_facial_features(self):
|
||||
test_face = generate_rgba_int_array((48, 48))
|
||||
test_face = os.path.normpath(os.path.join(DIR, "data/48by48rgba.png"))
|
||||
response = facial_features(test_face)
|
||||
|
||||
self.assertTrue(isinstance(response, list))
|
||||
@@ -353,7 +354,7 @@ class FullAPIRun(unittest.TestCase):
|
||||
|
||||
def test_good_int_array_facial_features(self):
|
||||
fer_set = set(['Angry', 'Sad', 'Neutral', 'Surprise', 'Fear', 'Happy'])
|
||||
test_face = generate_int_array((48,48))
|
||||
test_face = os.path.normpath(os.path.join(DIR, "data/48by48.png"))
|
||||
response = facial_features(test_face)
|
||||
|
||||
self.assertTrue(isinstance(response, list))
|
||||
@@ -371,7 +372,7 @@ class FullAPIRun(unittest.TestCase):
|
||||
# self.check_range(response)
|
||||
|
||||
def test_good_image_features_greyscale(self):
|
||||
test_image = generate_array((48,48))
|
||||
test_image = os.path.normpath(os.path.join(DIR, "data/48by48.png"))
|
||||
response = image_features(test_image)
|
||||
|
||||
self.assertTrue(isinstance(response, list))
|
||||
@@ -379,7 +380,7 @@ class FullAPIRun(unittest.TestCase):
|
||||
self.check_range(response)
|
||||
|
||||
def test_good_image_features_rgb(self):
|
||||
test_image = [[(random.random(),) * 3 for _ in xrange(48)] for _ in xrange(48)]
|
||||
test_image = os.path.normpath(os.path.join(DIR, "data/48by48rgb.png"))
|
||||
response = image_features(test_image)
|
||||
|
||||
self.assertTrue(isinstance(response, list))
|
||||
@@ -387,7 +388,7 @@ class FullAPIRun(unittest.TestCase):
|
||||
self.check_range(response)
|
||||
|
||||
def test_multi_api_image(self):
|
||||
test_data = generate_array((48,48))
|
||||
test_data = os.path.normpath(os.path.join(DIR, "data/48by48.png"))
|
||||
response = predict_image(test_data, apis=config.IMAGE_APIS, api_key=self.api_key)
|
||||
|
||||
self.assertTrue(isinstance(response, dict))
|
||||
@@ -558,15 +559,5 @@ def flatten(container):
|
||||
else:
|
||||
yield i
|
||||
|
||||
def generate_array(size):
|
||||
return [[random.random() for _ in xrange(size[0])] for _ in xrange(size[1])]
|
||||
|
||||
def generate_int_array(size):
|
||||
return [[random.randint(0, 255) for _ in xrange(size[0])] for _ in xrange(size[1])]
|
||||
|
||||
def generate_rgba_int_array(size):
|
||||
return [[[random.randint(0, 255) for _ in xrange(3)] for _ in xrange(size[0])] for _ in xrange(size[1])]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user