Using streamlit to Deploy a PoC App (3/3)


In this third part of the ML project miniseries, we’ll explore how to publish your newly created solar panel detection application using streamlit.

  1. Data collection and preprocessing
  2. Training neural network model usingfastai
  3. Deploying your app usingstreamlit

We’ve been working on building a tool to automatically detect solar panels from orthophotos. So far we’ve gone through how to collect and preprocess these images and use them to train a neural network model with fastai. In this part, we’ll focus on creating a web app PoC usingstreamlit.

You’ve come all this way and built your very own solar panel segmentation model. Why not share it with your friends and colleagues?

Show off your hard work and learn how to create various data-related apps usingstreamlit.

What is streamlit

Streamlit is a relatively new, open-source app framework that lets data scientists and ML engineers create and publish apps quickly. And it’s done all in Python.

Now, if you’re not a Python guru or want something with a little more ‘React’ feel to it, don’t fret. There are multiple dashboard frameworks you can choose from. For instance, our personal favorite – R Shiny. There’s also Dash, Viola, and you have the ability to share a jupyter notebook or create something from scratch in flask.

Since its publication in 2019,streamlithas seen significant interest – over 15.9k stars on Github! It continues to provide the Python community with functionality upgrades and regular maintenance. The library helps users create PoCs, apps, and dashboards as easily as possible, as quickly as possible, and all for free. It’s not unlike R Shiny – with the exception that R Shiny is a – you guessed it! – R package.

Writing hello world app in streamlit

There’s this idea withstreamlitthat you should be able to write what you want to see when you want to see it. Keep that in mind when trying to understand the code.

You can run your app with a simple command:streamlit run st_hello_world.pyin the terminal.

 

Interactive and latex demo in streamlit

It’s cool to see something like this so fast. Especially with the lack of so-called boilerplate code. Every symbol serves some purpose! Now let’s try adding some interactivity to the app.

 

There are some people (like me!) for whom a decent scientific blog system/web app framework MUST allow easy work with latex equations. Fortunately, that’s a piece of cake in streamlit. Due to the way f-strings work in Python, we have a tiny bit of boilerplate code to include here:

 

Solar panel segmentation in streamlit PoC app

By combining the features described above with a few more, we can create a functional demo app.

But first, I want to point out a few things:

  1. fastai requires thelabel_funcfunction to be available in scope upon loading (recall part 2 of the miniseries). That’s why it’s defined like that.
  2. Predictions are made based on either the provided example or an uploaded file.
  3. After uploading/choosing a file from the list it’s displayed until the model’s prediction is calculated. Then the image is substituted for the alpha channel showing the location of the panels.
  4. In lines 30-31, we change the mask to make solar panels non-transparent, and the rest of the image is slightly faded.

 

Sharing streamlit app with the World

And there we have it! A demo app that’s ready to be shared with the world. We’ve added ours on RStudio Connect. You can explore the Solar Panel Detection demo app for yourself.

It’s pretty easy to make a ‘Hello World’ withstreamlit. And in this tutorial, we’ve shared with you how to add a trained model. If you have any issues, be sure to check out the official tutorial.

While this method might be convenient for small applications to share with friends and colleagues, there’s another solution for enterprises – using RStudio Connect. So keep an eye out for a future post on how to upgrade your app deployment. In the meantime, feel free to contact us at hello@appsilon.com should you need any immediate assistance.

Conclusion

Using the new Python library streamlit, we were able to share our impressive solar panel identifier model with the world! We learned that this library is a great way to quickly visualize your Python experiments and a fairly friendly tool for beginners with great expressive power – a true Python package.

I guess that’s why its popularity is growing every month. And probably why you should be utilizing it more!

Sign up for our newsletter to get updates and receive our latest blogs in your inbox – like how to deploy streamlit apps on RStudio Connect!

This project was written under:

  • fastai version 2.4.1
  • streamlit version 0.86.0