
Shiny for Python Machine Learning Apps with pandas, scikit-learn and TensorFlow - posit::conf(2023)
Presented by Chelsea Parlett-Pelleriti With the introduction of Shiny for Python in 2022, users can now use the power of reactivity with their favorite Python packages. Shiny can be used to build interactive reports, dashboards, and web apps, that make sharing insights and results both simple and dynamic. This includes apps to display and explore popular Machine Learning models built with staple Python packages like pandas, scikit-learn, and TensorFlow. This talk will demonstrate how to build simple Shiny for Python apps that interface with these packages, and discuss some of the benefits of using Shiny for Python to build your web apps. Presented at Posit Conference, between Sept 19-20 2023, Learn more at posit.co/conference. -------------------------- Talk Track: The future is Shiny. Session Code: TALK-1087
image: thumbnail.jpg
Transcript#
This transcript was generated automatically and may contain errors.
Hello, my name is Chelsea Parlette-Pellariti and today I want to talk to you about building machine learning web apps using Shiny for Python.
Now it sounds like you heard a little bit about this before, of course, from all the other wonderful Shiny talks we have, but just to review, Shiny for Python is a tool that allows you to build interactive, reactive, and scalable web apps that just work with the already rich machine learning ecosystem that exists in Python.
So throughout this talk, I'm going to share some examples of how I've used Shiny for Python in my machine learning workflow, and I just want you to ask yourself, where could Shiny for Python fit into your machine learning workflow?
Now I'm not coming to you today as a developer who could give an incredibly complex and detailed talk about the inner workings of Shiny for Python and exactly how it's implemented. Rather, I'm coming to you as a Shiny for Python user, and I want to tell you what that experience is like so that you can see if it's something that you want to implement in your own workflow.
So why am I giving you this talk if I'm not a Shiny for Python developer? Well, let me set the scene. So it's last year before our StudioConf, and none of you knew that Shiny for Python was going to be announced, but I did because I spent the summer working with the developer team to build apps and create educational materials to help people learn how to use Shiny for Python. In fact, if you go to the Shiny for Python front page, you might see one of my apps on that front page, like this one.
But more than that, I'm a professor. And while a lot of you probably don't spend your day-to-day time in front of dozens of undergrads, I think as data people, a huge part of what we do is teach.
Because what is teaching but explaining complicated, complex, and difficult topics in a way that is easily digestible? And I found through my teaching that Shiny for Python apps really helped me accomplish that goal of communicating difficult things in an easily digestible way.
Because what is teaching but explaining complicated, complex, and difficult topics in a way that is easily digestible?
For instance, here's an example of an app that I use in my deep learning class. This is demonstrating a certain method that goes with computer vision models that basically tells you what about an image was important to the computer vision model in classifying that image. So here we see a picture of some elephants, and you can see that the model thinks that these are elephants. And on the right, you get a heat map showing you exactly what pixels were important in making that classification of elephant. And here you can see it's things that make sense, right, like ears and trunks.
And using Shiny for Python apps allows me to get my students involved and to interact with the material that we're learning.
Shiny as a framework
Now Shiny as a framework is not new. As a lot of you know, Shiny came out in 2012 for R. And in the next about 10 years, it has become a beloved tool for making web apps in the R community. Now last year, Shiny for Python was announced, and I think that just means that the community of people that is about to fall in love with Shiny just expanded.
Now I mentioned before that I'm a professor, but if you force me to put a label on myself, I would call myself a statistician. So as you can imagine, I fell in love with R and all its associated tools very early in my career. But I currently teach in an engineering department, which means I am forced to teach all of my classes in Python. The prereqs are in Python. The classes after mine are in Python. And just to be honest, a lot of my students are more comfortable in Python.
And I think that might be relatable to a lot of you, maybe your web developers, your boss, your whole team, your engineers, someone else uses Python. And so now you have to. And the beauty of Shiny for Python is that we can use the Shiny framework, which a lot of us may have fallen in love with when we were using it in R, and we can leverage it with all of the power that Python has.
And I joke a little bit about being forced to teach in Python, but I have to admit, Python has a great industry standard, very rich ecosystem of machine learning packages like scikit-learn, TensorFlow, Keras, and Pandas. And now we can leverage the power of these very established machine learning packages with the Shiny framework using Shiny for Python.
When to use Shiny for Python: interactivity
So I want to tell you about three different times you might want to use Shiny for Python in your machine learning workflow. And the first time is when you need interactivity.
Now, as a professor, I know that interactivity is key to deep understanding. Look, sometimes your boss or the CEO just wants a one-line memo telling them what to do, or they want a Word document with the information that you have found. But in cases where deep understanding is the goal, interactivity is going to be key for that. And Shiny for Python can provide that interactivity for you.
For instance, here's an example of an app that I use with my students to teach them about the differences between different classification models. As you can see on the screen, there's a bunch of different things that my students can change. They can change the data set they're looking at, and they can change different hyperparameters for our various models. Those are then going to interact with the graphs that are shown on the web page, and it allows them to explore and ask questions about these different classification models, and therefore engage and get a deeper understanding of how these models make classifications differently.
Another app that I use is this app that helps you look at and assess model calibration and the fairness of different models. Especially with this topic, it's really important if you want people to have a deep understanding to let them ask and then answer their own questions as they engage and as they get curious. And that's what interactivity through a Shiny for Python app allows you to do.
And we've seen apps like this before. This is another one that I use to explain item response theory models. And you can see it has your typical input sliders that affect a graph. But another thing that I really love is we don't just have to change graphs in response to user input. For instance, in this app, the actual text explanation of the model dynamically updates as the users change their different parameters. This again allows them to get curious, ask questions about what happens when you change this parameter or that one, and then get the answers to their questions.
So where might you have interactivity in your workflow? Well, in my humble opinion, it is in the three Ds. The first one is description. So when you have a new data set or you're trying to answer a new question, you might want to build a tool that allows your co-workers or your shareholders to explore the data and, like I mentioned before, ask and answer their own questions as they get curious about it. You can use Shiny for Python to build a very nice, well-working, no-code exploratory data analysis app that allows people to filter, plot, and summarize their data as they explore it.
The second D is in development. As you're building models or comparing models, you can use Shiny for Python at all stages of that process. For instance, you could even build a Shiny app that helps people on your team build an actual machine learning model. Or you can use it as a way to compare different models as you're trying to decide what is going to get put in production. Or even do something like a multiverse analysis in which you look at the effect of different modeling choices and how it might impact the inferences or the predictions that you're making.
And the last D is deployment. Now, the other two bullet points talked about times when you're in that model development or exploration process. But you can also use Shiny for Python once your model is completely settled on and you're just deploying it to users. For instance, a Shiny for Python web app makes an excellent front end for something like a recommendation system. And because it's interactive, it allows your users to have a richer experience with your model because they're not just getting one static prediction.
When to use Shiny for Python: reactivity
So another time that you might consider using Shiny for Python apps is when you need reactivity. Now, what is reactivity? Well, to take a step back, one thing I think we can all agree on is that machine learning is computationally expensive. Whether we're doing training, fine tuning, making inferences, a lot of the things that we're doing take a lot of computational power. And so we don't want an app that is going to run top to bottom every time a user changes something. Because sometimes, in fact, I might even venture to say oftentimes, you are going to be doing things that are computationally expensive when building machine learning apps.
So reactivity basically refers to the fact that we are going to minimally re-render our apps. Only things that absolutely need to change and be re-run are going to be changed in response to user input.
So here's the world's cutest app. This is looking at a computer vision model and it's basically allowing you to test out how different parts of the image are important to the model by seeing what happens when you completely cover it up. By the way, the takeaway from this screenshot is that if the computer vision model can't see the fluffy corgi butt, it is less confident that the image is a corgi.
This has a bunch of user inputs. For instance, it allows users to upload an image that they can then use with this app. When someone uploads an image, pretty much everything on the right-hand side of this app has to update, right? We need to update the picture. We need to update the occluded picture. And we need to make predictions both for the unoccluded and the occluded image.
However, if you look at the other inputs, we can also decide what is going to be occluded in the image, like what range of pixels are going to be removed. And when we change that, we don't need to update the original image and we don't need to update the model's original prediction because nothing about our user input actually affects that. And so we're saving the computational expense of updating that by using reactivity by saying, only re-render things that actually need to be re-rendered.
Now, one thing that is really nice about Shiny is not just that you get reactivity because that's not unique to Shiny. One thing that is unique to Shiny is this inferred reactivity. Inferred reactivity basically refers to the idea that Shiny is deciding what needs to be re-rendered in your app based on how you wrote it. You don't have to think about that at all.
In order to get a deeper idea of what I mean, let's look at this really silly demo app. Here we have an app that is plotting a scatterplot based on a sample of data. It lets users change various aesthetics. And then at the bottom left, it gives you a summary of the sample of your data.
When we have this app, we basically have a computational graph that tells you about how all the different parts of the app are related to each other. For instance, we start off our app with some data. And then if we press the action button, we take a sample of that data. From that sample, we might rely on the fact that users are going to choose the color and what's on the y-axis of our scatterplot. And using that, we then build our scatterplot. Then of course, we have that summary table, which doesn't rely on the y variable for our plot or the color of the dots in our plot, but does rely on the sample that we've randomly taken from our data.
Now, we just built a very simple computational graph. And you can see that this tells us how the different parts of the app relate to each other. For instance, if I update the dot color of my scatterplot, I have no need to update my summary table. And that's reflected in my graph. Only things downstream of changes that we make need to be updated when that input is updated.
Now, this is an incredibly simple app and the computational graphs for your apps as they grow in complexity is going to become at some point unmanageable if you had to manage this yourself. But the good news is that Shiny manages it for you. So instead of you having to design and specify the computational graph for your app, Shiny is inferring what this computational graph should be. So you get to benefit from reactivity without having to think about the computational graph explicitly.
And I know as I build more complicated apps, I am definitely going to mess up my computational graph at some point. So I'm very grateful that Shiny doesn't even make me think about it. And you can see with even this Corgi app that looks pretty simple when you look at it, the computational graph gets more and more complex as you add various functionality.
When to use Shiny for Python: scalability
So the last scenario when I think you might want to use Shiny for Python in your workflow is when you need scalability.
Now, the nice thing about Shiny for Python is that it's good for all parts of the process. It allows you to have a smooth on-ramp to build really fast, simple, but really good looking apps. But it also is there to support you when that app becomes mission critical and you have to scale it up and get it ready for production.
A lot of other web app frameworks focus on one portion of this process. There's a lot of great web app tools out there that allow you to spin up apps incredibly quickly and get a pretty good looking result. While others focus specifically on getting your apps ready for production when you need a very stable and good looking app that can be pinged, pinged, excuse me, millions of times a day by your users.
But the nice thing about Shiny for Python is that it really can handle a lot of these different stages. For instance, it's great at spinning up really quick apps. At the last conference, Joe gave a talk where he mentioned that the idea for the Shiny framework basically came to him fully formed in a blink of an eye upon waking up. And he really built a web app framework that allows you to build apps in the same way.
For instance, here's an example that I use in one of my classes that is basically a front end for Hangman and my students are responsible for building the backend. And just like Joe, this came to me as a fully formed idea one morning and I said, oh, I want my students to build Hangman today. And in about an hour, I was able to spin up this very simple Shiny app and have something that looked really good, especially given the fact that I only had about an hour to spend on it.
And I'm a professor, I don't have time to build production level web apps. And using Shiny for Python allows me to build things like this, where I had an idea one morning, I spent a couple of hours building an app and my students literally used it that afternoon.
And one of the tools that Shiny for Python offers you is actually something that was mentioned in the last talk, which is this serverless deployment via Shiny Live. So this is an example of that app that I showed you before with all of the classification models. And this is something that I've hosted with Shiny Live.
Now, through the magic of WebAssembly and Pyedide, you can actually have these apps run in your client's browser without actually technically hosting the app anywhere. If you look at the screenshot, all of the information needed for the app, like the code, any data that's relevant to the app, is all encoded and stored in what is admittedly a very long URL. But by sending someone that URL, they're able to run the app in their browser locally. All they need is that URL and an internet connection.
And what does that mean for you? Well, as the person deploying this app, it means no hosting costs because you're not actually hosting anything. For your users, that means they do not have to worry about what I am sure is a dumpster fire of a Python environment. If you're anything like me, they can just run it in their browser with an internet connection.
If you would like an example of an app hosted via Shiny Live, you can use this QR code in order to access a couple of links to apps that I've built and then hosted with Shiny Live.
So as you can see, Shiny is really great at building quick, easy apps, but it can also support you through the process of getting those apps production ready. For instance, let's say that I build this stock app that displays various stocks with various user inputs, and I can build this in about an hour. But then I can hand this over to a web developer who can then get it production ready without having to completely rewrite everything that already works. They can just add features as needed to our computational graph without completely rewriting what I wrote.
So again, Shiny is there to support you through the beginning when you're writing very simple apps, but it can also support you when you're ready to take those apps and put them into production. Now, one last thing I will say is that the scalability of Shiny is not just in the apps, but also in the skills. When you build Shiny for Python apps, you're learning about the Shiny framework, which is then a skill that is transferable back and forth to R and back to Python.
When you build Shiny for Python apps, you're learning about the Shiny framework, which is then a skill that is transferable back and forth to R and back to Python.
All right, I'm almost out of time, so I will end there. And all I want to say is, where do you think Shiny for Python could fit into your machine learning workflow? Thank you.
Q&A
So we have time for one question until our next speaker sets up. Do you have a question? The question is, what is the feature that you have in Shiny for R and you miss most from Shiny for Python?
I haven't run into anything that I feel like is missing in Shiny for Python yet. I will say the syntax in Shiny for Python feels a bit more intuitive to me. You can see if you go on the website, there's a really great blog that compares Shiny for Python to Shiny for R. And there's differences in how, for instance, we don't have things like isolate, and we have things like reactive events that are a little bit different in terms of syntax. And I actually find it more intuitive. So I know that didn't answer your question because I basically said I love Shiny for Python. But that's my answer. I haven't found anything that I feel like is missing, although I know the developers are working all the time to expand Shiny for Python.
