- Routes
- mount_gradio_app
mount_gradio_app
gradio.mount_gradio_app(···)描述
将 gradio.Blocks 挂载到现有的 FastAPI 应用程序。
示例用法
from fastapi import FastAPI
import gradio as gr
app = FastAPI()
@app.get("/")
def read_main():
return {"message": "This is your main app"}
io = gr.Interface(lambda x: "Hello, " + x + "!", "textbox", "textbox")
app = gr.mount_gradio_app(app, io, path="/gradio")然后在终端中运行 uvicorn run:app,并导航到 https://:8000/gradio。
初始化
app_kwargs: dict[str, Any] | None
app_kwargs: dict[str, Any] | None= None要传递给底层 FastAPI 应用程序的附加关键字参数,作为参数键和参数值的字典。例如,`{"docs_url": "/docs"}`
auth: Callable | tuple[str, str] | list[tuple[str, str]] | None
auth: Callable | tuple[str, str] | list[tuple[str, str]] | None= None如果提供,访问 Gradio 应用程序所需的用户名和密码(或用户名-密码元组列表)。也可以提供一个函数,该函数接受用户名和密码,并在登录有效时返回 True。
auth_dependency: Callable[[fastapi.Request], str | None] | None
auth_dependency: Callable[[fastapi.Request], str | None] | None= None一个接受 FastAPI 请求并返回字符串用户 ID 或 None 的函数。如果此函数对特定请求返回 None,则该用户无权访问 Gradio 应用程序(他们将看到 401 Unauthorized 响应)。用于外部身份验证系统(如 OAuth)。不能与 `auth` 一起使用。
root_path: str | None
root_path: str | None= None与此 FastAPI 应用程序的公共部署相对应的子路径。例如,如果应用程序在 "https://example.com/myapp" 上提供服务,则 `root_path` 应设置为 "/myapp"。可以提供以 http:// 或 https:// 开头的完整 URL,这将全部使用。通常,这不需要提供(即使您使用自定义 `path`)。但是,如果您在代理后面提供 FastAPI 应用程序服务,代理可能不会在请求头中提供 Gradio 应用程序的完整路径。在这种情况下,您可以在此处提供根路径。
allowed_paths: list[str] | None
allowed_paths: list[str] | None= None此 Gradio 应用程序允许提供服务的完整文件路径或父目录列表。必须是绝对路径。警告:如果您提供目录,这些目录或其子目录中的任何文件都可以被应用程序的所有用户访问。
blocked_paths: list[str] | None
blocked_paths: list[str] | None= None此 Gradio 应用程序不允许提供服务的完整文件路径或父目录列表(即,应用程序的用户无权访问)。必须是绝对路径。警告:优先于 `allowed_paths` 和 Gradio 默认公开的所有其他目录。
show_error: bool
show_error: bool= True如果为 True,Gradio 应用程序中的任何错误将显示在警报模态框中并打印在浏览器控制台日志中。否则,错误将仅在运行 Gradio 应用程序的终端会话中可见。
max_file_size: str | int | None
max_file_size: str | int | None= None可以上传的最大文件大小(以字节为单位)。可以是一个形式为 `
ssr_mode: bool | None
ssr_mode: bool | None= None如果为 True,Gradio 应用程序将以服务器端渲染模式渲染,这通常性能更高并提供更好的 SEO,但这需要系统上安装 Node 20+。如果为 False,应用程序将以客户端渲染模式渲染。如果为 None,将使用 GRADIO_SSR_MODE 环境变量或默认为 False。
node_server_name: str | None
node_server_name: str | None= None用于 SSR 的 Node 服务器名称。如果为 None,将使用 GRADIO_NODE_SERVER_NAME 环境变量或在系统中搜索 node 二进制文件。
mcp_server: bool | None
mcp_server: bool | None= None如果为 True,MCP 服务器将在 Gradio 应用程序上启动。如果为 None,将使用 GRADIO_MCP_SERVER 环境变量或默认为 False。
theme: Theme | str | None
theme: Theme | str | None= NoneTheme 对象或表示主题的字符串。如果为字符串,将查找具有该名称的内置主题(例如“soft”或“default”),或者将尝试从 Hugging Face Hub 加载主题(例如“gradio/monochrome”)。如果为 None,将使用 Default 主题。
css_paths: str | Path | list[str | Path] | None
css_paths: str | Path | list[str | Path] | None= None自定义 CSS 作为 pathlib.Path 到 CSS 文件或此类路径的列表。这些 CSS 文件将被读取、连接并包含在演示网页中。如果还设置了 `css` 参数,则 `css` 中的 CSS 将首先包含。