Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification | |
| import torch | |
| import torch.nn.functional as F | |
| MODEL_NAME = "imrgurmeet/fine-tuned-sentiment-model" | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) | |
| model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME) | |
| st.title("Sentiment Analyzer(Encoder-Only)") | |
| user_input = st.text_area("Enter text for sentiment analysis:") | |
| if st.button("Analyze"): | |
| if user_input.strip() != "": | |
| inputs = tokenizer(user_input, return_tensors="pt") | |
| outputs = model(**inputs) | |
| probs = F.softmax(outputs.logits, dim=-1) | |
| # Dynamically create label list based on model | |
| num_labels = model.config.num_labels | |
| if num_labels == 3: | |
| labels = ["Negative", "Neutral", "Positive"] | |
| elif num_labels == 2: | |
| labels = ["Negative", "Positive"] | |
| else: | |
| labels = [f"Class {i}" for i in range(num_labels)] | |
| pred_class = torch.argmax(probs).item() | |
| sentiment = labels[pred_class] | |
| st.write(f"**Sentiment:** {sentiment}") | |
| st.write(f"**Confidence:** {probs[0][pred_class]:.2f}") | |
| else: | |
| st.warning("Please enter some text.") | |