Spaces:
Sleeping
Sleeping
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. | |
--- | |
""") |