File size: 2,402 Bytes
d79620f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr


def validate_input(age, location):
    is_age_valid = True
    is_location_valid = True
    if not age or age < 3:
        is_age_valid = False
    if "london" in location.lower():
        is_location_valid = False

    return [
        gr.validate(is_age_valid, "Age must be at least 3"),
        gr.validate(is_location_valid, "Location must not be in London"),
    ]


def process_text(age, location):
    result = f"Processed: {age} -- {location.upper()}"
    return result


def validate_image(image):
    # we don't want to error when a user is clearing the image
    if not image:
        return None
    is_portrait = image.width < image.height

    return gr.validate(is_portrait, "Image must be in portrait mode")


def process_image(image):
    if not image:
        return "No image uploaded"
    return "HELLO IMAGE!!!"


def raise_error():
    raise ValueError("test error")


with gr.Blocks() as demo:
    with gr.Tab("Text"):
        gr.Markdown("# Validator Parameter Test Demo")

        with gr.Row():
            with gr.Column():
                age = gr.Number(
                    label="Enter age",
                    placeholder="Enter age",
                )
                location = gr.Textbox(
                    max_lines=3,
                    label="Enter location",
                    placeholder="Enter location",
                )

        validate_btn = gr.Button("Process with Validation", variant="primary")

        output_with_validation = gr.Textbox(
            label="Output (with validation)", interactive=False
        )

        validate_btn.click(
            fn=process_text,
            validator=validate_input,
            inputs=[age, location],
            outputs=output_with_validation,
        )
    with gr.Tab("Image"):
        im = gr.Image(label="Enter image", placeholder="Enter image", type="pil")
        t = gr.Textbox(label="Enter text", placeholder="Enter text")
        im.change(
            fn=process_image,
            validator=validate_image,
            inputs=im,
            outputs=t,
        )
    with gr.Tab("Validation Error"):
        error_btn = gr.Button("Raise Validation Error", variant="primary")
        error_btn.click(
            validator=raise_error,
            fn=raise_error,
            inputs=[],
            outputs=[],
        )


if __name__ == "__main__":
    demo.launch()