import streamlit as st
import os
import torch
from transformers import DistilBertTokenizerFast, DistilBertForSequenceClassification
import pandas as pd
# Set page config for a modern look
title = "AI Sentiment Classifier"
st.set_page_config(page_title=title, layout="centered")
# Custom CSS for high-tech look
st.markdown(
"""
""",
unsafe_allow_html=True,
)
st.markdown(f"
""", unsafe_allow_html=True)
review = st.text_area("Enter your review:", height=120, key="review_input")
# Load model and tokenizer only once
@st.cache_resource
def load_model():
model_dir = './sentiment_model'
tokenizer = DistilBertTokenizerFast.from_pretrained(model_dir)
model = DistilBertForSequenceClassification.from_pretrained(model_dir)
model.eval()
return tokenizer, model
tokenizer, model = load_model()
sentiment = None
show_result = False
if st.button("Analyze Sentiment"):
if review.strip():
# Tokenize and predict
inputs = tokenizer([review], padding=True, truncation=True, max_length=128, return_tensors='pt')
with torch.no_grad():
outputs = model(input_ids=inputs['input_ids'], attention_mask=inputs['attention_mask'])
pred = torch.argmax(outputs.logits, dim=1).item()
sentiment_map = {0: "negative", 1: "neutral", 2: "positive"}
sentiment = sentiment_map.get(pred, "unknown")
color = {"positive": "#00FFB3", "neutral": "#FFD600", "negative": "#FF4B4B"}[sentiment]
st.markdown(f"
Sentiment: {sentiment.capitalize()}
", unsafe_allow_html=True)
show_result = True
# Save to local history file
history_file = "history.csv"
file_exists = os.path.isfile(history_file)
import csv
with open(history_file, mode="a", newline='', encoding="utf-8") as f:
writer = csv.writer(f)
if not file_exists:
writer.writerow(["review", "sentiment"])
writer.writerow([review, sentiment])
else:
st.warning("Please enter a review to analyze.")
# Show history table
st.markdown("
", unsafe_allow_html=True)
st.subheader("Review History")
# Clear History Button
if st.button("Clear History 🗑️"):
history_file = "history.csv"
if os.path.isfile(history_file):
os.remove(history_file)
st.success("Review history cleared.")
st.experimental_rerun()
history_file = "history.csv"
if os.path.isfile(history_file):
try:
df = pd.read_csv(history_file)
if not df.empty:
st.dataframe(df[::-1].reset_index(drop=True), use_container_width=True)
else:
st.info("No review history yet.")
except Exception:
st.info("No review history yet.")
else:
st.info("No review history yet.")
st.markdown("""
""", unsafe_allow_html=True)