Gradio · 用 Python 快速构建机器学习 Web 应用|安装、构建与分享全指南
项目仓库: https://github.com/gradio-app/gradio
开源协议:Apache-2.0 · 定位:用 Python 为 ML 模型 / API / 任意 Python 函数快速构建 Web Demo · 要求:Python 3.10+ · 本地端口:7860 · 官方文档:gradio.app/docs
1 项目简介
Gradio 是一个开源 Python 包,让你快速为机器学习模型、API 或任意 Python 函数构建 Demo / Web 应用,几行代码即可,并用内置分享功能在几秒内生成公开链接。无需 JavaScript、CSS 或 Web 托管经验。
1.1 核心信息一览
| 项目项 | 内容 |
|---|---|
| 项目名称 | Gradio |
| 仓库地址 | github.com/gradio-app/gradio |
| 项目定位 | Python ML Web 应用构建框架 |
| 开源协议 | Apache-2.0 |
| Python 要求 | 3.10+ |
| 本地端口 | 7860 |
| 官方文档 | gradio.app/docs |
1.2 适用场景
- 把 ML 模型快速变成可交互 Web Demo 给业务方试用。
- 在 Jupyter / Colab / 服务器上一键起 UI,并用
share=True生成临时公网链接。 - 复杂 UI 用
gr.Blocks自定义布局;Chatbot 用gr.ChatInterface。
1.3 能力边界(官方口径)
- 本地运行后浏览器打开
http://localhost:7860。 share=True生成的临时*.gradio.live链接在代码运行期间有效。
2 功能特性
gr.Interface:高层封装,三参数 fn / inputs / outputs。gr.Blocks:低层自定义布局与数据流(如 Automatic1111 Web UI)。gr.ChatInterface:Chatbot UI 一键生成。- 30+ 内置组件:Textbox、Image、HTML 等。
- 热重载:
gradio app.py;--vibe自然语言编辑。 - 生态:
gradio_client(Python)、@gradio/client(JS)、Hugging Face Spaces、Server 模式。
3 官方界面预览
Gradio 生成的 Web 界面:左侧输入组件、右侧输出组件,自带 API 与分享按钮。
4 官方跳转链接汇总
| 类别 | 链接 | 说明 |
|---|---|---|
| 仓库主页 | https://github.com/gradio-app/gradio | 源码 |
| 官网 | https://gradio.app | 主页 |
| 文档 | gradio.app/docs | API |
| 指南 | gradio.app/guides | 教程 |
| Getting Started | gradio.app/getting_started | 入门 |
| PyPI | pypi.org/project/gradio | 包 |
5 前置要求与服务器配置
5.1 软件前提
- Python 3.10+;建议虚拟环境。
5.2 硬件建议
- 跑模型的机器按需配置;纯 UI 演示 1C1G 即可。
5.3 端口与资源
- 默认 7860;生产部署需反代并设置鉴权。
6 方式一:pip 安装与启动(推荐)
pip install --upgrade gradioimport gradio as gr
def greet(name, intensity):
return "Hello, " + name + "!" * int(intensity)
demo = gr.Interface(
fn=greet,
inputs=["text", "slider"],
outputs=["text"],
api_name="predict"
)
demo.launch()python app.py # 浏览器打开 http://localhost:7860
gradio app.py # 热重载7 方式二:分享与部署
demo.launch(share=True) # 生成 https://xxxx.gradio.live 临时链接生产部署到 Hugging Face Spaces 或 Server
模式(gradio.Server)。
8 配置项说明
launch():share、server_name、server_port、auth等。Interface:fn/inputs/outputs。
9 常用运维命令
- 升级:
pip install --upgrade gradio。 - AI coding skill:
gradio skills add。
10 常见故障排查
- 端口被占:改
server_port。 - 模型加载慢:检查 GPU / 依赖。
11 风险与合规说明
- 许可证:Apache-2.0。
share=True临时链接暴露公网,生产环境勿长期使用。- 本文整理自官方 README,命令以官方文档最新说明为准;不包含 star 数等动态数据。