2022-01-04 01:46:07 +08:00
|
|
|
import json
|
2021-05-24 02:08:06 +08:00
|
|
|
|
2022-01-21 21:44:12 +08:00
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
import gradio as gr
|
|
|
|
|
2021-05-24 02:08:06 +08:00
|
|
|
CHOICES = ["foo", "bar", "baz"]
|
2022-01-04 01:46:07 +08:00
|
|
|
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" }]}]}}"""
|
2021-05-24 02:08:06 +08:00
|
|
|
|
2022-01-21 21:44:12 +08:00
|
|
|
|
|
|
|
def fn(
|
|
|
|
text1,
|
|
|
|
text2,
|
|
|
|
num,
|
|
|
|
slider1,
|
|
|
|
slider2,
|
|
|
|
single_checkbox,
|
|
|
|
checkboxes,
|
|
|
|
radio,
|
|
|
|
dropdown,
|
|
|
|
im1,
|
|
|
|
im2,
|
|
|
|
im3,
|
|
|
|
im4,
|
|
|
|
video,
|
|
|
|
audio1,
|
|
|
|
audio2,
|
|
|
|
file,
|
|
|
|
df1,
|
|
|
|
df2,
|
|
|
|
):
|
2021-05-24 02:08:06 +08:00
|
|
|
return (
|
2022-01-21 21:44:12 +08:00
|
|
|
(text1 if single_checkbox else text2)
|
|
|
|
+ ", selected:"
|
|
|
|
+ ", ".join(checkboxes), # Text
|
2021-05-24 02:08:06 +08:00
|
|
|
{
|
2021-10-01 02:20:54 +08:00
|
|
|
"positive": num / (num + slider1 + slider2),
|
|
|
|
"negative": slider1 / (num + slider1 + slider2),
|
|
|
|
"neutral": slider2 / (num + slider1 + slider2),
|
|
|
|
}, # Label
|
2022-01-21 21:44:12 +08:00
|
|
|
(audio1[0], np.flipud(audio1[1]))
|
|
|
|
if audio1 is not None
|
|
|
|
else "files/cantina.wav", # Audio
|
2021-12-17 18:52:19 +08:00
|
|
|
np.flipud(im1) if im1 is not None else "files/cheetah1.jpg", # Image
|
2022-01-04 01:46:07 +08:00
|
|
|
video if video is not None else "files/world.mp4", # Video
|
2022-01-21 21:44:12 +08:00
|
|
|
[
|
|
|
|
("The", "art"),
|
|
|
|
("quick brown", "adj"),
|
|
|
|
("fox", "nn"),
|
|
|
|
("jumped", "vrb"),
|
|
|
|
("testing testing testing", None),
|
|
|
|
("over", "prp"),
|
|
|
|
("the", "art"),
|
|
|
|
("testing", None),
|
|
|
|
("lazy", "adj"),
|
|
|
|
("dogs", "nn"),
|
|
|
|
(".", "punc"),
|
|
|
|
]
|
|
|
|
+ [(f"test {x}", f"test {x}") for x in range(10)], # HighlightedText
|
2022-01-04 01:46:07 +08:00
|
|
|
# [("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
|
2022-01-21 21:44:12 +08:00
|
|
|
[
|
|
|
|
("The testing testing testing", None),
|
|
|
|
("over", 0.6),
|
|
|
|
("the", 0.2),
|
|
|
|
("testing", None),
|
|
|
|
("lazy", -0.1),
|
|
|
|
("dogs", 0.4),
|
|
|
|
(".", 0),
|
|
|
|
]
|
|
|
|
+ [(f"test", x / 10) for x in range(-10, 10)], # HighlightedText
|
2022-01-04 01:46:07 +08:00
|
|
|
json.loads(JSONOBJ), # JSON
|
2022-01-21 21:44:12 +08:00
|
|
|
"<button style='background-color: red'>Click Me: "
|
|
|
|
+ radio
|
|
|
|
+ "</button>", # HTML
|
2021-10-01 02:20:54 +08:00
|
|
|
"files/titanic.csv",
|
2021-12-30 16:20:28 +08:00
|
|
|
df1, # Dataframe
|
2022-01-21 21:44:12 +08:00
|
|
|
np.random.randint(0, 10, (4, 4)), # Dataframe
|
|
|
|
[
|
|
|
|
im for im in [im1, im2, im3, im4, "files/cheetah1.jpg"] if im is not None
|
|
|
|
], # Carousel
|
|
|
|
df2, # Timeseries
|
2021-05-24 02:08:06 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
iface = gr.Interface(
|
|
|
|
fn,
|
|
|
|
inputs=[
|
2021-10-01 02:20:54 +08:00
|
|
|
gr.inputs.Textbox(default="Lorem ipsum", label="Textbox"),
|
2022-02-09 02:56:13 +08:00
|
|
|
gr.inputs.Textbox(lines=3, placeholder="Type here..", label="Textbox 2"),
|
2021-05-24 02:08:06 +08:00
|
|
|
gr.inputs.Number(label="Number", default=42),
|
2022-02-09 02:56:13 +08:00
|
|
|
gr.inputs.Slider(minimum=10, maximum=20, default=15, label="Slider: 10 - 20"),
|
2021-12-30 16:20:28 +08:00
|
|
|
gr.inputs.Slider(maximum=20, step=0.04, label="Slider: step @ 0.04"),
|
2021-05-24 02:08:06 +08:00
|
|
|
gr.inputs.Checkbox(label="Checkbox"),
|
2022-01-21 21:44:12 +08:00
|
|
|
gr.inputs.CheckboxGroup(
|
|
|
|
label="CheckboxGroup", choices=CHOICES, default=CHOICES[0:2]
|
|
|
|
),
|
2021-10-01 02:20:54 +08:00
|
|
|
gr.inputs.Radio(label="Radio", choices=CHOICES, default=CHOICES[2]),
|
2021-05-24 02:08:06 +08:00
|
|
|
gr.inputs.Dropdown(label="Dropdown", choices=CHOICES),
|
2021-09-03 05:01:28 +08:00
|
|
|
gr.inputs.Image(label="Image", optional=True),
|
2022-02-09 02:56:13 +08:00
|
|
|
gr.inputs.Image(label="Image w/ Cropper", tool="select", optional=True),
|
2021-10-01 02:20:54 +08:00
|
|
|
gr.inputs.Image(label="Sketchpad", source="canvas", optional=True),
|
|
|
|
gr.inputs.Image(label="Webcam", source="webcam", optional=True),
|
2021-09-03 05:01:28 +08:00
|
|
|
gr.inputs.Video(label="Video", optional=True),
|
|
|
|
gr.inputs.Audio(label="Audio", optional=True),
|
2022-02-09 02:56:13 +08:00
|
|
|
gr.inputs.Audio(label="Microphone", source="microphone", optional=True),
|
2021-09-03 05:01:28 +08:00
|
|
|
gr.inputs.File(label="File", optional=True),
|
2022-02-09 02:56:13 +08:00
|
|
|
gr.inputs.Dataframe(label="Dataframe", headers=["Name", "Age", "Gender"]),
|
2021-12-30 16:20:28 +08:00
|
|
|
gr.inputs.Timeseries(x="time", y=["price", "value"], optional=True),
|
2021-05-24 02:08:06 +08:00
|
|
|
],
|
|
|
|
outputs=[
|
2021-12-21 09:20:29 +08:00
|
|
|
gr.outputs.Textbox(label="Textbox"),
|
|
|
|
gr.outputs.Label(label="Label"),
|
|
|
|
gr.outputs.Audio(label="Audio"),
|
|
|
|
gr.outputs.Image(label="Image"),
|
|
|
|
gr.outputs.Video(label="Video"),
|
2022-02-09 02:56:13 +08:00
|
|
|
gr.outputs.HighlightedText(
|
|
|
|
label="HighlightedText", color_map={"punc": "pink", "test 0": "blue"}
|
|
|
|
),
|
2022-01-04 01:46:07 +08:00
|
|
|
gr.outputs.HighlightedText(label="HighlightedText", show_legend=True),
|
2021-12-21 09:20:29 +08:00
|
|
|
gr.outputs.JSON(label="JSON"),
|
|
|
|
gr.outputs.HTML(label="HTML"),
|
|
|
|
gr.outputs.File(label="File"),
|
|
|
|
gr.outputs.Dataframe(label="Dataframe"),
|
2022-01-04 01:46:07 +08:00
|
|
|
gr.outputs.Dataframe(label="Numpy", type="numpy"),
|
2021-12-21 09:20:29 +08:00
|
|
|
gr.outputs.Carousel("image", label="Carousel"),
|
2022-02-09 02:56:13 +08:00
|
|
|
gr.outputs.Timeseries(x="time", y=["price", "value"], label="Timeseries"),
|
2021-05-24 02:08:06 +08:00
|
|
|
],
|
2021-12-30 16:20:28 +08:00
|
|
|
examples=[
|
2022-01-21 21:44:12 +08:00
|
|
|
[
|
|
|
|
"the quick brown fox",
|
|
|
|
"jumps over the lazy dog",
|
|
|
|
10,
|
|
|
|
12,
|
|
|
|
4,
|
|
|
|
True,
|
|
|
|
["foo", "baz"],
|
|
|
|
"baz",
|
|
|
|
"bar",
|
|
|
|
"files/cheetah1.jpg",
|
|
|
|
"files/cheetah1.jpg",
|
|
|
|
"files/cheetah1.jpg",
|
|
|
|
"files/cheetah1.jpg",
|
|
|
|
"files/world.mp4",
|
|
|
|
"files/cantina.wav",
|
|
|
|
"files/cantina.wav",
|
|
|
|
"files/titanic.csv",
|
|
|
|
[[1, 2, 3], [3, 4, 5]],
|
|
|
|
"files/time.csv",
|
|
|
|
]
|
|
|
|
]
|
|
|
|
* 3,
|
2022-01-27 10:33:49 +08:00
|
|
|
theme="default",
|
2021-10-21 06:16:09 +08:00
|
|
|
title="Kitchen Sink",
|
|
|
|
description="Try out all the components!",
|
2022-01-21 21:44:12 +08:00
|
|
|
article="Learn more about [Gradio](http://gradio.app)",
|
2021-05-24 02:08:06 +08:00
|
|
|
)
|
|
|
|
|
2021-06-30 03:04:22 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
iface.launch()
|