rabi commited on
Commit
8ea3cfe
·
1 Parent(s): bb83872

Add sentiment app

Browse files
app.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from model.inference import predict_sentiment
3
+
4
+ st.set_page_config(page_title="Sentiment AI", page_icon="💡")
5
+
6
+ st.title("💡 Sentiment AI Analyzer")
7
+
8
+ text = st.text_area("Enter your text:")
9
+
10
+ if st.button("Analyze"):
11
+ if text.strip() != "":
12
+ label, confidence = predict_sentiment(text)
13
+
14
+ st.subheader(f"Result: {label}")
15
+ st.write(f"Confidence: {confidence:.2f}")
16
+ else:
17
+ st.warning("Please enter some text")
model/__init__.py ADDED
File without changes
model/__pycache__/__init__.cpython-313.pyc ADDED
Binary file (165 Bytes). View file
 
model/__pycache__/inference.cpython-313.pyc ADDED
Binary file (1.21 kB). View file
 
model/inference.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
2
+ import torch
3
+
4
+ model_name = "Jurk06/bert-uncased-sst2"
5
+
6
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
7
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
8
+
9
+ labels = ["Negative", "Positive"]
10
+
11
+ def predict_sentiment(text):
12
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
13
+
14
+ outputs = model(**inputs)
15
+ probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
16
+
17
+ pred = torch.argmax(probs).item()
18
+ confidence = probs[0][pred].item()
19
+
20
+ return labels[pred], confidence
requirements.txt CHANGED
@@ -1,3 +1,6 @@
1
  altair
2
  pandas
 
 
 
3
  streamlit
 
1
  altair
2
  pandas
3
+ streamlit
4
+ transformers
5
+ torch
6
  streamlit