2022-08-23 23:31:04 +08:00
|
|
|
import json
|
2022-01-21 21:44:12 +08:00
|
|
|
import os
|
|
|
|
|
2022-07-12 01:20:11 +08:00
|
|
|
import pytest
|
|
|
|
|
2021-05-21 03:16:16 +08:00
|
|
|
import gradio as gr
|
|
|
|
from gradio import mix
|
2022-06-14 08:57:45 +08:00
|
|
|
from gradio.external import TooManyRequestsError
|
2021-11-09 22:48:55 +08:00
|
|
|
|
2021-11-15 22:39:06 +08:00
|
|
|
"""
|
2022-08-11 06:29:14 +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-15 22:39:06 +08:00
|
|
|
"""
|
|
|
|
|
2021-11-10 02:30:59 +08:00
|
|
|
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
2021-11-09 22:48:55 +08:00
|
|
|
|
2021-05-21 03:16:16 +08:00
|
|
|
|
2022-08-11 06:29:14 +08:00
|
|
|
class TestSeries:
|
2022-08-23 23:31:04 +08:00
|
|
|
def test_in_interface(self):
|
2023-04-28 04:09:50 +08:00
|
|
|
io1 = gr.Interface(lambda x: f"{x} World", "textbox", gr.Textbox())
|
|
|
|
io2 = gr.Interface(lambda x: f"{x}!", "textbox", gr.Textbox())
|
2021-05-21 03:16:16 +08:00
|
|
|
series = mix.Series(io1, io2)
|
2022-08-23 23:31:04 +08:00
|
|
|
assert series("Hello") == "Hello World!"
|
2021-05-21 03:16:16 +08:00
|
|
|
|
2022-08-23 23:31:04 +08:00
|
|
|
@pytest.mark.flaky
|
|
|
|
def test_with_external(self):
|
2023-03-24 06:33:44 +08:00
|
|
|
io1 = gr.load("spaces/abidlabs/image-identity")
|
|
|
|
io2 = gr.load("spaces/abidlabs/image-classifier")
|
2022-08-23 23:31:04 +08:00
|
|
|
series = mix.Series(io1, io2)
|
|
|
|
try:
|
2023-05-05 10:54:23 +08:00
|
|
|
with open(series("gradio/test_data/lion.jpg")) as f:
|
|
|
|
assert json.load(f)["label"] == "lion"
|
2022-08-23 23:31:04 +08:00
|
|
|
except TooManyRequestsError:
|
|
|
|
pass
|
2021-11-15 22:39:06 +08:00
|
|
|
|
|
|
|
|
2022-08-11 06:29:14 +08:00
|
|
|
class TestParallel:
|
2022-08-23 23:31:04 +08:00
|
|
|
def test_in_interface(self):
|
2023-04-28 04:09:50 +08:00
|
|
|
io1 = gr.Interface(lambda x: f"{x} World 1!", "textbox", gr.Textbox())
|
|
|
|
io2 = gr.Interface(lambda x: f"{x} World 2!", "textbox", gr.Textbox())
|
2021-05-21 03:16:16 +08:00
|
|
|
parallel = mix.Parallel(io1, io2)
|
2022-08-23 23:31:04 +08:00
|
|
|
assert parallel("Hello") == ["Hello World 1!", "Hello World 2!"]
|
2022-01-21 21:44:12 +08:00
|
|
|
|
2022-08-23 23:31:04 +08:00
|
|
|
def test_multiple_return_in_interface(self):
|
2022-06-23 14:48:42 +08:00
|
|
|
io1 = gr.Interface(
|
|
|
|
lambda x: (x, x + x), "textbox", [gr.Textbox(), gr.Textbox()]
|
|
|
|
)
|
2023-04-28 04:09:50 +08:00
|
|
|
io2 = gr.Interface(lambda x: f"{x} World 2!", "textbox", gr.Textbox())
|
2022-06-23 14:48:42 +08:00
|
|
|
parallel = mix.Parallel(io1, io2)
|
2022-08-23 23:31:04 +08:00
|
|
|
assert parallel("Hello") == [
|
2022-08-11 06:29:14 +08:00
|
|
|
"Hello",
|
|
|
|
"HelloHello",
|
|
|
|
"Hello World 2!",
|
|
|
|
]
|
2022-06-23 14:48:42 +08:00
|
|
|
|
2022-07-12 01:20:11 +08:00
|
|
|
@pytest.mark.flaky
|
2022-03-26 03:16:49 +08:00
|
|
|
def test_with_external(self):
|
2023-03-24 06:33:44 +08:00
|
|
|
io1 = gr.load("spaces/abidlabs/english_to_spanish")
|
|
|
|
io2 = gr.load("spaces/abidlabs/english2german")
|
2022-03-26 03:16:49 +08:00
|
|
|
parallel = mix.Parallel(io1, io2)
|
2022-06-14 08:57:45 +08:00
|
|
|
try:
|
|
|
|
hello_es, hello_de = parallel("Hello")
|
2022-08-11 06:29:14 +08:00
|
|
|
assert "hola" in hello_es.lower()
|
|
|
|
assert "hallo" in hello_de.lower()
|
2022-06-14 08:57:45 +08:00
|
|
|
except TooManyRequestsError:
|
|
|
|
pass
|