AbhishekBhavnani commited on
Commit
587a46c
·
verified ·
1 Parent(s): 9e99fa9

Fixing Tensor item Issue

Browse files
Files changed (1) hide show
  1. app.py +27 -30
app.py CHANGED
@@ -1,32 +1,29 @@
1
- import streamlit as st
2
- import pandas as pd
3
- import numpy as np
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
- # Use the pipeline for text classification
17
- classifier = pipeline(
18
- 'text-classification',
19
- model=model,
20
- tokenizer=tokenizer,
21
- device=-1 # -1 for CPU, set to GPU index if available (e.g., device=0)
22
- )
23
-
24
- st.title("Tweet Classification")
25
-
26
- text = st.text_area("Enter Your Tweet Here")
27
-
28
- if st.button("Predict"):
29
- result = classifier(text)
30
- st.write(result)
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.")