freddyaboulton HF Staff commited on
Commit
d79620f
·
verified ·
1 Parent(s): 544e95b

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +8 -8
  2. run.ipynb +1 -0
  3. run.py +90 -0
README.md CHANGED
@@ -1,12 +1,12 @@
 
1
  ---
2
- title: Validator Test
3
- emoji: 🌖
4
- colorFrom: green
5
- colorTo: green
6
  sdk: gradio
7
- sdk_version: 5.44.1
8
- app_file: app.py
9
  pinned: false
 
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+
2
  ---
3
+ title: validator_test
4
+ emoji: 🔥
5
+ colorFrom: indigo
6
+ colorTo: indigo
7
  sdk: gradio
8
+ sdk_version: 5.45.0
9
+ app_file: run.py
10
  pinned: false
11
+ hf_oauth: true
12
  ---
 
 
run.ipynb ADDED
@@ -0,0 +1 @@
 
 
1
+ {"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: validator_test"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "\n", "\n", "def validate_input(age, location):\n", " is_age_valid = True\n", " is_location_valid = True\n", " if not age or age < 3:\n", " is_age_valid = False\n", " if \"london\" in location.lower():\n", " is_location_valid = False\n", "\n", " return [\n", " gr.validate(is_age_valid, \"Age must be at least 3\"),\n", " gr.validate(is_location_valid, \"Location must not be in London\"),\n", " ]\n", "\n", "\n", "def process_text(age, location):\n", " result = f\"Processed: {age} -- {location.upper()}\"\n", " return result\n", "\n", "\n", "def validate_image(image):\n", " # we don't want to error when a user is clearing the image\n", " if not image:\n", " return None\n", " is_portrait = image.width < image.height\n", "\n", " return gr.validate(is_portrait, \"Image must be in portrait mode\")\n", "\n", "\n", "def process_image(image):\n", " if not image:\n", " return \"No image uploaded\"\n", " return \"HELLO IMAGE!!!\"\n", "\n", "\n", "def raise_error():\n", " raise ValueError(\"test error\")\n", "\n", "\n", "with gr.Blocks() as demo:\n", " with gr.Tab(\"Text\"):\n", " gr.Markdown(\"# Validator Parameter Test Demo\")\n", "\n", " with gr.Row():\n", " with gr.Column():\n", " age = gr.Number(\n", " label=\"Enter age\",\n", " placeholder=\"Enter age\",\n", " )\n", " location = gr.Textbox(\n", " max_lines=3,\n", " label=\"Enter location\",\n", " placeholder=\"Enter location\",\n", " )\n", "\n", " validate_btn = gr.Button(\"Process with Validation\", variant=\"primary\")\n", "\n", " output_with_validation = gr.Textbox(\n", " label=\"Output (with validation)\", interactive=False\n", " )\n", "\n", " validate_btn.click(\n", " fn=process_text,\n", " validator=validate_input,\n", " inputs=[age, location],\n", " outputs=output_with_validation,\n", " )\n", " with gr.Tab(\"Image\"):\n", " im = gr.Image(label=\"Enter image\", placeholder=\"Enter image\", type=\"pil\")\n", " t = gr.Textbox(label=\"Enter text\", placeholder=\"Enter text\")\n", " im.change(\n", " fn=process_image,\n", " validator=validate_image,\n", " inputs=im,\n", " outputs=t,\n", " )\n", " with gr.Tab(\"Validation Error\"):\n", " error_btn = gr.Button(\"Raise Validation Error\", variant=\"primary\")\n", " error_btn.click(\n", " validator=raise_error,\n", " fn=raise_error,\n", " inputs=[],\n", " outputs=[],\n", " )\n", "\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
run.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+
4
+ def validate_input(age, location):
5
+ is_age_valid = True
6
+ is_location_valid = True
7
+ if not age or age < 3:
8
+ is_age_valid = False
9
+ if "london" in location.lower():
10
+ is_location_valid = False
11
+
12
+ return [
13
+ gr.validate(is_age_valid, "Age must be at least 3"),
14
+ gr.validate(is_location_valid, "Location must not be in London"),
15
+ ]
16
+
17
+
18
+ def process_text(age, location):
19
+ result = f"Processed: {age} -- {location.upper()}"
20
+ return result
21
+
22
+
23
+ def validate_image(image):
24
+ # we don't want to error when a user is clearing the image
25
+ if not image:
26
+ return None
27
+ is_portrait = image.width < image.height
28
+
29
+ return gr.validate(is_portrait, "Image must be in portrait mode")
30
+
31
+
32
+ def process_image(image):
33
+ if not image:
34
+ return "No image uploaded"
35
+ return "HELLO IMAGE!!!"
36
+
37
+
38
+ def raise_error():
39
+ raise ValueError("test error")
40
+
41
+
42
+ with gr.Blocks() as demo:
43
+ with gr.Tab("Text"):
44
+ gr.Markdown("# Validator Parameter Test Demo")
45
+
46
+ with gr.Row():
47
+ with gr.Column():
48
+ age = gr.Number(
49
+ label="Enter age",
50
+ placeholder="Enter age",
51
+ )
52
+ location = gr.Textbox(
53
+ max_lines=3,
54
+ label="Enter location",
55
+ placeholder="Enter location",
56
+ )
57
+
58
+ validate_btn = gr.Button("Process with Validation", variant="primary")
59
+
60
+ output_with_validation = gr.Textbox(
61
+ label="Output (with validation)", interactive=False
62
+ )
63
+
64
+ validate_btn.click(
65
+ fn=process_text,
66
+ validator=validate_input,
67
+ inputs=[age, location],
68
+ outputs=output_with_validation,
69
+ )
70
+ with gr.Tab("Image"):
71
+ im = gr.Image(label="Enter image", placeholder="Enter image", type="pil")
72
+ t = gr.Textbox(label="Enter text", placeholder="Enter text")
73
+ im.change(
74
+ fn=process_image,
75
+ validator=validate_image,
76
+ inputs=im,
77
+ outputs=t,
78
+ )
79
+ with gr.Tab("Validation Error"):
80
+ error_btn = gr.Button("Raise Validation Error", variant="primary")
81
+ error_btn.click(
82
+ validator=raise_error,
83
+ fn=raise_error,
84
+ inputs=[],
85
+ outputs=[],
86
+ )
87
+
88
+
89
+ if __name__ == "__main__":
90
+ demo.launch()