abidlabs HF Staff commited on
Commit
a375bc3
·
verified ·
1 Parent(s): 2fa0549

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +161 -0
app.py ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+
4
+ import numpy as np
5
+
6
+ import gradio as gr
7
+
8
+ CHOICES = ["foo", "bar", "baz"]
9
+ JSONOBJ = """{"items":{"item":[{"id": "0001","type": null,"is_good": false,"ppu": 0.55,"batters":{"batter":[{ "id": "1001", "type": "Regular" },{ "id": "1002", "type": "Chocolate" },{ "id": "1003", "type": "Blueberry" },{ "id": "1004", "type": "Devil's Food" }]},"topping":[{ "id": "5001", "type": "None" },{ "id": "5002", "type": "Glazed" },{ "id": "5005", "type": "Sugar" },{ "id": "5007", "type": "Powdered Sugar" },{ "id": "5006", "type": "Chocolate with Sprinkles" },{ "id": "5003", "type": "Chocolate" },{ "id": "5004", "type": "Maple" }]}]}}"""
10
+
11
+
12
+ def fn(
13
+ text1,
14
+ text2,
15
+ num,
16
+ slider1,
17
+ slider2,
18
+ single_checkbox,
19
+ checkboxes,
20
+ radio,
21
+ dropdown,
22
+ multi_dropdown,
23
+ im1,
24
+ # im2,
25
+ # im3,
26
+ im4,
27
+ video,
28
+ audio1,
29
+ audio2,
30
+ file,
31
+ df1,
32
+ ):
33
+ return (
34
+ (text1 if single_checkbox else text2)
35
+ + ", selected:"
36
+ + ", ".join(checkboxes), # Text
37
+ {
38
+ "positive": num / (num + slider1 + slider2),
39
+ "negative": slider1 / (num + slider1 + slider2),
40
+ "neutral": slider2 / (num + slider1 + slider2),
41
+ }, # Label
42
+ (audio1[0], np.flipud(audio1[1]))
43
+ if audio1 is not None
44
+ else os.path.join(os.path.dirname(__file__), "files/cantina.wav"), # Audio
45
+ np.flipud(im1)
46
+ if im1 is not None
47
+ else os.path.join(os.path.dirname(__file__), "files/cheetah1.jpg"), # Image
48
+ video
49
+ if video is not None
50
+ else os.path.join(os.path.dirname(__file__), "files/world.mp4"), # Video
51
+ [
52
+ ("The", "art"),
53
+ ("quick brown", "adj"),
54
+ ("fox", "nn"),
55
+ ("jumped", "vrb"),
56
+ ("testing testing testing", None),
57
+ ("over", "prp"),
58
+ ("the", "art"),
59
+ ("testing", None),
60
+ ("lazy", "adj"),
61
+ ("dogs", "nn"),
62
+ (".", "punc"),
63
+ ]
64
+ + [(f"test {x}", f"test {x}") for x in range(10)], # HighlightedText
65
+ # [("The testing testing testing", None), ("quick brown", 0.2), ("fox", 1), ("jumped", -1), ("testing testing testing", 0), ("over", 0), ("the", 0), ("testing", 0), ("lazy", 1), ("dogs", 0), (".", 1)] + [(f"test {x}", x/10) for x in range(-10, 10)], # HighlightedText
66
+ [
67
+ ("The testing testing testing", None),
68
+ ("over", 0.6),
69
+ ("the", 0.2),
70
+ ("testing", None),
71
+ ("lazy", -0.1),
72
+ ("dogs", 0.4),
73
+ (".", 0),
74
+ ]
75
+ + [(f"test", x / 10) for x in range(-10, 10)], # HighlightedText
76
+ json.loads(JSONOBJ), # JSON
77
+ "<button style='background-color: red'>Click Me: "
78
+ + radio
79
+ + "</button>", # HTML
80
+ os.path.join(os.path.dirname(__file__), "files/titanic.csv"),
81
+ df1, # Dataframe
82
+ np.random.randint(0, 10, (4, 4)), # Dataframe
83
+ )
84
+
85
+
86
+ demo = gr.Interface(
87
+ fn,
88
+ inputs=[
89
+ gr.Textbox(value="Lorem ipsum", label="Textbox"),
90
+ gr.Textbox(lines=3, placeholder="Type here..", label="Textbox 2"),
91
+ gr.Number(label="Number", value=42),
92
+ gr.Slider(10, 20, value=15, label="Slider: 10 - 20"),
93
+ gr.Slider(maximum=20, step=0.04, label="Slider: step @ 0.04"),
94
+ gr.Checkbox(label="Checkbox"),
95
+ gr.CheckboxGroup(label="CheckboxGroup", choices=CHOICES, value=CHOICES[0:2]),
96
+ gr.Radio(label="Radio", choices=CHOICES, value=CHOICES[2]),
97
+ gr.Dropdown(label="Dropdown", choices=CHOICES),
98
+ gr.Dropdown(
99
+ label="Multiselect Dropdown (Max choice: 2)",
100
+ choices=CHOICES,
101
+ multiselect=True,
102
+ max_choices=2,
103
+ ),
104
+ gr.Image(label="Image"),
105
+ # gr.Image(label="Image w/ Cropper", tool="select"),
106
+ # gr.Image(label="Sketchpad", source="canvas"),
107
+ gr.Image(label="Webcam", sources=["webcam"]),
108
+ gr.Video(label="Video"),
109
+ gr.Audio(label="Audio"),
110
+ gr.Audio(label="Microphone", sources=["microphone"]),
111
+ gr.File(label="File"),
112
+ gr.Dataframe(label="Dataframe", headers=["Name", "Age", "Gender"]),
113
+ ],
114
+ outputs=[
115
+ gr.Textbox(label="Textbox"),
116
+ gr.Label(label="Label"),
117
+ gr.Audio(label="Audio"),
118
+ gr.Image(label="Image"),
119
+ gr.Video(label="Video"),
120
+ gr.HighlightedText(
121
+ label="HighlightedText", color_map={"punc": "pink", "test 0": "blue"}
122
+ ),
123
+ gr.HighlightedText(label="HighlightedText", show_legend=True),
124
+ gr.JSON(label="JSON"),
125
+ gr.HTML(label="HTML"),
126
+ gr.File(label="File"),
127
+ gr.Dataframe(label="Dataframe"),
128
+ gr.Dataframe(label="Numpy"),
129
+ ],
130
+ examples=[
131
+ [
132
+ "the quick brown fox",
133
+ "jumps over the lazy dog",
134
+ 10,
135
+ 12,
136
+ 4,
137
+ True,
138
+ ["foo", "baz"],
139
+ "baz",
140
+ "bar",
141
+ ["foo", "bar"],
142
+ os.path.join(os.path.dirname(__file__), "files/cheetah1.jpg"),
143
+ # os.path.join(os.path.dirname(__file__), "files/cheetah1.jpg"),
144
+ # os.path.join(os.path.dirname(__file__), "files/cheetah1.jpg"),
145
+ os.path.join(os.path.dirname(__file__), "files/cheetah1.jpg"),
146
+ os.path.join(os.path.dirname(__file__), "files/world.mp4"),
147
+ os.path.join(os.path.dirname(__file__), "files/cantina.wav"),
148
+ os.path.join(os.path.dirname(__file__), "files/cantina.wav"),
149
+ os.path.join(os.path.dirname(__file__), "files/titanic.csv"),
150
+ [[1, 2, 3, 4], [4, 5, 6, 7], [8, 9, 1, 2], [3, 4, 5, 6]],
151
+ ]
152
+ ]
153
+ * 3,
154
+ title="Kitchen Sink",
155
+ description="Try out all the components!",
156
+ article="Learn more about [Gradio](http://gradio.app)",
157
+ cache_examples=True,
158
+ )
159
+
160
+ if __name__ == "__main__":
161
+ demo.launch()