| import streamlit as st |
| import torch |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification |
| import torch.nn.functional as F |
|
|
| |
| st.set_page_config(page_title="Sentiment Analysis", layout="centered") |
|
|
| |
| MODEL_NAME = "nlptown/bert-base-multilingual-uncased-sentiment" |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) |
| model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME) |
|
|
| |
| st.title("π₯ Sentiment Analysis with Transformers") |
| st.write("Enter text below to analyze sentiment using a BERT-based model.") |
|
|
| |
| user_input = st.text_area("Enter text here:", "") |
|
|
| if st.button("Analyze Sentiment"): |
| if user_input.strip(): |
| |
| inputs = tokenizer(user_input, return_tensors="pt", truncation=True, padding=True) |
| |
| |
| with torch.no_grad(): |
| outputs = model(**inputs) |
| scores = F.softmax(outputs.logits, dim=-1).squeeze().tolist() |
| |
| |
| labels = ["Very Negative π‘", "Negative π", "Neutral π", "Positive π", "Very Positive π"] |
| sentiment = labels[scores.index(max(scores))] |
|
|
| |
| st.subheader("Sentiment Analysis Result:") |
| st.write(f"**Predicted Sentiment:** {sentiment}") |
| st.write("**Confidence Scores:**") |
| for label, score in zip(labels, scores): |
| st.write(f"- {label}: {score:.2%}") |
|
|
| else: |
| st.warning("β οΈ Please enter text before analyzing.") |
|
|
| |
| st.markdown("---") |
| st.markdown("π‘ *Built with PyTorch, Hugging Face Transformers & Streamlit*") |
|
|
|
|