# Gradio — Build ML Demos and Web UIs in Python > Gradio is the easiest way to build interactive machine learning demos and web interfaces. Define inputs and outputs for your model, and Gradio generates a shareable web app with just a few lines of Python code. Integrated with Hugging Face Spaces. ## Install Save as a script file and run: # Gradio — Build ML Demos and Web UIs in Python ## Quick Use ```bash # Install Gradio pip install gradio # Create a simple demo python3 -c " import gradio as gr def greet(name, intensity): return 'Hello, ' + name + '!' * int(intensity) demo = gr.Interface( fn=greet, inputs=['text', gr.Slider(1, 10)], outputs='text' ) demo.launch() " ``` ## Introduction Gradio makes it effortless to create web interfaces for machine learning models, APIs, or any Python function. In as few as three lines, you define your function, specify the input and output types, and Gradio generates a beautiful, interactive web UI — complete with examples, flagging, and a shareable public link. With over 42,000 GitHub stars and deep integration with Hugging Face, Gradio has become the standard tool for ML researchers and engineers to share their models. Every Hugging Face Space with a UI is powered by Gradio, making it the most widely deployed ML demo framework. ## What Gradio Does Gradio wraps any Python function into a web interface. You specify input components (text boxes, image uploaders, sliders, audio recorders) and output components (text, images, plots, audio, video), and Gradio handles the web server, UI rendering, file processing, and state management. ## Architecture Overview ``` [Python Function] def predict(image, text): return model(image, text) | [gr.Interface or gr.Blocks] Maps inputs/outputs to UI components | +-----+-----+ | | [Frontend] [Backend] Svelte UI FastAPI server Auto-generated File handling Responsive Queue system | | +-----+-----+ | [Shareable Link] Local: localhost:7860 Public: *.gradio.live HF Spaces: huggingface.co/spaces ``` ## Self-Hosting & Configuration ```python import gradio as gr from transformers import pipeline # Load ML model classifier = pipeline("sentiment-analysis") # Using Blocks for custom layout with gr.Blocks(theme=gr.themes.Soft()) as demo: gr.Markdown("# Sentiment Analysis") with gr.Row(): with gr.Column(): text_input = gr.Textbox( label="Enter text", placeholder="Type something...", lines=3 ) submit_btn = gr.Button("Analyze", variant="primary") with gr.Column(): label_output = gr.Label(label="Result") submit_btn.click( fn=lambda text: {r["label"]: r["score"] for r in classifier(text)}, inputs=text_input, outputs=label_output ) gr.Examples( examples=["I love this product!", "This is terrible."], inputs=text_input ) demo.launch(server_name="0.0.0.0", server_port=7860) ``` ## Key Features - **3-Line Demos** — wrap any function into a web UI with minimal code - **40+ Components** — text, image, audio, video, file, dataframe, plot, chatbot, and more - **Blocks API** — custom layouts with rows, columns, tabs, and accordions - **Public Sharing** — generate temporary public URLs with share=True - **Hugging Face Integration** — deploy directly to HF Spaces for free hosting - **Queuing** — built-in queue system for handling concurrent requests - **API Generation** — every Gradio app automatically gets a REST API - **Streaming** — real-time streaming for audio, video, and text generation ## Comparison with Similar Tools | Feature | Gradio | Streamlit | Dash | Panel | NiceGUI | |---|---|---|---|---|---| | Primary Use | ML Demos | Data Apps | Dashboards | Dashboards | General Web | | Setup Lines | 3-5 | 10-20 | 20-50 | 20-40 | 10-20 | | ML Components | Excellent | Good | Limited | Limited | Limited | | Sharing | Public URL + HF | Cloud | Self-host | Self-host | Self-host | | Layout | Interface + Blocks | Sequential | Flex layout | Full | Full | | Chatbot UI | Built-in | Via component | Manual | Manual | Manual | | API Auto-gen | Yes | No | No | No | No | ## FAQ **Q: Gradio vs Streamlit — which should I choose?** A: Gradio for ML model demos, input/output interfaces, and sharing with non-technical users. Streamlit for data dashboards, multi-page analytics apps, and apps with complex state management. **Q: How do I deploy a Gradio app permanently?** A: The easiest way is Hugging Face Spaces — push your code to a Space and it runs for free. For self-hosting, run behind Nginx with the Gradio app as a systemd service. **Q: Can Gradio handle real-time streaming?** A: Yes. Gradio supports streaming for text generation (like ChatGPT), audio processing, and video feeds. Use the streaming parameter in your interface definition. **Q: How do I add authentication?** A: Use demo.launch(auth=("username", "password")) for basic auth, or integrate with Hugging Face OAuth for Spaces deployment. ## Sources - GitHub: https://github.com/gradio-app/gradio - Documentation: https://www.gradio.app - Created by Abubakar Abid, acquired by Hugging Face - License: Apache-2.0 --- Source: https://tokrepo.com/en/workflows/10465308-366d-11f1-9bc6-00163e2b0d79 Author: Script Depot