hmb HF Staff commited on
Commit
9308a7a
·
verified ·
1 Parent(s): c550626

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
+ def fn(
12
+ text1,
13
+ text2,
14
+ num,
15
+ slider1,
16
+ slider2,
17
+ single_checkbox,
18
+ checkboxes,
19
+ radio,
20
+ dropdown,
21
+ multi_dropdown,
22
+ im1,
23
+ # im2,
24
+ # im3,
25
+ im4,
26
+ video,
27
+ audio1,
28
+ audio2,
29
+ file,
30
+ df1,
31
+ ):
32
+ return (
33
+ (text1 if single_checkbox else text2)
34
+ + ", selected:"
35
+ + ", ".join(checkboxes), # Text
36
+ {
37
+ "positive": num / (num + slider1 + slider2),
38
+ "negative": slider1 / (num + slider1 + slider2),
39
+ "neutral": slider2 / (num + slider1 + slider2),
40
+ }, # Label
41
+ (audio1[0], np.flipud(audio1[1]))
42
+ if audio1 is not None
43
+ else os.path.join(os.path.dirname(__file__), "files/cantina.wav"), # Audio
44
+ np.flipud(im1)
45
+ if im1 is not None
46
+ else os.path.join(os.path.dirname(__file__), "files/cheetah1.jpg"), # Image
47
+ [
48
+ ("The", "art"),
49
+ ("quick brown", "adj"),
50
+ ("fox", "nn"),
51
+ ("jumped", "vrb"),
52
+ ("testing testing testing", None),
53
+ ("over", "prp"),
54
+ ("the", "art"),
55
+ ("testing", None),
56
+ ("lazy", "adj"),
57
+ ("dogs", "nn"),
58
+ (".", "punc"),
59
+ ]
60
+ + [(f"test {x}", f"test {x}") for x in range(10)], # HighlightedText
61
+ # [("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
62
+ [
63
+ ("The testing testing testing", None),
64
+ ("over", 0.6),
65
+ ("the", 0.2),
66
+ ("testing", None),
67
+ ("lazy", -0.1),
68
+ ("dogs", 0.4),
69
+ (".", 0),
70
+ ]
71
+ + [("test", x / 10) for x in range(-10, 10)], # HighlightedText
72
+ video
73
+ if video is not None
74
+ else os.path.join(os.path.dirname(__file__), "files/world.mp4"), # Video
75
+ json.loads(JSONOBJ), # JSON
76
+ "<button style='background-color: red'>Click Me: "
77
+ + radio
78
+ + "</button>", # HTML
79
+ os.path.join(os.path.dirname(__file__), "files/titanic.csv"),
80
+ df1, # Dataframe
81
+ np.random.randint(0, 10, (4, 4)), # Dataframe
82
+ )
83
+
84
+ demo = gr.Interface(
85
+ fn,
86
+ css_paths=["windows95.css"],
87
+ theme=gr.themes.Windows95(),
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", elem_id="output-img"),
119
+ gr.HighlightedText(
120
+ label="HighlightedText", color_map={"punc": "pink", "test 0": "blue"}
121
+ ),
122
+ gr.HighlightedText(label="HighlightedText", show_legend=True),
123
+ gr.Video(label="Video"),
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()