Build Your First AI App UI with Streamlit

Imagine you’ve written some great Python code. Maybe it checks the sentiment of a tweet or summarizes a long email. It works well in your terminal, but showing that black window to your boss, a friend, or a recruiter isn’t very impressive. Streamlit helps you turn your code into a real app that runs in the cloud. In this article, I’ll show you how to build your first AI App UI with Streamlit.

A Guide to Build Your First AI App UI with Streamlit

We’ll build an app that takes any text you enter, like a review, message, or diary entry, and instantly tells you if it’s Positive or Negative using a real machine learning model.

Step 1: The Setup

You’ll need to install a few libraries first. Open your terminal or command prompt and install these three free tools for AI development:

pip install streamlit transformers torch

Step 2: Import and Title

Create a new file called app.py and open it in your code editor, such as VS Code.

First, let’s get started by importing the libraries and adding a title and short description to your app:

import streamlit as st
from transformers import pipeline

st.title("🧠 The Vibe Checker")
st.write("Enter any text below, and I'll use AI to tell you if it's **Positive** or **Negative**.")

The st.title command creates a big header for your app. st.write is a handy Streamlit tool that can show text, dataframes, and even markdown.

Step 3: Load the Brain

This is the most important technical step. Loading an AI model uses time and memory. If the model loads every time someone clicks a button, the app will run very slowly.

To solve this, we use a special decorator called @st.cache_resource. It runs the function once to load the model and keeps it in memory. The model only reloads if the app restarts:

@st.cache_resource
def load_model():
    # We use a standard 'sentiment-analysis' pipeline from Hugging Face.
    # It downloads a small, efficient model (distilbert) by default.
    return pipeline("sentiment-analysis")

# Load the model instantly
with st.spinner("Loading AI Brain..."):
    classifier = load_model()

The pipeline(“sentiment-analysis”) function downloads a model called distilbert-base-uncased-finetuned-sst-2-english. This model is lightweight (about 260MB), accurate, and runs quickly on a regular CPU. You don’t need a GPU.

Step 4: The User Input

Next, we need a way for users to interact with the model. We’ll add a text area for input:

user_text = st.text_area("What's on your mind?",
                         placeholder="Type something like: 'I love coding with Python!'")

Step 5: The Logic and Display

Finally, we’ll connect the input to the model and add a button to start the analysis:

if st.button("Analyze Vibe"):
    if user_text.strip(): # Check if text is not empty
        # Pass the text to the model
        result = classifier(user_text)
        
        # The model returns a list like [{'label': 'POSITIVE', 'score': 0.99}]
        label = result[0]['label']
        score = result[0]['score']
        
        # Display the result with some dynamic flair
        if label == "POSITIVE":
            st.success(f"**Positive Vibe Detected!** (Confidence: {score:.2f})")
            st.balloons()  # Fun animation
        else:
            st.error(f"**Negative Vibe Detected.** (Confidence: {score:.2f})")
    else:
        st.warning("Please enter some text first!")

Streamlit runs the whole script from top to bottom each time you interact with the app. Since we cached the model in Step 3, these re-runs are very fast. The st.balloons() function adds a fun touch and makes your app feel more polished.

To launch it, go back to your terminal and type:

streamlit run app.py
Build Your First AI App UI with Streamlit: Sample Output

A new tab will open in your browser, and your AI application will be up and running.

Closing Thoughts

You’ve just built an AI App UI that wraps around a Python function. Now you can:

  1. Replace pipeline(“sentiment-analysis”) with a summarization model, and you have a TL;DR Generator.
  2. Replace it with a translation model, and you have a Universal Translator.
  3. Replace it with a custom Scikit-Learn model trained on your company’s sales data, and you have a Sales Forecaster Dashboard.

If you found this article useful, you can follow me on Instagram for daily AI tips and practical resources. You might also like my latest book, Hands-On GenAI, LLMs & AI Agents. It’s a step-by-step guide to help you get ready for jobs in today’s AI field.

Aman Kharwal
Aman Kharwal

AI/ML Engineer | Published Author. My aim is to decode data science for the real world in the most simple words.

Articles: 2086

5 Comments

  1. Dear Aman
    Can you publish an RAG ,Agentic Agent fully end to end project with deployment stages and types

  2. I am practicing above streamlit project on colab but it is excuted half way the UI didn’t allow to enter text as text box is in disable state
    pls help on this

Leave a Reply

Discover more from AmanXai by Aman Kharwal

Subscribe now to keep reading and get access to the full archive.

Continue reading