Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +13 -6
src/streamlit_app.py
CHANGED
|
@@ -3,8 +3,7 @@ from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
| 3 |
import torch
|
| 4 |
import torch.nn.functional as F
|
| 5 |
|
| 6 |
-
|
| 7 |
-
MODEL_NAME = "nlptown/bert-base-multilingual-uncased-sentiment"
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 9 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 10 |
|
|
@@ -14,14 +13,22 @@ user_input = st.text_area("Enter text for sentiment analysis:")
|
|
| 14 |
|
| 15 |
if st.button("Analyze"):
|
| 16 |
if user_input.strip() != "":
|
| 17 |
-
# Tokenize input
|
| 18 |
inputs = tokenizer(user_input, return_tensors="pt")
|
| 19 |
-
# Forward pass
|
| 20 |
outputs = model(**inputs)
|
| 21 |
probs = F.softmax(outputs.logits, dim=-1)
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
pred_class = torch.argmax(probs).item()
|
| 24 |
-
sentiment = [
|
|
|
|
| 25 |
st.write(f"**Sentiment:** {sentiment}")
|
| 26 |
st.write(f"**Confidence:** {probs[0][pred_class]:.2f}")
|
| 27 |
else:
|
|
|
|
| 3 |
import torch
|
| 4 |
import torch.nn.functional as F
|
| 5 |
|
| 6 |
+
MODEL_NAME = "imrgurmeet/fine-tuned-sentiment-model"
|
|
|
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 8 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 9 |
|
|
|
|
| 13 |
|
| 14 |
if st.button("Analyze"):
|
| 15 |
if user_input.strip() != "":
|
|
|
|
| 16 |
inputs = tokenizer(user_input, return_tensors="pt")
|
|
|
|
| 17 |
outputs = model(**inputs)
|
| 18 |
probs = F.softmax(outputs.logits, dim=-1)
|
| 19 |
+
|
| 20 |
+
# Dynamically create label list based on model
|
| 21 |
+
num_labels = model.config.num_labels
|
| 22 |
+
if num_labels == 3:
|
| 23 |
+
labels = ["Negative", "Neutral", "Positive"]
|
| 24 |
+
elif num_labels == 2:
|
| 25 |
+
labels = ["Negative", "Positive"]
|
| 26 |
+
else:
|
| 27 |
+
labels = [f"Class {i}" for i in range(num_labels)]
|
| 28 |
+
|
| 29 |
pred_class = torch.argmax(probs).item()
|
| 30 |
+
sentiment = labels[pred_class]
|
| 31 |
+
|
| 32 |
st.write(f"**Sentiment:** {sentiment}")
|
| 33 |
st.write(f"**Confidence:** {probs[0][pred_class]:.2f}")
|
| 34 |
else:
|