Sentiment-Analysis / src /streamlit_app.py
Diary14's picture
Update src/streamlit_app.py
b83203e verified
Raw
History Blame Contribute Delete
1.59 kB
import streamlit as st
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import pandas as pd
st.set_page_config(page_title="Sentiment Analysis", layout="wide")
st.title("Welcome to the Sentiment Analyzer")
st.write("**Note**: All reviews must be entered in English.")
@st.cache_resource
def load_model():
model_id = "Diary14/roberta-sentiment-lora"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForSequenceClassification.from_pretrained(model_id)
return tokenizer, model
with st.spinner("Loading model"):
tokenizer, model = load_model()
user_input = st.text_area(
"Enter reviews here :",
placeholder="Example :\nThis product is amazing!\nI really don't like it....",
height=200
)
if st.button("Analyze reviews"):
lines = [line.strip() for line in user_input.split("\n") if line.strip() != ""]
if not lines:
st.warning("Please enter at least one review.")
else:
inputs = tokenizer(lines, return_tensors="pt", truncation=True, padding=True)
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
predictions = torch.argmax(logits, dim=-1).tolist()
results = []
for text, pred in zip(lines, predictions):
sentiment = "Positive review" if pred == 1 else "Negative review"
results.append({"Text/Review": text, "Sentiment": sentiment})
df = pd.DataFrame(results)
st.dataframe(df, use_container_width=True)