Package: gradio
Website|Documentation|Guides|Getting Started|Examples|中文Gradio: Build Machine Learning Web Apps — in PythonGradio is an open-source Python package that allows you to quicklybuilda demo or web application for your machine learning model, API, or any arbitrary Python function. You can thensharea 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 💫InstallationPrerequisite: Gradio requiresPython 3.8 or higherWe recommend installing Gradio usingpip, 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 systemsare provided here.Building Your First DemoYou 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:importgradioasgrdefgreet(name,intensity):return"Hello "*intensity+name+"!"demo=gr.Interface(fn=greet,inputs=["text","slider"],outputs=["text"],)demo.launch()[!TIP]
We shorten the imported name fromgradiotogrfor 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 runpython app.pyfrom the terminal.The demo below will open in a browser onhttp://localhost:7860if running from a file. If you are running within a notebook, the demo will appear embedded within the notebook.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 inhot reload mode, which automatically reloads the Gradio app whenever you make changes to the file. To do this, simply type ingradiobefore the name of the file instead ofpython. In the example above, you would type:gradio app.pyin your terminal. Learn more about hot reloading in theHot Reloading Guide.Understanding theInterfaceClassYou'll notice that in order to make your first demo, you created an instance of thegr.Interfaceclass. TheInterfaceclass is designed to create demos for machine learning models which accept one or more inputs, and return one or more outputs.TheInterfaceclass has three core arguments:fn: the function to wrap a user interface (UI) aroundinputs: 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.Thefnargument is very flexible -- you can passanyPython 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.Theinputandoutputarguments take one or more Gradio components. As we'll see, Gradio includes more than30 built-in components(such as thegr.Textbox(),gr.Image(), andgr.HTML()components) that are designed for machine learning applications.[!TIP]
For theinputsandoutputsarguments, 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 toinputs, 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 tooutputs. This flexibility makes theInterfaceclass a very powerful way to create demos.We'll dive deeper into thegr.Interfaceon our series onbuilding Interfaces.Sharing Your DemoWhat 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 setshare=Trueinlaunch(), and a publicly accessible URL will be created for your demo. Let's revisit our example demo,  but change the last line as follows:importgradioasgrdefgreet(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.liveNow, 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 onsharing your Gradio application.An Overview of GradioSo far, we've been discussing theInterfaceclass, which is a high-level class that lets to build demos quickly with Gradio. But what else does Gradio do?Chatbots withgr.ChatInterfaceGradio includes another high-level class,gr.ChatInterface, which is specifically designed to create Chatbot UIs. Similar toInterface, you supply a function and Gradio creates a fully working Chatbot UI. If you're interested in creating a chatbot, you can jump straightour dedicated guide ongr.ChatInterface.Custom Demos withgr.BlocksGradio also offers a low-level approach for designing web apps with more flexible layouts and data flows with thegr.Blocksclass. 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 usinggr.Blocks(). For example, the popular image generationAutomatic1111 Web UIis built using Gradio Blocks. We dive deeper into thegr.Blockson our series onbuilding with Blocks.The Gradio Python & JavaScript EcosystemThat's the gist of the coregradioPython 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 moretechnical API documentation.Questions?If you'd like to report a bug or have a feature request, please create anissue on GitHub. For general questions about usage, we are available onour Discord serverand happy to help.If you like Gradio, please leave us a ⭐ on GitHub!Open Source StackGradio is built on top of many wonderful open-source libraries!LicenseGradio is licensed under the Apache License 2.0 found in theLICENSEfile in the root directory of this repository.CitationAlso check out the paperGradio: 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},
}