Streamlit: How to deploy your AI app

Wednesday, June 28, 2023 by alfredo_lhuissier73
Streamlit: How to deploy your AI app


Streamlit is a Python library that is open-source, providing a seamless way to develop and distribute interactive web applications and data visualizations. With Streamlit, you can effortlessly create web apps using Python code, enhanced by its robust additional features. The library comes equipped with integrated support for various data visualization libraries such as matplotlib, pandas, and plotly, simplifying the process of generating interactive charts and graphs that dynamically update based on user input. It is a popular tool among data scientists, machine learning (ML) engineers and developers looking to share interactive web apps with their audience.


Deployment on Streamlit is very easy. You just have to follow these 3 steps:

  1. Create a Streamlit app
  2. Upload your app to Github
  3. Deploy your Github repository to Streamlit

1. Create a Streamlit app

For this tutorial, we are going to create a simple app that uses Langchain and OpenAI's GPT to summarize the contents of a URL. You will need to have python >=3.11 installed.

Note: You can try this with your own app if you have one ready to go.

First, open your terminal and create a new folder named "streamlit-app". Then create a python file "" and a "requirements.txt" text file

mkdir streamlit-app
cd streamlit-app
touch requirements.txt

Open the text editor of your preference, navigate to the requirements.txt file, and proceed to insert the names of the libraries required to execute the application.:


On your terminal, run this command to install the libraries:

pip install -r requirements.txt

Now open your file and paste the following code:

import validators, streamlit as st
from langchain.chat_models import ChatOpenAI
from langchain.document_loaders import UnstructuredURLLoader
from langchain.chains.summarize import load_summarize_chain
from langchain.prompts import PromptTemplate

# Streamlit app
st.subheader('Summarize URL')

# Get OpenAI API key and URL to be summarized
with st.sidebar:
    openai_api_key = st.text_input("OpenAI API key", value="", type="password")
    st.caption("*If you don't have an OpenAI API key, get it [here](*")
    model = st.selectbox("OpenAI chat model", ("gpt-3.5-turbo", "gpt-3.5-turbo-16k"))
    st.caption("*If the article is long, choose gpt-3.5-turbo-16k.*")
url = st.text_input("URL", label_visibility="collapsed")

# If 'Summarize' button is clicked
if st.button("Summarize"):
    # Validate inputs
    if not openai_api_key.strip() or not url.strip():
        st.error("Please provide the missing fields.")
    elif not validators.url(url):
        st.error("Please enter a valid URL.")
            with st.spinner("Please wait..."):
                # Load URL data
                loader = UnstructuredURLLoader(urls=[url])
                data = loader.load()
                # Initialize the ChatOpenAI module, load and run the summarize chain
                llm = ChatOpenAI(temperature=0, model=model, openai_api_key=openai_api_key)
                prompt_template = """Write a summary of the following in 250-300 words:

                prompt = PromptTemplate(template=prompt_template, input_variables=["text"])
                chain = load_summarize_chain(llm, chain_type="stuff", prompt=prompt)
                summary =

        except Exception as e:
            st.exception(f"Exception: {e}")

Save the file. You can now run the app with the following command:

python # or python3

Here is the app's original repository

2. Upload your app to Github

Create a Github account if you haven't already. Then create a new repository and save the URL. The URL must look like this:<your_username>/<your_repository_name>

Create Github Repo
Create Github Repo
Remote Github URL
Remote Github URL

Open your terminal and go to the root folder of your app, then type the following commands:

git init # Initialize a git repository
git add . # Add files to your new commit
git commit -m "first commit" # Make the commit
git remote add origin <YOUR_REPOSITORY_URL> # Connects your local git repository to your remote Github one
git push origin main # Pushes your code to your remote repo

This will upload your code to Github.

3. Deploy your Github repository to Streamlit

Go to the Streamlit website and create a Community Cloud account.

After you finished setting up your account, click on "New app" and authorize Streamlit to access your Github repositories. new app new app

Select your repository and branch from the lists, and write the "Main file path" field (e.g

Connect Github repo to Streamlit
Connect Github repo to Streamlit

πŸŽ‰ That is all! Your app should be up and running. You can now share the URL of the app with the community.

πŸ€” Final Thoughts

We learned how to create and deploy a Streamlit app from a Github repository in three simple steps. Now it's your turn to build amazing things with AI and share them with the world. That is all for this tutorial, I hope you you find it useful. And feel free to reach out to me on LinkedIn or Twitter if you have any questions.

And practice what you've learn here during our amazing AI Hackathons! Join the AI revolution!

Discover tutorials with similar technologies

Upcoming AI Hackathons and Events