x = np.arange(start_year, final_year + 1) year_count = x.shape[0] plt_format = ({"cross": "X", "line": "-", "circle": "o--"})[point_style] fig = plt.figure() ax = fig.add_subplot(111) for i, company in enumerate(companies): series = np.arange(0, year_count, dtype=float) series = series**2 * (i + 1) series += np.random.rand(year_count) * noise ax.plot(x, series, plt_format) if show_legend: plt.legend(companies) plt.close() return fig demo = gr.Interface( stock_forecast, [ gr.Radio([2025, 2030, 2035, 2040], label="Project to:"), gr.CheckboxGroup(["Google", "Microsoft", "Gradio"]), gr.Slider(minimum=1, maximum=100), "checkbox", gr.Dropdown(["cross", "line", "circle"], label="Style"), ], gr.Image(plot=True, label="forecast"), ) if __name__ == "__main__": demo.launch()
import numpy as np import gradio as gr def sepia(input_img): sepia_filter = np.array( [[0.393, 0.769, 0.189], [0.349, 0.686, 0.168], [0.272, 0.534, 0.131]] ) sepia_img = input_img.dot(sepia_filter.T) sepia_img /= sepia_img.max() return sepia_img demo = gr.Interface(sepia, gr.Image(shape=(200, 200)), "image") if __name__ == "__main__": demo.launch()
import numpy as np import gradio as gr def snap(image): return np.flipud(image) demo = gr.Interface(snap, gr.Image(source="webcam", tool=None), "image") if __name__ == "__main__": demo.launch()
import tensorflow as tf import gradio as gr inception_net = tf.keras.applications.MobileNetV2() # load the model # Download human-readable labels for ImageNet. response = requests.get("https://git.io/JJkYN") labels = response.text.split("\n") def classify_image(inp): inp = inp.reshape((-1, 224, 224, 3)) inp = tf.keras.applications.mobilenet_v2.preprocess_input(inp) prediction = inception_net.predict(inp).flatten() return {labels[i]: float(prediction[i]) for i in range(1000)} image = gr.Image(shape=(224, 224)) label = gr.Label(num_top_classes=3) gr.Interface( fn=classify_image, inputs=image, outputs=label, examples=[ os.path.join(os.path.dirname(__file__), "images/cheetah1.jpg"), os.path.join(os.path.dirname(__file__), "images/lion.jpg") ] ).launch()
gr.Markdown( """ # Detect Disease From Scan With this model you can lorem ipsum - ipsum 1 - ipsum 2 """ ) disease = gr.CheckboxGroup( choices=["Covid", "Malaria", "Lung Cancer"], label="Disease to Scan For" ) with gr.Tabs(): with gr.TabItem("X-ray"): with gr.Row(): xray_scan = gr.Image() xray_results = gr.JSON() xray_run = gr.Button("Run") xray_progress = gr.StatusTracker(cover_container=True) xray_run.click( xray_model, inputs=[disease, xray_scan], outputs=xray_results, status_tracker=xray_progress, ) with gr.TabItem("CT Scan"): with gr.Row(): ct_scan = gr.Image() ct_results = gr.JSON() ct_run = gr.Button("Run")
theta = a / 180 * 3.14 tmax = ((2 * v) * np.sin(theta)) / g timemat = tmax * np.linspace(0, 1, 40)[:, None] x = (v * timemat) * np.cos(theta) y = ((v * timemat) * np.sin(theta)) - ((0.5 * g) * (timemat**2)) fig = plt.figure() plt.scatter(x=x, y=y, marker=".") plt.xlim(0, 100) plt.ylim(0, 60) return fig demo = gr.Blocks() with demo: gr.Markdown( "Let's do some kinematics! Choose the speed and angle to see the trajectory." ) with gr.Row(): speed = gr.Slider(25, min=1, max=30, label="Speed") angle = gr.Slider(45, min=0, max=90, label="Angle") output = gr.Image(type="plot") btn = gr.Button("Run") btn.click(plot, [speed, angle], output) if __name__ == "__main__": demo.launch()
import requests import torch from PIL import Image from torchvision import transforms import gradio as gr model = torch.hub.load("pytorch/vision:v0.6.0", "resnet18", pretrained=True).eval() # Download human-readable labels for ImageNet. response = requests.get("https://git.io/JJkYN") labels = response.text.split("\n") def predict(inp): inp = Image.fromarray(inp.astype("uint8"), "RGB") inp = transforms.ToTensor()(inp).unsqueeze(0) with torch.no_grad(): prediction = torch.nn.functional.softmax(model(inp)[0], dim=0) return {labels[i]: float(prediction[i]) for i in range(1000)} inputs = gr.Image() outputs = gr.Label(num_top_classes=3) demo = gr.Interface(fn=predict, inputs=inputs, outputs=outputs) if __name__ == "__main__": demo.launch()
demo = gr.Interface( fn, inputs=[ gr.Textbox(default_value="Lorem ipsum", label="Textbox"), gr.Textbox(lines=3, placeholder="Type here..", label="Textbox 2"), gr.Number(label="Number", default=42), gr.Slider(minimum=10, maximum=20, default_value=15, label="Slider: 10 - 20"), gr.Slider(maximum=20, step=0.04, label="Slider: step @ 0.04"), gr.Checkbox(label="Checkbox"), gr.CheckboxGroup( label="CheckboxGroup", choices=CHOICES, default_selected=CHOICES[0:2] ), gr.Radio(label="Radio", choices=CHOICES, default_selected=CHOICES[2]), gr.Dropdown(label="Dropdown", choices=CHOICES), gr.Image(label="Image"), gr.Image(label="Image w/ Cropper", tool="select"), gr.Image(label="Sketchpad", source="canvas"), gr.Image(label="Webcam", source="webcam"), gr.Video(label="Video"), gr.Audio(label="Audio"), gr.Audio(label="Microphone", source="microphone"), gr.File(label="File"), gr.Dataframe(label="Dataframe", headers=["Name", "Age", "Gender"]), gr.Timeseries(x="time", y=["price", "value"]), ], outputs=[ gr.Textbox(label="Textbox"), gr.Label(label="Label"), gr.Audio(label="Audio"), gr.Image(label="Image"),
import gradio as gr with gr.Blocks() as demo: txt = gr.Textbox(label="Small Textbox", lines=1) txt = gr.Textbox(label="Large Textbox", lines=5) num = gr.Number(label="Number") check = gr.Checkbox(label="Checkbox") check_g = gr.CheckboxGroup(label="Checkbox Group", choices=["One", "Two", "Three"]) radio = gr.Radio(label="Radio", choices=["One", "Two", "Three"]) drop = gr.Dropdown(label="Dropdown", choices=["One", "Two", "Three"]) slider = gr.Slider(label="Slider") audio = gr.Audio() video = gr.Video() image = gr.Image() ts = gr.Timeseries() df = gr.Dataframe() html = gr.HTML() json = gr.JSON() md = gr.Markdown() label = gr.Label() highlight = gr.HighlightedText() # layout components are static only # carousel doesn't work like other components # carousel = gr.Carousel() if __name__ == "__main__": demo.launch()
def fake_gan(*args): time.sleep(1) image = random.choice([ "https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=387&q=80", "https://images.unsplash.com/photo-1554151228-14d9def656e4?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=386&q=80", "https://images.unsplash.com/photo-1542909168-82c3e7fdca5c?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8aHVtYW4lMjBmYWNlfGVufDB8fDB8fA%3D%3D&w=1000&q=80", "https://images.unsplash.com/photo-1546456073-92b9f0a8d413?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=387&q=80", "https://images.unsplash.com/photo-1601412436009-d964bd02edbc?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=464&q=80", ]) return image demo = gr.Interface( fn=fake_gan, inputs=[ gr.Image(label="Initial Image (optional)"), gr.Slider(25, minimum=0, maximum=50, label="TV_scale (for smoothness)"), gr.Slider(25, minimum=0, maximum=50, label="Range_Scale (out of range RBG)"), gr.Number(label="Seed"), gr.Number(label="Respacing"), ], outputs=gr.Image(label="Generated Image"), title="FD-GAN", description= "This is a fake demo of a GAN. In reality, the images are randomly chosen from Unsplash.", )
import gradio import gradio as gr urlretrieve("https://gr-models.s3-us-west-2.amazonaws.com/mnist-model.h5", "mnist-model.h5") model = tf.keras.models.load_model("mnist-model.h5") def recognize_digit(image): image = image.reshape(1, -1) prediction = model.predict(image).tolist()[0] return {str(i): prediction[i] for i in range(10)} im = gradio.Image(shape=(28, 28), image_mode="L", invert_colors=False, source="canvas") demo = gr.Interface( recognize_digit, im, gradio.Label(num_top_classes=3), live=True, interpretation="default", capture_session=True, ) if __name__ == "__main__": demo.launch()