Python library for easily interacting with trained machine learning models
npx @tessl/cli install tessl/pypi-gradio@5.44.0Gradio is an open-source Python framework that enables developers to quickly build and deploy web applications for machine learning models, APIs, and arbitrary Python functions. It provides a simple, declarative interface for creating interactive demos and web apps without requiring JavaScript, CSS, or web hosting expertise.
pip install gradioimport gradio as grCommon pattern for building interfaces:
import gradio as gr
# For simple function interfaces
interface = gr.Interface(...)
# For custom layouts and interactions
with gr.Blocks() as demo:
# Add components and define interactions
passimport gradio as gr
def greet(name):
return f"Hello {name}!"
# Create interface automatically
interface = gr.Interface(
fn=greet,
inputs="text",
outputs="text",
title="Greeting App"
)
interface.launch()import gradio as gr
def process_text(text):
return text.upper()
with gr.Blocks() as demo:
gr.Markdown("# Text Processor")
with gr.Row():
input_text = gr.Textbox(label="Input")
output_text = gr.Textbox(label="Output")
submit_btn = gr.Button("Process")
submit_btn.click(
fn=process_text,
inputs=input_text,
outputs=output_text
)
demo.launch()import gradio as gr
def chatbot_response(message, history):
return f"You said: {message}"
chat_interface = gr.ChatInterface(
fn=chatbot_response,
title="Simple Chatbot"
)
chat_interface.launch()Gradio follows a component-based architecture with several key concepts:
Interface, Blocks, ChatInterface) that manage the overall app structure and serve web interfacesTextbox, Button, Image, etc.) that handle data display and user interactionRow, Column, Tabs) that organize component positioning and visual structureState and session handlingThis architecture enables rapid prototyping of ML interfaces while supporting production deployment with authentication, real-time updates, and cloud integration.
Foundation classes for building different types of Gradio applications, from simple function wrappers to complex multi-component interfaces.
class Interface:
def __init__(self, fn, inputs, outputs, **kwargs): ...
def launch(self, **kwargs): ...
class Blocks:
def __enter__(self): ...
def __exit__(self, *args): ...
def launch(self, **kwargs): ...
class ChatInterface:
def __init__(self, fn, **kwargs): ...
def launch(self, **kwargs): ...
class TabbedInterface:
def __init__(self, interface_list, tab_names, **kwargs): ...
def launch(self, **kwargs): ...Comprehensive set of UI components for handling different data types including text, media files, forms, and specialized interfaces.
class Textbox:
def __init__(self, value=None, label=None, **kwargs): ...
class Button:
def __init__(self, value=None, **kwargs): ...
def click(self, fn, inputs=None, outputs=None, **kwargs): ...
class Image:
def __init__(self, value=None, **kwargs): ...
class Audio:
def __init__(self, value=None, **kwargs): ...
class File:
def __init__(self, value=None, **kwargs): ...
class Slider:
def __init__(self, minimum=0, maximum=100, **kwargs): ...
class Dropdown:
def __init__(self, choices=None, **kwargs): ...Container components for organizing UI elements into structured layouts with responsive design support.
class Row:
def __enter__(self): ...
def __exit__(self, *args): ...
class Column:
def __enter__(self): ...
def __exit__(self, *args): ...
class Tabs:
def __enter__(self): ...
def __exit__(self, *args): ...
class Tab:
def __init__(self, label, **kwargs): ...
def __enter__(self): ...
def __exit__(self, *args): ...Reactive event system for connecting user interactions to Python functions with comprehensive event data and dependency management.
class EventData:
def __init__(self, target, data): ...
def on(triggers, fn, inputs=None, outputs=None, **kwargs): ...
class SelectData(EventData): ...
class KeyUpData(EventData): ...
class LikeData(EventData): ...Comprehensive theming system with predefined themes and utilities for customizing visual appearance and branding.
class Theme:
def __init__(self, **kwargs): ...
class Base(Theme): ...
class Default(Theme): ...
class Glass(Theme): ...
class Monochrome(Theme): ...
class Color:
def __init__(self, *args, **kwargs): ...
class Font:
def __init__(self, *args, **kwargs): ...Support functions and classes for progress tracking, notifications, examples, and integration with external systems.
class Progress:
def __init__(self, track_tqdm=False): ...
def __call__(self, iterable): ...
class Examples:
def __init__(self, examples, inputs, **kwargs): ...
def Info(message): ...
def Warning(message): ...
def Success(message): ...
def update(**kwargs): ...
def skip(): ...Functions for loading models from Hugging Face, integrating with existing web frameworks, and working with external APIs.
def load(name, **kwargs): ...
def load_chat(name, **kwargs): ...
def load_openapi(url, **kwargs): ...
def mount_gradio_app(app, gradio_app, path): ...
class Request:
def __init__(self, request): ...