Fixing Tensor item Issue
Browse files
app.py
CHANGED
@@ -1,32 +1,29 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import
|
3 |
-
|
4 |
-
|
5 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline, AutoConfig
|
6 |
-
model_ckpt = "AbhishekBhavnani/TweetClassification"
|
7 |
-
|
8 |
-
model = AutoModelForSequenceClassification.from_pretrained(
|
9 |
-
model_ckpt,
|
10 |
-
low_cpu_mem_usage=False
|
11 |
-
)
|
12 |
|
|
|
|
|
13 |
tokenizer = AutoTokenizer.from_pretrained(model_ckpt)
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
)
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
4 |
+
import torch.nn.functional as F
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
# Load tokenizer and model
|
7 |
+
model_ckpt = "AbhishekBhavnani/TweetClassification"
|
8 |
tokenizer = AutoTokenizer.from_pretrained(model_ckpt)
|
9 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_ckpt)
|
10 |
+
model.eval() # Important for inference mode
|
11 |
+
|
12 |
+
# App UI
|
13 |
+
st.title("Tweet Emotion Classifier")
|
14 |
+
text = st.text_area("Enter your tweet here")
|
15 |
+
|
16 |
+
if st.button("Predict"):
|
17 |
+
if text.strip():
|
18 |
+
with torch.no_grad():
|
19 |
+
# Tokenize input
|
20 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
21 |
+
outputs = model(**inputs)
|
22 |
+
probs = F.softmax(outputs.logits, dim=-1)
|
23 |
+
top = torch.argmax(probs, dim=1).item()
|
24 |
+
label = model.config.id2label[str(top)]
|
25 |
+
score = probs[0][top].item()
|
26 |
+
|
27 |
+
st.success(f"**Prediction**: {label} ({score:.4f})")
|
28 |
+
else:
|
29 |
+
st.warning("Please enter a tweet.")
|