Go to file
pngwn 561579d9b7
fix-tests (#7345)
* fix-tests

* [tmp] Comment-out

* Fix the URL constructor calls in `resolve_wasm_src()`, `should_proxy_wasm_src()`, and `<DownloadLink />` to handle relative URLs

* Remove a circular dependency between lite/index.ts and lite/custom-element/index.ts to solve a bug that the dev app is mounted twice sometimes

* Fix js/app/test/image_component_events.spec.ts

* Set the `testIgnore` in `.config/playwright.config.js`

* Fix the Lite dev mode only to create an app and expose the controller for Playwright, without editors etc.

* add changeset

* Set the mocked ruff version as 0.2.2

* Extend timeout

* Fix to use the built lite files instead of the dev server

* add changeset

* comment out failed tests

* Revert "comment out failed tests"

This reverts commit 3580d79887.

* Fix the Gellery component to work in Wasm

* Fix js/app/test/file_explorer_component_events.spec.ts to run on Wasm

* Ignore queue_full_e2e_test.spec.ts

* Revert "[tmp] Comment-out"

This reverts commit c775c0cc29.

* Revert "Extend timeout"

This reverts commit 742d1e1e83.

* Remove a commented out line

* Refactor file_explorer_component_events.spec.ts

* Revert "fix-tests", restoring the original test-functional.yml content

This reverts commit 9ff2a7ddc5.

* Set CI step names

* [tmp] Revert "Revert "fix-tests", restoring the original test-functional.yml content"

This reverts commit de2dbe3317.

* Revert "[tmp] Revert "Revert "fix-tests", restoring the original test-functional.yml content""

This reverts commit 32154f3bb1.

* [tmp] Revert "Revert "[tmp] Revert "Revert "fix-tests", restoring the original test-functional.yml content"""

This reverts commit 204075e190.

* Fix vite.config.js removing unnecessary code

* Revert "Set the `testIgnore` in `.config/playwright.config.js`"

This reverts commit 98dccc5be9.

* Add gallery_component_events.spec.ts

* Revert js/app/test

* tweak

* tweak

* revert workflow changes

* add changeset

---------

Co-authored-by: Yuichiro Tachibana (Tsuchiya) <t.yic.yt@gmail.com>
Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
2024-03-04 14:03:46 +00:00
.changeset fix-tests (#7345) 2024-03-04 14:03:46 +00:00
.config fix-tests (#7345) 2024-03-04 14:03:46 +00:00
.devcontainer ensure login form has correct styles (#5324) 2023-08-24 16:42:28 +01:00
.github fix-tests (#7345) 2024-03-04 14:03:46 +00:00
.vscode when adding custom head html, ensure there are no duplicate meta tags (#7510) 2024-02-23 17:04:04 +00:00
client Update client.py: Docs typo (#7585) 2024-03-03 14:24:46 -08:00
demo Adds a gr.DownloadButton component (#7518) 2024-02-28 12:07:36 -08:00
gradio fix-tests (#7345) 2024-03-04 14:03:46 +00:00
guides Fixed redirects and reformatted code in creating-a-fast-chatbot guide (#7563) 2024-02-28 14:56:29 -08:00
js fix-tests (#7345) 2024-03-04 14:03:46 +00:00
readme_files Version 4 development branch (#5498) 2023-10-31 04:46:02 +00:00
scripts Improve File Explorer performance (#7337) 2024-02-13 16:51:47 -06:00
test Add /logout functionality for Gradio auth (#7547) 2024-02-27 14:53:24 -08:00
testing-guidelines fix typos in ci.md; labal -> label (#7533) 2024-02-26 15:05:56 +00:00
.dockerignore
.editorconfig Fix the wildcard in .editorconfig to match files in nested directories (#4165) 2023-05-11 10:29:28 -04:00
.git-blame-ignore-revs Add more reformatting commits to .git-blame-ignore-revs (#7066) 2024-01-18 20:49:31 +00:00
.gitignore Add py.typed to gradio backend (#6455) 2023-11-16 12:38:42 -08:00
build_pypi.sh Improve Embed and CDN handling and fix a couple of related bugs (#6261) 2023-11-02 22:29:03 +00:00
CHANGELOG.md chore: update versions (#7463) 2024-02-22 13:29:11 -08:00
CITATION.cff A new face for the Sultan of ML deployment (#1943) 2022-08-04 10:48:17 -07:00
CONTRIBUTING.md Adding instructions on how to build Gradio-Lite locally (#6870) 2023-12-27 00:38:52 -08:00
globals.d.ts generate docs json in ci, reimplement main vs release (#5092) 2023-08-11 15:54:56 +01:00
LICENSE
package.json chore(deps): update dependency chromatic to v11 (#7513) 2024-02-23 10:35:57 -08:00
pnpm-lock.yaml Adds a gr.DownloadButton component (#7518) 2024-02-28 12:07:36 -08:00
pnpm-workspace.yaml Version 4 development branch (#5498) 2023-10-31 04:46:02 +00:00
pyproject.toml Reverts the revert that dropped changes from 2 PRs (#7495) 2024-02-20 18:36:10 -08:00
readme_template.md Rewriting parts of the README and getting started guides for 4.0 (#6767) 2023-12-20 11:07:48 -08:00
README.md fix: typos (#7161) 2024-01-25 19:05:55 +00:00
render_readme.py Rewriting parts of the README and getting started guides for 4.0 (#6767) 2023-12-20 11:07:48 -08:00
renovate.json remove renovate (#6783) 2023-12-14 10:27:19 -08:00
requirements-oauth.txt Sign in with Hugging Face (OAuth support) (#4943) 2023-08-10 15:12:40 -04:00
requirements.txt chore: update versions (#7463) 2024-02-22 13:29:11 -08:00
SECURITY.md
style.md Make ci work (#7233) 2024-02-01 00:46:55 +00:00
tsconfig.json Ensure gr.CheckboxGroup updates as expected. (#6236) 2023-11-02 20:22:30 +00:00

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 a beautiful demo like the one above, so let's get started 💫

Installation

Prerequisite: Gradio requires Python 3.8 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 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 " * intensity + name + "!"

demo = gr.Interface(
    fn=greet,
    inputs=["text", "slider"],
    outputs=["text"],
)

demo.launch()

Tip

We shorten the imported name from gradio to gr for better readability of code. This is a widely adopted convention that you should follow so that anyone working with your code can easily understand it.

Now, run your code. If you've written the Python code in a file named, for example, 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. Learn more about hot reloading 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:

  • fn: the function to wrap a user interface (UI) around
  • inputs: the Gradio component(s) to use for the input. The number of components should match the number of arguments in your function.
  • outputs: the Gradio component(s) to use for the output. The number of components should match the number of return values from your function.

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 input and output 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 to build demos quickly with Gradio. But what else does Gradio do?

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 our dedicated guide on gr.ChatInterface.

Custom Demos with gr.Blocks

Gradio also offers a low-level approach for designing web apps with more flexible layouts and data flows with the gr.Blocks class. Blocks allows you to do things like control where components appear on the page, handle complex data flows (e.g. outputs can serve as inputs to other functions), and update 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.

The Gradio Python & JavaScript Ecosystem

That's the gist of the core gradio Python library, but Gradio is actually so much more! Its 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:

  • Gradio Python Client (gradio_client): query any Gradio app programmatically in Python.
  • Gradio JavaScript Client (@gradio/client): query any Gradio app programmatically in JavaScript.
  • Gradio-Lite (@gradio/lite): write Gradio apps in Python that run entirely in the browser (no server needed!), thanks to Pyodide.
  • Hugging Face Spaces: the most popular place to host Gradio applications — for free!

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: key features about Gradio demos.

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

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},
}