Spaces:
Sleeping
Sleeping
File size: 2,087 Bytes
e13d798 6024eb3 e13d798 6024eb3 1d36b52 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
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.
---
""") |