gradio README

The project's README from github.com/gradio-app/gradio. As one markdown file: README.md.

gradio
Gradio 5.0 - the easiest way to build AI web apps | Product Hunt gradio-app%2Fgradio | Trendshift

gradio-backend gradio-ui PyPI PyPI downloads Python version Twitter follow

Website | Documentation | Guides | Getting Started | Examples

English | 中文

Gradio: Build Machine Learning Web Apps — in Python

Gradio is an open-source Python package that allows you to quickly build a demo or web application for your machine learning model, API, or any arbitrary Python function. You can then share a link to your demo or web application in just a few seconds using Gradio's built-in sharing features. No JavaScript, CSS, or web hosting experience needed!

It just takes a few lines of Python to create your own demo, so let's get started 💫

Installation

Prerequisite: Gradio requires Python 3.10 or higher.

We recommend installing Gradio using pip, which is included by default in Python. Run this in your terminal or command prompt:

pip install --upgrade gradio

[!TIP] It is best to install Gradio in a virtual environment. Detailed installation instructions for all common operating systems are provided here.

Building Your First Demo

You can run Gradio in your favorite code editor, Jupyter notebook, Google Colab, or anywhere else you write Python. Let's write your first Gradio app:

import 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()

[!TIP] We shorten the imported name from gradio to gr. This is a widely adopted convention for better readability of code.

Now, run your code. If you've written the Python code in a file named app.py, then you would run python app.py from the terminal.

The demo below will open in a browser on http://localhost:7860 if running from a file. If you are running within a notebook, the demo will appear embedded within the notebook.

hello_world_4 demo

Type your name in the textbox on the left, drag the slider, and then press the Submit button. You should see a friendly greeting on the right.

[!TIP] When developing locally, you can run your Gradio app in hot reload mode, which automatically reloads the Gradio app whenever you make changes to the file. To do this, simply type in gradio before the name of the file instead of python. In the example above, you would type: gradio app.py in your terminal. You can also enable vibe mode by using the --vibe flag, e.g. gradio --vibe app.py, which provides an in-browser chat that can be used to write or edit your Gradio app using natural language. Learn more in the Hot Reloading Guide.

Understanding the Interface Class

You'll notice that in order to make your first demo, you created an instance of the gr.Interface class. The Interface class is designed to create demos for machine learning models which accept one or more inputs, and return one or more outputs.

The Interface class has three core arguments:

The fn argument is very flexible -- you can pass any Python function that you want to wrap with a UI. In the example above, we saw a relatively simple function, but the function could be anything from a music generator to a tax calculator to the prediction function of a pretrained machine learning model.

The inputs and outputs arguments take one or more Gradio components. As we'll see, Gradio includes more than 30 built-in components (such as the gr.Textbox(), gr.Image(), and gr.HTML() components) that are designed for machine learning applications.

[!TIP] For the inputs and outputs arguments, you can pass in the name of these components as a string ("textbox") or an instance of the class (gr.Textbox()).

If your function accepts more than one argument, as is the case above, pass a list of input components to inputs, with each input component corresponding to one of the arguments of the function, in order. The same holds true if your function returns more than one value: simply pass in a list of components to outputs. This flexibility makes the Interface class a very powerful way to create demos.

We'll dive deeper into the gr.Interface on our series on building Interfaces.

Sharing Your Demo

What good is a beautiful demo if you can't share it? Gradio lets you easily share a machine learning demo without having to worry about the hassle of hosting on a web server. Simply set share=True in launch(), and a publicly accessible URL will be created for your demo. Let's revisit our example demo, but change the last line as follows:

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 🚀

When you run this code, a public URL will be generated for your demo in a matter of seconds, something like:

👉   https://a23dsf231adb.gradio.live

Now, anyone around the world can try your Gradio demo from their browser, while the machine learning model and all computation continues to run locally on your computer.

To learn more about sharing your demo, read our dedicated guide on sharing your Gradio application.

An Overview of Gradio

So far, we've been discussing the Interface class, which is a high-level class that lets you build demos quickly with Gradio. But what else does Gradio include?

Custom Demos with gr.Blocks

Gradio offers a low-level approach for designing web apps with more customizable layouts and data flows with the gr.Blocks class. Blocks supports things like controlling where components appear on the page, handling multiple data flows and more complex interactions (e.g. outputs can serve as inputs to other functions), and updating properties/visibility of components based on user interaction — still all in Python.

You can build very custom and complex applications using gr.Blocks(). For example, the popular image generation Automatic1111 Web UI is built using Gradio Blocks. We dive deeper into the gr.Blocks on our series on building with Blocks.

Chatbots with gr.ChatInterface

Gradio includes another high-level class, gr.ChatInterface, which is specifically designed to create Chatbot UIs. Similar to Interface, you supply a function and Gradio creates a fully working Chatbot UI. If you're interested in creating a chatbot, you can jump straight to our dedicated guide on gr.ChatInterface.

The Gradio Python & JavaScript Ecosystem

That's the gist of the core gradio Python library, but Gradio is actually so much more! It's an entire ecosystem of Python and JavaScript libraries that let you build machine learning applications, or query them programmatically, in Python or JavaScript. Here are other related parts of the Gradio ecosystem:

What's Next?

Keep learning about Gradio sequentially using the Gradio Guides, which include explanations as well as example code and embedded interactive demos. Next up: let's dive deeper into the Interface class.

Or, if you already know the basics and are looking for something specific, you can search the more technical API documentation.

AI Coding Skills

Gradio provides a "skill" that enriches AI coding assistants (like Cursor, Claude Code, Codex, etc.) with Gradio-specific knowledge, so that they can build Gradio apps more effectively. This is especially useful when creating custom Gradio components or styling. Install the Gradio skill for coding assistants with a single command:

gradio skills add

This installs to the shared .agents/skills directory used by Codex, Cursor, OpenCode, and other compatible agents. Use --global to install at the user level (applies to all projects), or an agent flag such as --claude to also create a link in that agent's skills directory.

You can also install a skill for a specific Gradio Space, which generates API usage docs (Python, JS, cURL) on the fly:

gradio skills add abidlabs/en2fr

Questions?

If you'd like to report a bug or have a feature request, please create an issue on GitHub. For general questions about usage, we are available on our Discord server and happy to help.

If you like Gradio, please leave us a ⭐ on GitHub!

Open Source Stack

Gradio is built on top of many wonderful open-source libraries!

huggingface python fastapi encode svelte vite pnpm tailwind storybook chromatic

License

Gradio is licensed under the Apache License 2.0 found in the LICENSE file in the root directory of this repository.

Citation

Also check out the paper Gradio: Hassle-Free Sharing and Testing of ML Models in the Wild, ICML HILL 2019, and please cite it if you use Gradio in your work.

@article{abid2019gradio,
  title = {Gradio: Hassle-Free Sharing and Testing of ML Models in the Wild},
  author = {Abid, Abubakar and Abdalla, Ali and Abid, Ali and Khan, Dawood and Alfozan, Abdulrahman and Zou, James},
  journal = {arXiv preprint arXiv:1906.02569},
  year = {2019},
}

gradio (gradio-app/gradio)

Searching Database…

43.8K ·GitHub
这是什么?Gradio 是一个开源的 Python 包,只需几行 Python 代码,就能把机器学习模型、API 或任意 Python 函数变成一个可以分享的演示页面或网页应用。
Gradio 是我导入的库,还是一个托管服务?是一个需要导入的 Python 包——它提供 gr.Interface、gr.Blocks 和 gr.ChatInterface 等类,用 30 多个现成的组件(比如 gr.Textbox() 和 gr.Image())就能搭建网页界面。
一个完整的 Gradio 应用从头到尾是什么样子的?先 pip install gradio,然后用 gr.Interface 把一个函数包起来,指定输入和输出组件,再运行 python app.py——演示页面就会在浏览器里打开;如果你用的是笔记本环境(notebook),它会直接嵌在单元格里显示。
写第一个应用之前,我需要装好什么?只需要 Python 3.10 或更新版本;Gradio 用 pip 安装,而 pip 是随 Python 一起自带的,README 还建议在虚拟环境里安装。
如果我分享一个演示,它会在网上保留多久?只在你的脚本运行期间有效:分享会生成一个公开网址,但模型和所有计算都继续在你自己的电脑上运行,所以你一关掉,演示也就下线了。
我需要有网页开发经验吗?不需要——README 就是面向 Python 用户的:做出一个能用的演示,不需要懂 JavaScript、CSS 或网站托管。
不管我用什么操作系统,它都能在我电脑上运行吗?Gradio 在所有常见操作系统上都能安装,而且它生成的应用是网页应用,所以试用你演示的人只要有个浏览器就行。
我可以把自己的产品里用上 Gradio 吗?可以——它基于 Apache License 2.0 开源,许可证全文放在仓库根目录的 LICENSE 文件里。
gr.Interface、gr.Blocks 和 gr.ChatInterface 有什么区别?Interface 是给函数包上输入和输出的高层用法;ChatInterface 是做聊天机器人界面的快捷方式;Blocks 是底层类,用来做自定义布局和数据流——Automatic1111 的图像生成 Web UI 就是用它搭建的。
每次改完都要重新运行 python app.py,有没有更顺手的办法?有——gradio app.py 会以热重载模式运行应用,你一改动文件它就自动重新加载;gradio --vibe app.py 还会在浏览器里加一个聊天窗口,可以用自然语言让它帮你编写或修改应用。
不想自己管服务器的话,大家一般把 Gradio 应用部署在哪里?Hugging Face Spaces——README 称它是托管 Gradio 应用最流行的地方,而且托管是免费的。
不用浏览器,其他代码也能调用 Gradio 应用吗?可以——官方提供了 Python 和 JavaScript(@gradio/client)的客户端,可以用编程方式调用任何 Gradio 应用。
Creator
gradio-app
Size
122.9 MB
Stars
43.8K
Age
7y
Commits · 90 Days