freddyaboulton HF Staff commited on
Commit
89471fc
·
verified ·
1 Parent(s): 454e132

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +8 -8
  2. cheetah.jpg +0 -0
  3. run.ipynb +1 -0
  4. run.py +79 -0
README.md CHANGED
@@ -1,12 +1,12 @@
 
1
  ---
2
- title: Component Props
3
- emoji: 😻
4
- colorFrom: purple
5
- colorTo: red
6
  sdk: gradio
7
- sdk_version: 5.49.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: component_props
4
+ emoji: 🔥
5
+ colorFrom: indigo
6
+ colorTo: indigo
7
  sdk: gradio
8
+ sdk_version: 6.0.0
9
+ app_file: run.py
10
  pinned: false
11
+ hf_oauth: true
12
  ---
 
 
cheetah.jpg ADDED
run.ipynb ADDED
@@ -0,0 +1 @@
 
 
1
+ {"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: component_props"]}, {"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": ["# Downloading files from the demo repo\n", "import os\n", "!wget -q https://github.com/gradio-app/gradio/raw/main/demo/component_props/cheetah.jpg"]}, {"cell_type": "code", "execution_count": null, "id": "44380577570523278879349135829904343037", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "from gradio.media import get_image\n", "\n", "with gr.Blocks() as demo:\n", " a = gr.Number(value=5, minimum=0, maximum=10, label=\"Input A\", info=\"Enter a number between 0 and 10\")\n", " output_a = gr.JSON(label=\"Output\", elem_id=\"output\")\n", " with gr.Row():\n", " show_value_btn = gr.Button(\"Show Value\")\n", " double_btn = gr.Button(\"Double Value and Maximum\")\n", " reset_btn = gr.Button(\"Reset\")\n", "\n", " def process_with_props(x: gr.Number):\n", " return {\n", " \"value\": x.value,\n", " \"maximum\": x.maximum,\n", " \"minimum\": x.minimum,\n", " }\n", " show_value_btn.click(process_with_props, a, output_a)\n", "\n", " def double_value_and_max(x: gr.Number):\n", " x.maximum *= 2 # type: ignore\n", " x.value = (x.value or 0) * 2\n", " x.info = f\"Enter a number between 0 and {x.maximum}\"\n", " return x\n", "\n", " double_btn.click(double_value_and_max, a, a).then(\n", " process_with_props, a, output_a\n", " )\n", "\n", " def reset(x: gr.Number):\n", " x.maximum = 10\n", " x.value = 5\n", " x.info = \"Enter a number between 0 and 10\"\n", " return x\n", "\n", " reset_btn.click(reset, a, a).then(\n", " process_with_props, a, output_a\n", " )\n", "\n", " # Image component demo\n", " gr.Markdown(\"## Image Component Props\")\n", " b = gr.Image(value=get_image(\"cheetah.jpg\"), label=\"Input Image\", width=300, height=300, type=\"filepath\")\n", " output_b = gr.JSON(label=\"Image Props Output\", elem_id=\"image-output\")\n", " with gr.Row():\n", " show_image_props_btn = gr.Button(\"Show Image Props\")\n", " change_image_size_btn = gr.Button(\"Change Image Size\")\n", " reset_image_btn = gr.Button(\"Reset Image\")\n", "\n", " def show_image_props(x: gr.Image):\n", " return {\n", " \"value\": x.value if x.value is None else str(x.value),\n", " \"width\": x.width,\n", " \"height\": x.height,\n", " \"type\": x.type,\n", " }\n", " show_image_props_btn.click(show_image_props, b, output_b)\n", "\n", " def change_image_size(x: gr.Image):\n", " x.width = 400\n", " x.height = 400\n", " return x\n", "\n", " change_image_size_btn.click(change_image_size, b, b).then(\n", " show_image_props, b, output_b\n", " )\n", "\n", " def reset_image(x: gr.Image):\n", " x.width = 300\n", " x.height = 300\n", " x.value = get_image(\"cheetah.jpg\")\n", " return x\n", "\n", " reset_image_btn.click(reset_image, b, b).then(\n", " show_image_props, b, output_b\n", " )\n", "\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
run.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from gradio.media import get_image
3
+
4
+ with gr.Blocks() as demo:
5
+ a = gr.Number(value=5, minimum=0, maximum=10, label="Input A", info="Enter a number between 0 and 10")
6
+ output_a = gr.JSON(label="Output", elem_id="output")
7
+ with gr.Row():
8
+ show_value_btn = gr.Button("Show Value")
9
+ double_btn = gr.Button("Double Value and Maximum")
10
+ reset_btn = gr.Button("Reset")
11
+
12
+ def process_with_props(x: gr.Number):
13
+ return {
14
+ "value": x.value,
15
+ "maximum": x.maximum,
16
+ "minimum": x.minimum,
17
+ }
18
+ show_value_btn.click(process_with_props, a, output_a)
19
+
20
+ def double_value_and_max(x: gr.Number):
21
+ x.maximum *= 2 # type: ignore
22
+ x.value = (x.value or 0) * 2
23
+ x.info = f"Enter a number between 0 and {x.maximum}"
24
+ return x
25
+
26
+ double_btn.click(double_value_and_max, a, a).then(
27
+ process_with_props, a, output_a
28
+ )
29
+
30
+ def reset(x: gr.Number):
31
+ x.maximum = 10
32
+ x.value = 5
33
+ x.info = "Enter a number between 0 and 10"
34
+ return x
35
+
36
+ reset_btn.click(reset, a, a).then(
37
+ process_with_props, a, output_a
38
+ )
39
+
40
+ # Image component demo
41
+ gr.Markdown("## Image Component Props")
42
+ b = gr.Image(value=get_image("cheetah.jpg"), label="Input Image", width=300, height=300, type="filepath")
43
+ output_b = gr.JSON(label="Image Props Output", elem_id="image-output")
44
+ with gr.Row():
45
+ show_image_props_btn = gr.Button("Show Image Props")
46
+ change_image_size_btn = gr.Button("Change Image Size")
47
+ reset_image_btn = gr.Button("Reset Image")
48
+
49
+ def show_image_props(x: gr.Image):
50
+ return {
51
+ "value": x.value if x.value is None else str(x.value),
52
+ "width": x.width,
53
+ "height": x.height,
54
+ "type": x.type,
55
+ }
56
+ show_image_props_btn.click(show_image_props, b, output_b)
57
+
58
+ def change_image_size(x: gr.Image):
59
+ x.width = 400
60
+ x.height = 400
61
+ return x
62
+
63
+ change_image_size_btn.click(change_image_size, b, b).then(
64
+ show_image_props, b, output_b
65
+ )
66
+
67
+ def reset_image(x: gr.Image):
68
+ x.width = 300
69
+ x.height = 300
70
+ x.value = get_image("cheetah.jpg")
71
+ return x
72
+
73
+ reset_image_btn.click(reset_image, b, b).then(
74
+ show_image_props, b, output_b
75
+ )
76
+
77
+
78
+ if __name__ == "__main__":
79
+ demo.launch()