Wednesday, March 6, 2024

Streamlit - Deploying AI app onto stremlit

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.

The process is pretty easy. Below are few steps 

mkdir streamlit-app

cd streamlit-app

touch streamlit_app.py

touch requirements.txt


Requirements txt file contents are 


streamlit==1.22.0

langchain==0.0.176

openai==0.27.7

tiktoken==0.4.0

unstructured==0.6.8

tabulate==0.9.0

pdf2image==1.16.3

pytesseract==0.3.10


To test locally, below can be done 


pip install -r requirements.txt


Create a python file like this below say streamlit_app.py


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](https://platform.openai.com/account/api-keys).*")

    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.")

    else:

        try:

            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:

                    

                    {text}

                """

                prompt = PromptTemplate(template=prompt_template, input_variables=["text"])

                chain = load_summarize_chain(llm, chain_type="stuff", prompt=prompt)

                summary = chain.run(data)

                st.success(summary)

        except Exception as e:

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



python streamlit_app.py # or python3 streamlit_app.py



To deploy, need to create a git repository and push the files above to it


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


Now create Streamlit account and authorise the streamlit to read the GitHub repo. 


Now there will be option to deploy. Once it is done, the app will be available at the given location! 






References

https://medium.com/@alfredolhuissier/streamlit-how-to-deploy-your-ai-app-7a516548eb90

No comments:

Post a Comment