Implementation:Bentoml BentoML Gradio Mount
| Knowledge Sources | |
|---|---|
| Domains | Web UI, ASGI |
| Last Updated | 2026-02-13 15:00 GMT |
Overview
Provides a decorator to mount a Gradio Blocks application onto a BentoML service as an ASGI sub-application at a specified URL path.
Description
The gradio module exposes the mount_gradio_app function, which acts as a class decorator for BentoML services. It takes a Gradio Blocks instance, configures it for production use (disabling dev mode, enabling error display, removing file size limits, setting root path and favicon), and creates a Gradio ASGI app via gr.routes.App.create_app. The resulting ASGI app is then mounted onto the service class using BentoML's asgi_app decorator.
The decorator supports two application orderings: it can be applied either before or after the @bentoml.service() decorator. When applied after (i.e., the object is already a Service instance), it accesses the inner class directly. When applied before, it stores Gradio app metadata on the class via the __bentoml_gradio_apps__ attribute for later mounting during Service initialization.
The module requires the gradio package and raises a MissingDependencyException if it is not installed.
Usage
Use this module to embed a Gradio web UI into a BentoML service, enabling interactive demos and user interfaces alongside model inference endpoints.
Code Reference
Source Location
- Repository: Bentoml_BentoML
- File: src/_bentoml_sdk/gradio.py
- Lines: 1-90
Signature
def mount_gradio_app(blocks: Blocks, path: str, name: str = "gradio_ui") -> Callable:
"""Mount a Gradio app to a BentoML service."""
...
Import
from _bentoml_sdk.gradio import mount_gradio_app
# or via public API:
import bentoml.gradio
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| blocks | gradio.Blocks | Yes | The Gradio Blocks instance to mount |
| path | str | Yes | The URL path prefix where Gradio will be served (must start with '/') |
| name | str | No | The name identifier for the mounted app (default: "gradio_ui") |
Outputs
| Name | Type | Description |
|---|---|---|
| decorator | Callable | A decorator that attaches the Gradio ASGI app to the BentoML service class or Service wrapper |
Usage Examples
import gradio as gr
import bentoml
blocks = gr.Blocks()
with blocks:
gr.Markdown("# My Model Demo")
inp = gr.Textbox(label="Input")
out = gr.Textbox(label="Output")
# Gradio decorator applied before @bentoml.service()
@bentoml.service()
@bentoml.gradio.mount_gradio_app(blocks, path="/ui")
class MyService:
@bentoml.api
def predict(self, text: str) -> str:
return text.upper()
# Alternatively, Gradio decorator applied after @bentoml.service()
@bentoml.gradio.mount_gradio_app(blocks, path="/ui")
@bentoml.service()
class MyService:
@bentoml.api
def predict(self, text: str) -> str:
return text.upper()