File size: 2,213 Bytes
8766bc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import json

import pytest

import gradio as gr
from gradio import mix
from gradio.external import TooManyRequestsError

"""
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.
"""


class TestSeries:
    def test_in_interface(self):
        io1 = gr.Interface(lambda x: f"{x} World", "textbox", gr.Textbox())
        io2 = gr.Interface(lambda x: f"{x}!", "textbox", gr.Textbox())
        series = mix.Series(io1, io2)
        assert series("Hello") == "Hello World!"

    @pytest.mark.flaky
    def test_with_external(self):
        io1 = gr.load("spaces/gradio-tests/image-identity")
        io2 = gr.load("spaces/gradio-tests/image-classifier")
        series = mix.Series(io1, io2)
        try:
            with open(series("gradio/test_data/lion.jpg")) as f:
                assert json.load(f)["label"] == "lion"
        except TooManyRequestsError:
            pass


class TestParallel:
    def test_in_interface(self):
        io1 = gr.Interface(lambda x: f"{x} World 1!", "textbox", gr.Textbox())
        io2 = gr.Interface(lambda x: f"{x} World 2!", "textbox", gr.Textbox())
        parallel = mix.Parallel(io1, io2)
        assert parallel("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: f"{x} World 2!", "textbox", gr.Textbox())
        parallel = mix.Parallel(io1, io2)
        assert parallel("Hello") == [
            "Hello",
            "HelloHello",
            "Hello World 2!",
        ]

    @pytest.mark.flaky
    def test_with_external(self):
        io1 = gr.load("spaces/gradio-tests/english_to_spanish")
        io2 = gr.load("spaces/gradio-tests/english2german")
        parallel = mix.Parallel(io1, io2)
        try:
            hello_es, hello_de = parallel("Hello")
            assert "hola" in hello_es.lower()
            assert "hallo" in hello_de.lower()
        except TooManyRequestsError:
            pass