Delightful Machine Learning Apps with Gradio
Episode Deep Dive
Guest Introduction and Background
Freddy Boulton has a deep background in data science and statistics. Early in his career, he experienced the frustrations of sharing machine learning models with non-technical stakeholders—especially around how to let them interact with the models. This experience led him to focus on tools and open-source frameworks for rapidly building ML web applications. Freddy is a core contributor to Gradio, an open-source Python library that aims to simplify the process of turning ML functions into shareable web applications.
What to Know If You're New to Python
Here are a few points that will help you get more out of this conversation if you're relatively new to Python:
- Familiarize yourself with virtual environments (e.g.,
venv
orconda
) to keep your ML projects and dependencies separate. - Know how to install external packages (
pip install some-library
) so you can easily add tools like Gradio. - A basic understanding of functions, inputs, and outputs in Python will help clarify how Gradio “wraps” machine learning models.
Key Points and Takeaways
- Gradio as a Rapid ML UI Tool
Gradio lets you convert a Python function into a fully interactive web-based UI in just a few lines of code. Instead of manually coding the HTML/JS for sliders, text boxes, or image inputs, Gradio handles the frontend. This lowers the barrier for data scientists who aren't web developers but still need to share or demo their models. Beyond simple demos, Gradio supports advanced features like streaming outputs and chat-style interfaces.
- Links and Tools:
- Gradio: gradio.app
- GitHub Repo: github.com/gradio-app/gradio
- Links and Tools:
- Comparisons with Other Frameworks
Freddy highlighted how Gradio fits alongside other Python frameworks such as Streamlit, Plotly Dash, and Shiny for rapidly prototyping ML apps. Unlike most web frameworks (Flask, Django, FastAPI) requiring you to think in terms of routes and manual callbacks, Gradio’s “function in, UI out” approach is more direct for many data science workflows.
- Links and Tools:
- Flask: flask.palletsprojects.com
- Django: djangoproject.com
- Streamlit: streamlit.io
- Shiny: shiny.rstudio.com
- Links and Tools:
- Interface vs. Blocks vs. Chat Interface
Gradio offers several APIs:
gr.Interface
: Easiest “one-liner” approach to wrap a Python function with prebuilt inputs/outputs.gr.Blocks
: Gives you more control over layout, columns, rows, and custom logic.gr.ChatInterface
: Creates a chat-style interface, which is perfect for large language model (LLM) integrations where the conversation is streamed back as tokens.- Links and Tools:
- LLMs: OpenAI, Meta’s Llama 2, or your favorite hosted model
- Building Realistic ML Demos
Freddy and Michael discussed examples like time series forecasting, classification with XGBoost, and explainability with SHAP. By returning plots (e.g., using Matplotlib or Plotly) directly from a Python function, Gradio displays them in a web UI.
- Links and Tools:
- XGBoost: xgboost.ai
- SHAP: github.com/slundberg/shap
- Matplotlib: matplotlib.org
- Links and Tools:
- Sharing Apps with Hugging Face Spaces
Gradio integrates seamlessly with Hugging Face Spaces. You can upload your Gradio project (via Git) and instantly get a hosted web app. Hugging Face provides free CPU-based hosting for smaller demos, and you can pay for GPU instances if you need more power.
- Links and Tools:
- Hugging Face Spaces: huggingface.co/spaces
- Links and Tools:
- Ephemeral Links for Quick Collaboration
You can add
share=True
to yourGradio
app’s.launch()
method and instantly generate a live link. This is extremely handy for internal demos or user testing sessions. No specialized deployment, no custom domains, just a publicly accessible URL valid for up to 72 hours.- Links and Tools:
- Gradio share mode: gradio.app/docs#sharing-and-deploying
- Links and Tools:
- Under the Hood: FastAPI and Svelte
Gradio runs a lightweight FastAPI server in the background and relies on a Svelte-based frontend. While you don’t have to know these technologies directly, it highlights that your Gradio app is effectively a modern web application with asynchronous capabilities.
- Links and Tools:
- FastAPI: fastapi.tiangolo.com
- Svelte: svelte.dev
- Links and Tools:
- Future Directions: Custom Components & WebAssembly
Freddy shared that upcoming features include an easier path to develop custom Gradio components without modifying the core library. There’s also work around “Gradio Wasm,” which will let ML models run directly in the browser (via Pyodide) for a fully client-side experience.
- Links and Tools:
- Pyodide: pyodide.org
- Gradio GitHub Issues: github.com/gradio-app/gradio/issues
- Links and Tools:
Interesting Quotes and Stories
- “Machine learning can feel abstract. If you just show stakeholders a PDF with metrics, it’s not real until they can type or click on something.” – Reflecting on the importance of interactive demos.
- “We’ve all had an older relative ask us about ChatGPT or stable diffusion at this point.” – Showcasing how AI and ML have truly gone mainstream.
Key Definitions and Terms
- Reactive Programming: A programming paradigm where changes in data automatically update relevant components of the UI.
- SHAP: A method for interpreting individual model predictions, showing which features contributed to the outcome.
- Ephemeral Link: A temporary URL that lets others access your local Gradio app. It expires automatically.
- Pyodide: A project that compiles Python to WebAssembly, allowing Python (and many PyData libraries) to run in the browser.
Learning Resources
Here are some courses from Talk Python Training to help you dive deeper into topics mentioned during the episode:
- Build An Audio AI App: Create practical ML solutions for spoken-word media.
- Data Science Jumpstart with 10 Projects: A gentle introduction to data science through hands-on coding.
- Reactive Web Dashboards with Shiny: Explore a reactive approach to building dashboards—useful if you want to compare Shiny with Gradio.
Overall Takeaway
Gradio is a powerful, straightforward path for Python developers—especially data scientists—to share and deploy machine learning models via simple yet effective user interfaces. It eliminates the complexity of coding a traditional web frontend, making collaboration and iteration faster. By pairing Gradio with services like Hugging Face Spaces, ephemeral links, or even a self-managed server, you can deliver interactive and compelling ML apps that truly engage your audience.
Links from the show
Gradio: gradio.app
Use as API Example: huggingface.co
Components: gradio.app
Svelte: svelte.dev
Flutter UI/Code structure: docs.flutter.dev
XKCD Matplotlib Theme: matplotlib.org
Gradio XKCD Full Theme: huggingface.co
PrivateGPT: ai.meta.com
Langchain: docs.langchain.com
pipdeptree: pypi.org
Watch this episode on YouTube: youtube.com
Episode transcripts: talkpython.fm
--- Stay in touch with us ---
Subscribe to Talk Python on YouTube: youtube.com
Talk Python on Bluesky: @talkpython.fm at bsky.app
Talk Python on Mastodon: talkpython
Michael on Bluesky: @mkennedy.codes at bsky.app
Michael on Mastodon: mkennedy