import streamlit as st
import joblib
import pandas as pd
import re
import nltk
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
st.set_page_config(layout="centered")
col1, col2, col3 = st.columns([1, 4, 1])
with col2:
st.image("images.jpg", use_container_width=True)
# ----------------------------
# NLTK setup
# ----------------------------
nltk.download("stopwords")
nltk.download("wordnet")
stop_words = set(stopwords.words("english"))
lemmatizer = WordNetLemmatizer()
# ----------------------------
# Load trained model
# ----------------------------
model = joblib.load(r"model.pkl")
# ----------------------------
# Streamlit UI
# ----------------------------
st.set_page_config(page_title="Flipkart Sentiment", layout="centered")
st.title("Flipkart Review Sentiment Analysis")
st.write("Enter a review to predict sentiment")
review_text = st.text_area(" Review Text", height=180)
# ----------------------------
# Predict
# ----------------------------
if st.button("Predict Sentiment"):
if review_text.strip() == "":
st.warning("Please enter a review")
else:
# Clean text (same as training)
text = review_text.lower()
text = re.sub(r"[^a-z\s]", "", text)
words = text.split()
words = [lemmatizer.lemmatize(w) for w in words if w not in stop_words]
cleaned_text = " ".join(words)
# IMPORTANT: numeric columns must exist (use 0 if not provided)
input_df = pd.DataFrame({
"review_text": [cleaned_text],
"up_votes": [0],
"down_votes": [0]
})
prediction = model.predict(input_df)[0]
if prediction == 1:
st.success(" Positive Review")
else:
st.error(" Negative Review")
st.markdown("", unsafe_allow_html=True)
st.markdown("""