Spaces:
Running
Running
File size: 1,592 Bytes
976ced4 b83203e 976ced4 b83203e fab084c b83203e 976ced4 b83203e 976ced4 b83203e 976ced4 b83203e 976ced4 b83203e 976ced4 b83203e 976ced4 b83203e 976ced4 fab084c | 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 | 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) |