gradio/test/test_mix.py

71 lines
2.4 KiB
Python
Raw Normal View History

import os
2021-05-21 03:16:16 +08:00
import unittest
import pytest
2021-05-21 03:16:16 +08:00
import gradio as gr
from gradio import mix
from gradio.external import TooManyRequestsError
2021-11-15 22:39:06 +08:00
"""
WARNING: Some of these tests have an external dependency: namely that Hugging Face's Hub and Space APIs do not change, and they keep their most famous models up. So if, e.g. Spaces is down, then these test will not pass.
"""
2021-11-10 02:30:59 +08:00
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
2021-05-21 03:16:16 +08:00
class TestSeries(unittest.TestCase):
def test_in_interface(self):
io1 = gr.Interface(lambda x: x + " World", "textbox", gr.Textbox())
io2 = gr.Interface(lambda x: x + "!", "textbox", gr.Textbox())
2021-05-21 03:16:16 +08:00
series = mix.Series(io1, io2)
self.assertEqual(series.process(["Hello"]), ["Hello World!"])
2021-05-21 03:16:16 +08:00
@pytest.mark.flaky
2022-03-26 03:16:49 +08:00
def test_with_external(self):
io1 = gr.Interface.load("spaces/abidlabs/image-identity")
io2 = gr.Interface.load("spaces/abidlabs/image-classifier")
series = mix.Series(io1, io2)
try:
output = series("gradio/test_data/lion.jpg")
self.assertGreater(output["lion"], 0.5)
except TooManyRequestsError:
pass
2021-11-15 22:39:06 +08:00
2021-05-21 03:16:16 +08:00
class TestParallel(unittest.TestCase):
def test_in_interface(self):
io1 = gr.Interface(lambda x: x + " World 1!", "textbox", gr.Textbox())
io2 = gr.Interface(lambda x: x + " World 2!", "textbox", gr.Textbox())
2021-05-21 03:16:16 +08:00
parallel = mix.Parallel(io1, io2)
self.assertEqual(
parallel.process(["Hello"]), ["Hello World 1!", "Hello World 2!"]
)
def test_multiple_return_in_interface(self):
io1 = gr.Interface(
lambda x: (x, x + x), "textbox", [gr.Textbox(), gr.Textbox()]
)
io2 = gr.Interface(lambda x: x + " World 2!", "textbox", gr.Textbox())
parallel = mix.Parallel(io1, io2)
self.assertEqual(
parallel.process(["Hello"]), ["Hello", "HelloHello", "Hello World 2!"]
)
@pytest.mark.flaky
2022-03-26 03:16:49 +08:00
def test_with_external(self):
io1 = gr.Interface.load("spaces/abidlabs/english_to_spanish")
io2 = gr.Interface.load("spaces/abidlabs/english2german")
parallel = mix.Parallel(io1, io2)
try:
hello_es, hello_de = parallel("Hello")
self.assertIn("hola", hello_es.lower())
self.assertIn("hallo", hello_de.lower())
except TooManyRequestsError:
pass
2021-05-21 03:16:16 +08:00
if __name__ == "__main__":
2021-05-21 03:16:16 +08:00
unittest.main()