Sentiment_analysis / src /streamlit_app.py
Rahul9971's picture
Update src/streamlit_app.py
1d36b52 verified
import streamlit as st
import pickle
import re
import string
# Load vectorizer and model
with open('vectorizer.pkl', 'rb') as f:
vectorizer = pickle.load(f)
with open('model.pkl', 'rb') as f:
model = pickle.load(f)
# Preprocessing function
def preprocess(text_input):
# Remove punctuation
clean = text_input.translate(str.maketrans('', '', string.punctuation))
# Split into words
words = clean.split()
# Filter words: no repeated chars, digits, non-ascii
filtered = [
w for w in words
if not re.search(r'(.)\1{2,}', w)
and not w.isdigit()
and w.isascii()
]
return " ".join(filtered)
# App UI
st.title("✨ Sentiment Analysis App ✨")
st.header("πŸ” Analyze the Sentiment of Your Comments Instantly")
title = st.text_input(
"Enter a movie review or comment below:",
"I didn't feel humiliated"
)
if st.button("πŸ” Predict Sentiment"):
title = title.lower().strip()
if not title:
st.warning("⚠️ Please enter a valid comment.")
else:
filtered_text = preprocess(title)
if filtered_text.strip() == "":
st.warning("⚠️ Your input was too short or invalid after preprocessing.")
else:
X_test_vectorized = vectorizer.transform([filtered_text])
prediction = model.predict(X_test_vectorized)[0]
if prediction == 1:
st.success("πŸŽ‰ **The sentiment is Positive! Great vibes ahead!**")
else:
st.error("😞 **The sentiment is Negative. Let's work on making it better! πŸ’ͺ**")
st.markdown("""
---
### How This Works
- **Title:** The main heading grabs your attention and shows what this app is all about.
- **Header:** A quick subtitle explaining that you can analyze the sentiment of your comments in real time.
- **Input Box:** Enter any movie review or comment here to see if it's positive or negative.
πŸ’‘ *Using emojis makes the app more fun and inviting!*
Clear instructions help you get started right away.
---
""")