File size: 6,656 Bytes
0ad74ed |
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
import ast
import inspect
from pathlib import Path
import pytest
from fastapi.testclient import TestClient
import gradio as gr
class TestEvent:
def test_clear_event(self):
def fn_img_cleared():
print("image cleared")
with gr.Blocks() as demo:
img = gr.Image(
type="pil", label="Start by uploading an image", elem_id="input_image"
)
img.clear(fn_img_cleared, [], [])
assert "dependencies" in demo.config
assert demo.config["dependencies"][0]["targets"][0][1] == "clear"
def test_event_data(self):
with gr.Blocks() as demo:
text = gr.Textbox()
gallery = gr.Gallery()
def fn_img_index(evt: gr.SelectData):
return evt.index
gallery.select(fn_img_index, None, text)
app, _, _ = demo.launch(prevent_thread_lock=True)
client = TestClient(app)
resp = client.post(
f"{demo.local_api_url}run/predict",
json={"fn_index": 0, "data": [], "event_data": {"index": 1, "value": None}},
)
assert resp.status_code == 200
assert resp.json()["data"][0] == "1"
def test_consecutive_events(self):
def double(x):
return x + x
def reverse(x):
return x[::-1]
def clear():
return ""
with gr.Blocks() as child:
txt1 = gr.Textbox()
txt2 = gr.Textbox()
txt3 = gr.Textbox()
txt1.submit(double, txt1, txt2).then(reverse, txt2, txt3).success(
clear, None, txt1
)
with gr.Blocks() as parent:
txt0 = gr.Textbox()
txt0.submit(lambda x: x, txt0, txt0)
child.render()
assert "dependencies" in parent.config
assert parent.config["dependencies"][1]["trigger_after"] is None
assert parent.config["dependencies"][2]["trigger_after"] == 1
assert parent.config["dependencies"][3]["trigger_after"] == 2
assert not parent.config["dependencies"][2]["trigger_only_on_success"]
assert parent.config["dependencies"][3]["trigger_only_on_success"]
def test_on_listener(self):
with gr.Blocks() as demo:
name = gr.Textbox(label="Name")
output = gr.Textbox(label="Output Box")
greet_btn = gr.Button("Greet")
def greet(name):
return "Hello " + name + "!"
gr.on(
triggers=[name.submit, greet_btn.click, demo.load],
fn=greet,
inputs=name,
outputs=output,
)
with gr.Row():
num1 = gr.Slider(1, 10)
num2 = gr.Slider(1, 10)
num3 = gr.Slider(1, 10)
output = gr.Number(label="Sum")
@gr.on(inputs=[num1, num2, num3], outputs=output)
def sum(a, b, c):
return a + b + c
assert "dependencies" in demo.config
assert demo.config["dependencies"][0]["targets"] == [
(name._id, "submit"),
(greet_btn._id, "click"),
(demo._id, "load"),
]
assert demo.config["dependencies"][1]["targets"] == [
(num1._id, "change"),
(num2._id, "change"),
(num3._id, "change"),
(0, "load"),
]
def test_load_chaining(self):
calls = 0
def increment():
nonlocal calls
calls += 1
return str(calls)
with gr.Blocks() as demo:
out = gr.Textbox(label="Call counter")
demo.load(increment, inputs=None, outputs=out).then(
increment, inputs=None, outputs=out
)
assert "dependencies" in demo.config
assert demo.config["dependencies"][0]["targets"][0][1] == "load"
assert demo.config["dependencies"][0]["trigger_after"] is None
assert demo.config["dependencies"][1]["targets"][0][1] == "then"
assert demo.config["dependencies"][1]["trigger_after"] == 0
def test_load_chaining_reuse(self):
calls = 0
def increment():
nonlocal calls
calls += 1
return str(calls)
with gr.Blocks() as demo:
out = gr.Textbox(label="Call counter")
demo.load(increment, inputs=None, outputs=out).then(
increment, inputs=None, outputs=out
)
with gr.Blocks() as demo2:
demo.render()
assert "dependencies" in demo2.config
assert demo2.config["dependencies"][0]["targets"][0][1] == "load"
assert demo2.config["dependencies"][0]["trigger_after"] is None
assert demo2.config["dependencies"][1]["targets"][0][1] == "then"
assert demo2.config["dependencies"][1]["trigger_after"] == 0
class TestEventErrors:
def test_event_defined_invalid_scope(self):
with gr.Blocks() as demo:
textbox = gr.Textbox()
textbox.blur(lambda x: x + x, textbox, textbox)
with pytest.raises(AttributeError):
demo.load(lambda: "hello", None, textbox)
with pytest.raises(AttributeError):
textbox.change(lambda x: x + x, textbox, textbox)
def test_event_pyi_file_matches_source_code():
"""Test that the template used to create pyi files (search INTERFACE_TEMPLATE in component_meta) matches the source code of EventListener._setup."""
code = (
Path(__file__).parent / ".." / "gradio" / "components" / "button.pyi"
).read_text()
mod = ast.parse(code)
segment = None
for node in ast.walk(mod):
if isinstance(node, ast.FunctionDef) and node.name == "click":
segment = ast.get_source_segment(code, node)
# This would fail if Button no longer has a click method
assert segment
sig = inspect.signature(gr.Button.click)
for param in sig.parameters.values():
if param.name in ["block", "time_limit", "stream_every", "like_user_message"]:
continue
assert param.name in segment
code = (
Path(__file__).parent / ".." / "gradio" / "components" / "image.pyi"
).read_text()
mod = ast.parse(code)
segment = None
for node in ast.walk(mod):
if isinstance(node, ast.FunctionDef) and node.name == "stream":
segment = ast.get_source_segment(code, node)
# This would fail if Image no longer has a stream method
assert segment
sig = inspect.signature(gr.Image.stream)
for param in ["time_limit", "stream_every"]:
assert param in segment
|