Learn Python with Talk Python's 270 hours of courses

Delightful Machine Learning Apps with Gradio

Episode #430, published Tue, Sep 19, 2023, recorded Thu, Aug 10, 2023

So, you've got this amazing machine learning model you created. And you want to share it and let your colleagues and users experiment with it on the web. How do you get started? Learning Flask or Django? Great frameworks, but you might consider Gradio which is a rapid development UI framework for ML models. On this episode, we have Freddy Boulton, to introduce us all to Gradio.

Watch this episode on YouTube
Play on YouTube
Watch the live stream version

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:

  1. Familiarize yourself with virtual environments (e.g., venv or conda) to keep your ML projects and dependencies separate.
  2. Know how to install external packages (pip install some-library) so you can easily add tools like Gradio.
  3. A basic understanding of functions, inputs, and outputs in Python will help clarify how Gradio “wraps” machine learning models.

Key Points and Takeaways

  1. 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.
  2. 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.
  3. 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:
  4. 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.
  5. 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.
  6. Ephemeral Links for Quick Collaboration You can add share=True to your Gradio 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.
  7. 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.
  8. 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.

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:

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

Freddy on Twitter: @freddy_alfonso_
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

Talk Python's Mastodon Michael Kennedy's Mastodon