aliabd HF staff commited on
Commit
8d2d17c
·
1 Parent(s): da2759c

Upload with huggingface_hub

Browse files
Files changed (8) hide show
  1. README.md +5 -6
  2. img/a.jpg +0 -0
  3. img/b.jpg +0 -0
  4. mp4/a.mp4 +0 -0
  5. mp4/b.mp4 +0 -0
  6. requirements.txt +2 -0
  7. run.ipynb +1 -0
  8. run.py +142 -0
README.md CHANGED
@@ -1,12 +1,11 @@
 
1
  ---
2
- title: Event Trigger Main
3
  emoji: 🔥
4
- colorFrom: gray
5
- colorTo: blue
6
  sdk: gradio
7
  sdk_version: 3.17.0
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: event_trigger_main
4
  emoji: 🔥
5
+ colorFrom: indigo
6
+ colorTo: indigo
7
  sdk: gradio
8
  sdk_version: 3.17.0
9
+ app_file: run.py
10
  pinned: false
11
  ---
 
 
img/a.jpg ADDED
img/b.jpg ADDED
mp4/a.mp4 ADDED
Binary file (130 kB). View file
 
mp4/b.mp4 ADDED
Binary file (261 kB). View file
 
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+
2
+ https://gradio-main-build.s3.amazonaws.com/cd7370d9ebc18b7d02bc82aa8e2f23295c7993d5/gradio-3.17.0-py3-none-any.whl
run.ipynb ADDED
@@ -0,0 +1 @@
 
 
1
+ {"cells": [{"cell_type": "markdown", "id": 302934307671667531413257853548643485645, "metadata": {}, "source": ["# Gradio Demo: event_trigger"]}, {"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", "os.mkdir('img')\n", "!wget -q -O img/a.jpg https://github.com/gradio-app/gradio/raw/main/demo/event_trigger/img/a.jpg\n", "!wget -q -O img/b.jpg https://github.com/gradio-app/gradio/raw/main/demo/event_trigger/img/b.jpg\n", "os.mkdir('mp4')\n", "!wget -q -O mp4/a.mp4 https://github.com/gradio-app/gradio/raw/main/demo/event_trigger/mp4/a.mp4\n", "!wget -q -O mp4/b.mp4 https://github.com/gradio-app/gradio/raw/main/demo/event_trigger/mp4/b.mp4"]}, {"cell_type": "code", "execution_count": null, "id": 44380577570523278879349135829904343037, "metadata": {}, "outputs": [], "source": ["# %%\n", "import gradio as gr\n", "\n", "\n", "TEST_VIDEO_A = \"mp4/a.mp4\"\n", "TEST_VIDEO_B = \"mp4/b.mp4\"\n", "\n", "TEST_IMAGE_A = \"img/a.jpg\"\n", "TEST_IMAGE_B = \"img/b.jpg\"\n", "\n", "\n", "def alert_change(component, value):\n", " print(f\"Detected {component} change, {type(value)}\")\n", "\n", " if type(value) == list or type(value) == str:\n", " print(value)\n", "\n", "\n", "def change_interactive(state):\n", " return gr.update(interactive=not state), not state\n", "\n", "\n", "with gr.Blocks() as demo:\n", " with gr.Tab(label=\"Text change\"):\n", " with gr.Row():\n", " with gr.Column():\n", " textbox1 = gr.Textbox()\n", " textbox2 = gr.Textbox(interactive=True)\n", "\n", " with gr.Column():\n", " btn = gr.Button()\n", "\n", " def btn_click(state):\n", " return state\n", "\n", " def text_change(value):\n", " print(\"text_change\", value)\n", "\n", " btn.click(fn=btn_click, inputs=textbox1, outputs=textbox2)\n", " textbox2.change(fn=alert_change, inputs=[gr.State(\"Text\"), textbox2])\n", "\n", " with gr.Tab(label=\"Video change, play, pause\"):\n", " with gr.Row():\n", " with gr.Column():\n", " radio1 = gr.Radio(\n", " choices=[TEST_VIDEO_A, TEST_VIDEO_B],\n", " interactive=True,\n", " type=\"index\",\n", " )\n", "\n", " video_btn = gr.Button(\"Change interactive\")\n", "\n", " with gr.Column():\n", " video1 = gr.Video(value=TEST_VIDEO_A, interactive=False)\n", " video1_interactive = gr.State(value=False)\n", "\n", " def change_video(index):\n", " if index == 0:\n", " return TEST_VIDEO_A\n", " elif index == 1:\n", " return TEST_VIDEO_B\n", "\n", " def video_play():\n", " print(\"video_play\")\n", "\n", " def video_pause():\n", " print(\"video_pause\")\n", "\n", " def video_stop():\n", " print(\"video_stop\")\n", "\n", " video1.play(fn=video_play)\n", " video1.pause(fn=video_pause)\n", " video1.stop(fn=video_stop)\n", "\n", " radio1.change(fn=change_video, inputs=radio1, outputs=video1)\n", " video1.change(fn=alert_change, inputs=[gr.State(\"Video\"), video1])\n", "\n", " video_btn.click(\n", " fn=change_interactive,\n", " inputs=video1_interactive,\n", " outputs=[video1, video1_interactive],\n", " )\n", "\n", " with gr.Tab(label=\"Image change\"):\n", " with gr.Row():\n", " with gr.Column():\n", " radio2 = gr.Radio(\n", " choices=[TEST_IMAGE_A, TEST_IMAGE_B],\n", " interactive=True,\n", " type=\"index\",\n", " )\n", "\n", " with gr.Column():\n", " image1 = gr.Image(value=TEST_IMAGE_A, interactive=True)\n", "\n", " def change_image(index):\n", " if index == 0:\n", " return TEST_IMAGE_A\n", " elif index == 1:\n", " return TEST_IMAGE_B\n", "\n", " radio2.change(fn=change_image, inputs=radio2, outputs=image1)\n", " image1.change(fn=alert_change, inputs=[gr.State(\"Image\"), image1])\n", "\n", " with gr.Tab(label=\"File\"):\n", " with gr.Row():\n", " with gr.Column():\n", " radio3 = gr.Radio(\n", " choices=[\"A\", \"B\", \"AB\"],\n", " interactive=True,\n", " type=\"index\",\n", " )\n", "\n", " file_btn = gr.Button(\"Change interactive\")\n", "\n", " with gr.Column():\n", " file1 = gr.File(\n", " value=[TEST_IMAGE_A, TEST_IMAGE_B],\n", " interactive=False,\n", " file_count=\"multiple\",\n", " )\n", " file1_interactive = gr.State(value=False)\n", "\n", " def change_file(index):\n", " if index == 0:\n", " return [TEST_IMAGE_A]\n", " elif index == 1:\n", " return [TEST_IMAGE_A]\n", " elif index == 2:\n", " return [TEST_IMAGE_A, TEST_IMAGE_B]\n", "\n", " radio3.change(fn=change_file, inputs=radio3, outputs=file1)\n", " file1.change(fn=alert_change, inputs=[gr.State(\"File\"), file1])\n", "\n", " file_btn.click(\n", " fn=change_interactive,\n", " inputs=file1_interactive,\n", " outputs=[file1, file1_interactive],\n", " )\n", "\n", "demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
run.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # %%
2
+ import gradio as gr
3
+
4
+
5
+ TEST_VIDEO_A = "mp4/a.mp4"
6
+ TEST_VIDEO_B = "mp4/b.mp4"
7
+
8
+ TEST_IMAGE_A = "img/a.jpg"
9
+ TEST_IMAGE_B = "img/b.jpg"
10
+
11
+
12
+ def alert_change(component, value):
13
+ print(f"Detected {component} change, {type(value)}")
14
+
15
+ if type(value) == list or type(value) == str:
16
+ print(value)
17
+
18
+
19
+ def change_interactive(state):
20
+ return gr.update(interactive=not state), not state
21
+
22
+
23
+ with gr.Blocks() as demo:
24
+ with gr.Tab(label="Text change"):
25
+ with gr.Row():
26
+ with gr.Column():
27
+ textbox1 = gr.Textbox()
28
+ textbox2 = gr.Textbox(interactive=True)
29
+
30
+ with gr.Column():
31
+ btn = gr.Button()
32
+
33
+ def btn_click(state):
34
+ return state
35
+
36
+ def text_change(value):
37
+ print("text_change", value)
38
+
39
+ btn.click(fn=btn_click, inputs=textbox1, outputs=textbox2)
40
+ textbox2.change(fn=alert_change, inputs=[gr.State("Text"), textbox2])
41
+
42
+ with gr.Tab(label="Video change, play, pause"):
43
+ with gr.Row():
44
+ with gr.Column():
45
+ radio1 = gr.Radio(
46
+ choices=[TEST_VIDEO_A, TEST_VIDEO_B],
47
+ interactive=True,
48
+ type="index",
49
+ )
50
+
51
+ video_btn = gr.Button("Change interactive")
52
+
53
+ with gr.Column():
54
+ video1 = gr.Video(value=TEST_VIDEO_A, interactive=False)
55
+ video1_interactive = gr.State(value=False)
56
+
57
+ def change_video(index):
58
+ if index == 0:
59
+ return TEST_VIDEO_A
60
+ elif index == 1:
61
+ return TEST_VIDEO_B
62
+
63
+ def video_play():
64
+ print("video_play")
65
+
66
+ def video_pause():
67
+ print("video_pause")
68
+
69
+ def video_stop():
70
+ print("video_stop")
71
+
72
+ video1.play(fn=video_play)
73
+ video1.pause(fn=video_pause)
74
+ video1.stop(fn=video_stop)
75
+
76
+ radio1.change(fn=change_video, inputs=radio1, outputs=video1)
77
+ video1.change(fn=alert_change, inputs=[gr.State("Video"), video1])
78
+
79
+ video_btn.click(
80
+ fn=change_interactive,
81
+ inputs=video1_interactive,
82
+ outputs=[video1, video1_interactive],
83
+ )
84
+
85
+ with gr.Tab(label="Image change"):
86
+ with gr.Row():
87
+ with gr.Column():
88
+ radio2 = gr.Radio(
89
+ choices=[TEST_IMAGE_A, TEST_IMAGE_B],
90
+ interactive=True,
91
+ type="index",
92
+ )
93
+
94
+ with gr.Column():
95
+ image1 = gr.Image(value=TEST_IMAGE_A, interactive=True)
96
+
97
+ def change_image(index):
98
+ if index == 0:
99
+ return TEST_IMAGE_A
100
+ elif index == 1:
101
+ return TEST_IMAGE_B
102
+
103
+ radio2.change(fn=change_image, inputs=radio2, outputs=image1)
104
+ image1.change(fn=alert_change, inputs=[gr.State("Image"), image1])
105
+
106
+ with gr.Tab(label="File"):
107
+ with gr.Row():
108
+ with gr.Column():
109
+ radio3 = gr.Radio(
110
+ choices=["A", "B", "AB"],
111
+ interactive=True,
112
+ type="index",
113
+ )
114
+
115
+ file_btn = gr.Button("Change interactive")
116
+
117
+ with gr.Column():
118
+ file1 = gr.File(
119
+ value=[TEST_IMAGE_A, TEST_IMAGE_B],
120
+ interactive=False,
121
+ file_count="multiple",
122
+ )
123
+ file1_interactive = gr.State(value=False)
124
+
125
+ def change_file(index):
126
+ if index == 0:
127
+ return [TEST_IMAGE_A]
128
+ elif index == 1:
129
+ return [TEST_IMAGE_A]
130
+ elif index == 2:
131
+ return [TEST_IMAGE_A, TEST_IMAGE_B]
132
+
133
+ radio3.change(fn=change_file, inputs=radio3, outputs=file1)
134
+ file1.change(fn=alert_change, inputs=[gr.State("File"), file1])
135
+
136
+ file_btn.click(
137
+ fn=change_interactive,
138
+ inputs=file1_interactive,
139
+ outputs=[file1, file1_interactive],
140
+ )
141
+
142
+ demo.launch()