Gradio: Build and Share Machine Learning Apps in Python

Summary
Gradio is an open-source Python library that simplifies the creation and sharing of interactive web applications for machine learning models, APIs, or any Python function. It allows developers to quickly build user interfaces without needing JavaScript, CSS, or web hosting expertise, offering a straightforward way to demo AI projects. With Gradio, you can transform your Python functions into shareable web demos in just a few lines of code.
Repository Info
Tags
Click on any tag to explore related repositories
Introduction
Gradio is an open-source Python library designed to simplify the creation and sharing of interactive web applications for machine learning models, APIs, or any arbitrary Python function. It empowers developers to build delightful UIs for their AI projects quickly, without requiring extensive knowledge of JavaScript, CSS, or web hosting. With Gradio, you can transform your Python functions into shareable web demos in just a few lines of code.
Installation
To get started with Gradio, you need Python 3.10 or higher. The recommended way to install Gradio is using pip:
pip install --upgrade gradio
It is highly recommended to install Gradio within a virtual environment. Detailed installation instructions for various operating systems are available in the official guides.
Examples
Gradio makes it incredibly easy to build your first interactive demo. Here's a simple example demonstrating how to create a basic greeting application:
import gradio as gr
def greet(name, intensity):
return "Hello, " + name + "!" * int(intensity)
demo = gr.Interface(
fn=greet,
inputs=["text", "slider"],
outputs=["text"],
)
demo.launch()
This code creates a web interface where users can input text and adjust a slider, then see a dynamic greeting. The gr.Interface class is central to this, allowing you to wrap any Python function with a UI by defining its inputs and outputs.
You can also easily share your demo with the world by setting share=True when launching:
import gradio as gr
def greet(name):
return "Hello " + name + "!"
demo = gr.Interface(fn=greet, inputs="textbox", outputs="textbox")
demo.launch(share=True) # Share your demo with just 1 extra parameter ?
This will generate a publicly accessible URL, allowing anyone to interact with your application running locally on your machine.
Why Use Gradio?
Gradio offers several compelling reasons for developers working with machine learning and AI:
- Simplicity and Speed: Build interactive demos for your models with minimal Python code, eliminating the need for front-end development skills.
- Instant Sharing: Easily generate public links to your demos, making it simple to share your work for feedback, testing, or showcasing.
- Flexibility: Beyond the high-level
gr.Interfacefor quick demos, Gradio providesgr.Blocksfor creating custom layouts and complex data flows, andgr.ChatInterfacespecifically for building chatbots. - Rich Component Library: Access over 30 built-in UI components tailored for machine learning applications, from textboxes and sliders to image and audio inputs.
- Ecosystem Integration: Seamlessly integrate with Hugging Face Spaces for free hosting, and utilize Python and JavaScript clients (
gradio_client,@gradio/client) to programmatically interact with Gradio apps. - No-Code Options: Explore
gradio sketchfor building applications visually without writing code.
Links
- Official Website: https://gradio.app
- Documentation: https://gradio.app/docs/
- GitHub Repository: https://github.com/gradio-app/gradio
- Guides: https://gradio.app/guides/
- PyPI: https://pypi.org/project/gradio/
- Discord: https://discord.com/invite/feTf9x3ZSB