Path: blob/master/Applied Data Modelling using Gradio/1.2 Gradio Basics.ipynb
7216 views
Kernel: Python (TensorFlow 3.10)
Gradio is a Python library used to quickly build interactive web UIs for ML models or functions. It wraps your Python function into a browser-based app with inputs and outputs.
A Gradio app = Function (logic) + Inputs + Outputs + Interface wrapper
Step-by-Step Implementation
1. Import a install Gradio App
In [1]:
2. Define a Function
This is your core logic.
Example: Simple Addition
In [2]:
3. Create Interface
In [4]:
4. Launch App
In [5]:
Out[5]:
* Running on local URL: http://127.0.0.1:7864
* To create a public link, set `share=True` in `launch()`.
Key Parameters of gr.Interface
fn (Function) The backend logic Must accept inputs → return outputs
Inputs : Defines UI components:
| Type | Example |
|---|---|
| "text" | Text input |
| "number" | Numeric input |
| "slider" | Range input |
| "image" | Image upload |
In [6]:
outputs: Defines output display
| Type | Example |
|---|---|
| "text" | Text result |
| "number" | Numeric output |
| "label" | Classification |
| "image" | Image output |
title & description
In [7]:
Out[7]:
Gradio Interface for: add_numbers
---------------------------------
inputs:
|-<gradio.components.number.Number object at 0x000002AD52E0FC10>
|-<gradio.components.number.Number object at 0x000002AD515AB050>
outputs:
|-<gradio.components.number.Number object at 0x000002AD52E8A050>
Example: ML Model (Churn Prediction)
In [8]:
Out[8]:
* Running on local URL: http://127.0.0.1:7865
* To create a public link, set `share=True` in `launch()`.
Advanced Components
Using Sliders
In [9]:
Using Dropdown
In [10]:
Out[10]:
<gradio.components.dropdown.Dropdown at 0x2ad52ff7cd0>
Using Image Input
Using gr.Blocks (Advanced UI)
In [12]:
Out[12]:
* Running on local URL: http://127.0.0.1:7866
* To create a public link, set `share=True` in `launch()`.
Deployment Options
Local (launch())
Shareable link (launch(share=True))
Deploy on:
Hugging Face Spaces
Cloud (AWS / Azure / GCP)
Summary
gr.Interface → Quick UI wrapper
fn → Logic
inputs/outputs → UI structure
launch() → Run app
In [ ]: