gradio/test/test_outputs.py

78 lines
15 KiB
Python
Raw Normal View History

2019-02-28 08:54:08 +08:00
import numpy as np
import unittest
import os
from gradio import outputs
2019-03-06 15:23:04 +08:00
import json
2019-02-28 08:54:08 +08:00
PACKAGE_NAME = 'gradio'
2019-03-18 20:38:10 +08:00
BASE64_IMG = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wgARCADIAMgDASIAAhEBAxEB/8QAHAAAAQUBAQEAAAAAAAAAAAAABQACAwQGBwEI/8QAGgEAAwEBAQEAAAAAAAAAAAAAAQIDBAAFBv/aAAwDAQACEAMQAAAB09a1OqDQ98RNiD50j1JoWICkgVvMXUcqCNvqPJeNIQ++Pr0KuXmGfJPfPrQvUA6AQpUg3Y6OqBCz2SJZ7N5Zm+SPp0Escx6s70WQRK41/dsKw2yVmDkAUiVmgur0womMbrYuzSqrUxSYuMvBGByxWsSerPHXBJz0n2aZ9bZMc8C3cKvk3alA87zHa8Y84KNW2JzFKJlKV/S8LvkZSYpc70xBb1K88NddNM7i2kEpht+eWSLVZsMlNmsWaSpmvjLxGiHz01bl10e/Tl6IaFl3kUC2IVtWyW9wCJOqyAukhJrPR7/GHrftey1JfYIqcsz0gOpyxw2wPzIwXeDOpcwoxEUd7Rn6cWpvpH2KKdNA/Nkxxz2kLStNpc5pM95JLUnAfa8usaflqiwJ5keBTT0Gnnx3NptDzyUd0jOvyQnZNS31sUhtxb/JF21YlTMiSNEKKU6cm9KF0OTVIvYxK3XdHZYxRd3NjrwXRi5qvk5zRwq1kk4uOD6V82vkgs5wfhmh9LNWkjizPmXtuHqCDKnbsyNI47vv86o7cvSPMdRou0pi6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iC2R3Ol0Tcnjpx8Fh451+buXN4MUDPjOWLJizYlnxNwnXOrT9mt3Mkk3jHea8w+Mptz6qn65Y4rDMsZbotdYqIGW5hXryuXfW8UEYx4hfAJzecsLknvcBn1A/nnv7n5HiPosfO1H+pOdYu7QfPThqmTrPqKz9hn/F9mOE/Zji+i739uv8Amau248ON+7/4fTcf9X/OoSdqo42OGRLwSVwiW5zPOv2kOyPAw/YeWdtn7dacPPRPr9j+0/8Ah9Nj/wAWdek0j5TXnbPx9SWO3BgOO2gg19icJxjnwv2//8QAIxEAAgICAwABBQEAAAAAAAAAAAECEQMQEiExQQQTIjJRIP/aAAgBAwEBPwGP7El2cKRxsrVnIstCHO+tL2yy+jkyT09LwXpQtqBwoUBoWpR+RCoi4y6Jx4vUPR5Bv8bObs9EmWVZ4y+7MPyyU+WsaOBVqj7VkFXRb+CjteFN9jkY58kxagvxs5jdIU3YuhRY3fTJdIVQgSMbpPbfwUes6TIytEYR/pKaRycnZKblqHW5DySZcjjL+mGXVCkynIyLhAUqLExanks530WXZgffZGiUlBGTLyLE0xRtC81ZY5nJsx+EFkroycr/ACJeaaojmkiGSMlQ2iUUlbOWoiXR9K24NGVuUif8/wAcRNoy+D1FEVbURYlCNIm+9e6SK1m+CfusZ9N3kRm/Vk9fAhC1/8QAJBEAAgIBBAIDAQEBAAAAAAAAAAECEQMSITFBECIEMlETFGH/2gAIAQIBAT8Bl9CMtiWQTdml9FvsXAsdiVFdj/6NJKyjJk9KIy/Ta6FGhcGixY6Konuy+jkklXjSmh0iL7ExD8KXQ4dn87dsaobdCs1dGhMUN6FBV4tFF0XaOif4ONCJCnsQka6Gylz45e5aWxGJKNE2JGRCikiKR0cjN1uR9mTfZDgyy0tDle4ib3NRxHYSZWljZV8FKKHNSdCPkK5IoR9p7kcUInokPJFbUZP0aR
2019-02-28 08:54:08 +08:00
class TestLabel(unittest.TestCase):
2019-06-19 04:13:50 +08:00
def test_path_exists(self):
out = outputs.Label()
2020-07-01 07:11:35 +08:00
path = outputs.BASE_OUTPUT_INTERFACE_JS_PATH.format(out.__class__.__name__)
2019-06-20 03:03:54 +08:00
self.assertTrue(os.path.exists(os.path.join(PACKAGE_NAME, path)))
2019-02-28 08:54:08 +08:00
2020-06-12 03:31:44 +08:00
# def test_postprocessing_string(self):
# string = 'happy'
# out = outputs.Label()
# label = json.loads(out.postprocess(string))
# self.assertDictEqual(label, {outputs.Label.LABEL_KEY: string})
#
# def test_postprocessing_1D_array(self):
# array = np.array([0.1, 0.2, 0, 0.7, 0])
# true_label = {outputs.Label.LABEL_KEY: 3,
# outputs.Label.CONFIDENCES_KEY: [
# {outputs.Label.LABEL_KEY: 3, outputs.Label.CONFIDENCE_KEY: 0.7},
# {outputs.Label.LABEL_KEY: 1, outputs.Label.CONFIDENCE_KEY: 0.2},
# {outputs.Label.LABEL_KEY: 0, outputs.Label.CONFIDENCE_KEY: 0.1},
# ]}
# out = outputs.Label()
# label = json.loads(out.postprocess(array))
# self.assertDictEqual(label, true_label)
2019-02-28 08:54:08 +08:00
2020-06-12 03:31:44 +08:00
# def test_postprocessing_1D_array_no_confidences(self):
# array = np.array([0.1, 0.2, 0, 0.7, 0])
# true_label = {outputs.Label.LABEL_KEY: 3}
# out = outputs.Label(show_confidences=False)
# label = json.loads(out.postprocess(array))
# self.assertDictEqual(label, true_label)
#
# def test_postprocessing_int(self):
# true_label_array = np.array([[[3]]])
# true_label = {outputs.Label.LABEL_KEY: 3}
# out = outputs.Label()
# label = json.loads(out.postprocess(true_label_array))
# self.assertDictEqual(label, true_label)
2019-02-28 08:54:08 +08:00
2019-06-19 04:13:50 +08:00
class TestTextbox(unittest.TestCase):
def test_path_exists(self):
out = outputs.Textbox()
2020-07-01 07:11:35 +08:00
path = outputs.BASE_OUTPUT_INTERFACE_JS_PATH.format(out.__class__.__name__)
2019-06-20 03:03:54 +08:00
self.assertTrue(os.path.exists(os.path.join(PACKAGE_NAME, path)))
2019-02-28 08:54:08 +08:00
def test_postprocessing(self):
string = 'happy'
out = outputs.Textbox()
string = out.postprocess(string)
2019-02-28 08:54:08 +08:00
self.assertEqual(string, string)
2019-03-18 20:38:10 +08:00
class TestImage(unittest.TestCase):
2019-06-19 04:13:50 +08:00
def test_path_exists(self):
out = outputs.Image()
2020-07-01 07:11:35 +08:00
path = outputs.BASE_OUTPUT_INTERFACE_JS_PATH.format(out.__class__.__qualname__)
2019-06-20 03:03:54 +08:00
self.assertTrue(os.path.exists(os.path.join(PACKAGE_NAME, path)))
2019-03-18 20:38:10 +08:00
def test_postprocessing(self):
string = BASE64_IMG
out = outputs.Textbox()
string = out.postprocess(string)
self.assertEqual(string, string)
2019-02-28 08:54:08 +08:00
if __name__ == '__main__':
unittest.main()