[![CircleCI](https://circleci.com/gh/gradio-app/gradio.svg?style=svg)](https://circleci.com/gh/gradio-app/gradio) [![PyPI version](https://badge.fury.io/py/gradio.svg)](https://badge.fury.io/py/gradio) [![codecov](https://codecov.io/gh/gradio-app/gradio/branch/master/graph/badge.svg?token=NNVPX9KEGS)](https://codecov.io/gh/gradio-app/gradio) [![PyPI - Downloads](https://img.shields.io/pypi/dm/gradio)](https://pypi.org/project/gradio/) [![Twitter Follow](https://img.shields.io/twitter/follow/gradio.svg?style=social&label=Follow)](https://twitter.com/gradio) # Welcome to Gradio Quickly create customizable UI components around your models. Gradio makes it easy for you to "play around" with your model in your browser by dragging-and-dropping in your own images, pasting your own text, recording your own voice, etc. and seeing what the model outputs. ![Interface montage](demo/screenshots/montage.gif) Gradio is useful for: * Creating demos of your machine learning code for clients / collaborators / users * Getting feedback on model performance from users * Debugging your model interactively during development ## Getting Started You can find an interactive version of this README at [https://gradio.app/getting_started](https://gradio.app/getting_started). ### Quick Start To get Gradio running with a simple example, follow these three steps: 1. Install Gradio from pip. ```bash pip install gradio ``` 2. Run the code below as a Python script or in a Python notebook (or in a [colab notebook](https://colab.research.google.com/drive/18ODkJvyxHutTN0P5APWyGFO_xwNcgHDZ?usp=sharing)). ```python import gradio as gr def greet(name): return "Hello " + name + "!!" iface = gr.Interface(fn=greet, inputs="text", outputs="text") iface.launch() ``` 3. The interface below will appear automatically within the Python notebook, or pop in a browser on [http://localhost:7860](http://localhost:7860/) if running from a script. ![hello_world interface](demo/screenshots/hello_world/1.gif) ### The Interface Gradio can wrap almost any Python function with an easy to use interface. That function could be anything from a simple tax calculator to a pretrained model. The core `Interface` class is initialized with three parameters: - `fn`: the function to wrap - `inputs`: the input component type(s) - `outputs`: the output component type(s) With these three arguments, we can quickly create interfaces and `launch()` them. But what if you want to change how the UI components look or behave? ### Customizable Components What if we wanted to customize the input text field - for example, we wanted it to be larger and have a text hint? If we use the actual input class for `Textbox` instead of using the string shortcut, we have access to much more customizability. To see a list of all the components we support and how you can customize them, check out the [Docs](https://gradio.app/docs) ```python import gradio as gr def greet(name): return "Hello " + name + "!" iface = gr.Interface( fn=greet, inputs=gr.inputs.Textbox(lines=2, placeholder="Name Here..."), outputs="text") iface.launch() ``` ![hello_world_2 interface](demo/screenshots/hello_world_2/1.gif) ### Multiple Inputs and Outputs Let's say we had a much more complex function, with multiple inputs and outputs. In the example below, we have a function that takes a string, boolean, and number, and returns a string and number. Take a look how we pass a list of input and output components. ```python import gradio as gr def greet(name, is_morning, temperature): salutation = "Good morning" if is_morning else "Good evening" greeting = "%s %s. It is %s degrees today" % ( salutation, name, temperature) celsius = (temperature - 32) * 5 / 9 return greeting, round(celsius, 2) iface = gr.Interface( fn=greet, inputs=["text", "checkbox", gr.inputs.Slider(0, 100)], outputs=["text", "number"]) iface.launch() ``` ![hello_world_3 interface](demo/screenshots/hello_world_3/1.gif) We simply wrap the components in a list. Furthermore, if we wanted to compare multiple functions that have the same input and return types, we can even pass a list of functions for quick comparison. ### Working with Images Let's try an image to image function. When using the `Image` component, your function will receive a numpy array of your specified size, with the shape `(width, height, 3)`, where the last dimension represents the RGB values. We'll return an image as well in the form of a numpy array. ```python import gradio as gr import numpy as np def sepia(input_img): sepia_filter = np.array([[.393, .769, .189], [.349, .686, .168], [.272, .534, .131]]) sepia_img = input_img.dot(sepia_filter.T) sepia_img /= sepia_img.max() return sepia_img iface = gr.Interface(sepia, gr.inputs.Image(shape=(200, 200)), "image") iface.launch() ``` ![sepia_filter interface](demo/screenshots/sepia_filter/1.gif) Additionally, our `Image` input interface comes with an 'edit' button which opens tools for cropping, flipping, rotating, drawing over, and applying filters to images. We've found that manipulating images in this way will often reveal hidden flaws in a model. In addition to images, Gradio supports other media input types, such as audio or video uploads. Read about these in the [Docs](https://gradio.app/docs). ### Working with Data You can use Gradio to support inputs and outputs from your typical data libraries, such as numpy arrays, pandas dataframes, and plotly graphs. Take a look at the demo below (ignore the complicated data manipulation in the function!) ```python import gradio as gr import numpy as np import pandas as pd import matplotlib.pyplot as plt def sales_projections(employee_data): sales_data = employee_data.iloc[:, 1:4].astype("int").to_numpy() regression_values = np.apply_along_axis(lambda row: np.array(np.poly1d(np.polyfit([0,1,2], row, 2))), 0, sales_data) projected_months = np.repeat(np.expand_dims( np.arange(3,12), 0), len(sales_data), axis=0) projected_values = np.array([ month * month * regression[0] + month * regression[1] + regression[2] for month, regression in zip(projected_months, regression_values)]) plt.plot(projected_values.T) plt.legend(employee_data["Name"]) return employee_data, plt.gcf(), regression_values iface = gr.Interface(sales_projections, gr.inputs.Dataframe( headers=["Name", "Jan Sales", "Feb Sales", "Mar Sales"], default=[["Jon", 12, 14, 18], ["Alice", 14, 17, 2], ["Sana", 8, 9.5, 12]] ), [ "dataframe", "plot", "numpy" ], description="Enter sales figures for employees to predict sales trajectory over year." ) iface.launch() ``` ![sales_projections interface](demo/screenshots/sales_projections/1.gif) ### Example Inputs You can provide example data that a user can easily load into the model. This can be helpful to demonstrate the types of inputs the model expects, as well as to provide a way to explore your dataset in conjunction with your model. To load example data, you provide a **nested list** to the `examples=` keyword argument of the Interface constructor. Each sublist within the outer list represents a data sample, and each element within the sublist represents an input for each input component. The format of example data for each component is specified in the [Docs](https://gradio.app/docs). ```python import gradio as gr def calculator(num1, operation, num2): if operation == "add": return num1 + num2 elif operation == "subtract": return num1 - num2 elif operation == "multiply": return num1 * num2 elif operation == "divide": return num1 / num2 iface = gr.Interface(calculator, ["number", gr.inputs.Radio(["add", "subtract", "multiply", "divide"]), "number"], "number", examples=[ [5, "add", 3], [4, "divide", 2], [-4, "multiply", 2.5], [0, "subtract", 1.2], ], title="test calculator", description="heres a sample toy calculator. enjoy!", flagging_options=["this", "or", "that"], ) iface.launch() ``` ![calculator interface](demo/screenshots/calculator/1.gif) You can load a large dataset into the examples to browse and interact with the dataset through Gradio. The examples will be automatically paginated (you can configure this through the `examples_per_page` argument of Interface) and you can use CTRL + arrow keys to navigate through the examples quickly. ### Live Interfaces You can make interfaces automatically responsive by setting `live=True` in the interface. Now the interface will recalculate as soon as the user input. ```python import gradio as gr def calculator(num1, operation, num2): if operation == "add": return num1 + num2 elif operation == "subtract": return num1 - num2 elif operation == "multiply": return num1 * num2 elif operation == "divide": return num1 / num2 iface = gr.Interface(calculator, ["number", gr.inputs.Radio(["add", "subtract", "multiply", "divide"]), "number"], "number", live=True ) iface.launch() ``` ![calculator_live interface](demo/screenshots/calculator_live/1.gif) Note there is no submit button, because the interface resubmits automatically on change. ### Using State Your function may use data that persists beyond a single function call. If the data is something accessible to all function calls, you can create a global variable outside the function call and access it inside the function. For example, you may load a large model outside the function and use it inside the function so that every function call does not need to reload the model. Another type of data persistence Gradio supports is session state, where data persists across multiple submits within a page load. To store data with this permanence, use `gr.get_state` and `gr.set_state` methods. ```python import gradio as gr import random def chat(message): history = gr.get_state() or [] if message.startswith("How many"): response = random.randint(1,10) elif message.startswith("How"): response = random.choice(["Great", "Good", "Okay", "Bad"]) elif message.startswith("Where"): response = random.choice(["Here", "There", "Somewhere"]) else: response = "I don't know" history.append((message, response)) gr.set_state(history) html = "