"Although Gradio reads inputs as strings, type manipulation allows you to perform and display an even wider range of functions. Run the cell below and observe how it takes two numbers as string inputs and outputs a string displaying the sum of the two numbers."
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "399ff8c3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running on local URL: http://127.0.0.1:7863\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
## Gradio: How you can build a GUI within a Jupyter Notebook
#### By Team JACT
%% Cell type:markdown id: tags:
## GUI Background
A graphical user interface (GUI) is an interface through which a user interacts with electronic devices such as computers and smartphones through the use of icons, menus and other visual indicators or representations (graphics). GUIs graphically display information and related user controls, unlike text-based interfaces, where data and commands are strictly in text. GUI representations are manipulated by a pointing device such as a mouse, trackball, stylus, or by a finger on a touch screen.
First, the gradio library must be installed on your computer. It requires Python 3.7 or later. If you have not done so already, please check your version of Python and run the following line of code:
%% Cell type:code id:cdfdbb3e tags:
``` python
!python--version
```
%% Output
Python 3.8.2
%% Cell type:markdown id:b8de36df tags:
On your terminal,
%% Cell type:code id:8f5edb3b tags:
``` python
#!pip install gradio
```
%% Cell type:markdown id: tags:
Next, import the library as follows:
%% Cell type:code id:227461d8 tags:
``` python
importgradioasgr
```
%% Cell type:markdown id: tags:
Gradio can be used with a wide range of media-text, pictures, video, and sound. It is most useful for demonstrating machine learning algorithms.
To get a feel for how it works, run the cell below this one. An interface will automatically pop up within the Jupyter Notebook. You can type your input directing into the interface.
To create a public link, set `share=True` in `launch()`.
%% Cell type:markdown id:5f8fff86 tags:
Although Gradio reads inputs as strings, type manipulation allows you to perform and display an even wider range of functions. Run the cell below and observe how it takes two numbers as string inputs and outputs a string displaying the sum of the two numbers.
To create a public link, set `share=True` in `launch()`.
%% Cell type:markdown id: tags:
### The Interface
The core interface has three parameters:
1. fn: The function.
2. inputs: The input component.
3. outputs: The output component.
With these components, you can quickly create and launch an interface.
%% Cell type:markdown id:d4bd4273 tags:
### Adding additional Inputs:
Suppose you had a more complex function, with multiple inputs and outputs. In the example below, we define a function that takes a string, boolean, and number, and returns a string and number. Take a look how you pass a list of input and output components.
greeting=f"{salutation}{name}. It is {temperature} degrees today"
celsius=(temperature-32)*5/9
returngreeting,round(celsius,2)
demo=gr.Interface(
fn=greet,
inputs=["text","checkbox",gr.Slider(0,100)],
outputs=["text","number"],
)
demo.launch()
```
%% Output
Running on local URL: http://127.0.0.1:7861
To create a public link, set `share=True` in `launch()`.
%% Cell type:markdown id:46e5071f tags:
### Uses of Gradio
%% Cell type:markdown id: tags:
Gradio can load in data, similar to pandas frames, by using the command `gradio.inputs.Dataframe(data_name)`. It can only take in strings, numbers, bools, and dates as data types. Gradio does not contain a library of datasets, so data must be input by the user. It can also work with time series, images, audio, video, and generic file uploads.
Applications:
- Machine learning interface
- Image classification.
- Text generation interface (e.g. ChatBot).
- Audio and video editing
- Reverse audio files.
- Flip video files.
- Using machine learning, gradio can detect the main note in an inputted audio file.
- File Outputs:
- Zip files directly within Python.
- Output your data in various file formats including JSON, HTML, PNG, etc.
- Using a function called `.Carousel()`, Gradio can output a set of components that can be easily scrolled through.
%% Cell type:markdown id: tags:
An example of a question could be, what is the conclusion of the sentence: Today will be a good day.
A user could use Gardio to create an input field, then use the information from that field to generate a response. For example, the input could be sent to a text generation model such as GPT-2 to generate a response.
For a visual from data, you could create a cat/dog image classification model and Gradio demo to upload new images for class prediction. The model will be a Keras convolutional neural network (CNN) that would be trained on images of cats and dogs as features and their class names as labels
%% Cell type:markdown id: tags:
How this data or tool could be used in some of the team projects (maybe not your own)