Spaces:
Sleeping
Sleeping
Commit
Β·
98db479
1
Parent(s):
e631481
require
Browse files
app.py
CHANGED
@@ -1,50 +1,83 @@
|
|
1 |
import streamlit as st
|
2 |
import torch
|
3 |
-
from transformers import
|
4 |
-
import torch.nn.functional as F
|
5 |
|
6 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
@st.cache_resource
|
8 |
def load_model():
|
9 |
-
|
10 |
-
|
11 |
-
return
|
12 |
|
13 |
-
|
|
|
14 |
|
15 |
# Streamlit UI
|
16 |
-
st.
|
17 |
-
|
18 |
-
st.title("π¬ Sentiment Analyzer")
|
19 |
-
st.write("Analyze the sentiment of any text! Enter a sentence below and get an instant analysis.")
|
20 |
-
|
21 |
-
user_input = st.text_area("Enter your text:", "")
|
22 |
-
|
23 |
-
if st.button("Analyze Sentiment"):
|
24 |
-
if user_input:
|
25 |
-
with st.spinner("Analyzing..."):
|
26 |
-
inputs = tokenizer(user_input, return_tensors="pt", truncation=True, padding=True)
|
27 |
-
outputs = model(**inputs)
|
28 |
-
probs = F.softmax(outputs.logits, dim=-1)
|
29 |
-
sentiment_index = torch.argmax(probs).item()
|
30 |
-
confidence = round(probs[0][sentiment_index].item() * 100, 2)
|
31 |
-
|
32 |
-
# Map index to label
|
33 |
-
labels = ["Negative", "Neutral", "Positive"] # Adjust this based on your training labels
|
34 |
-
sentiment = labels[sentiment_index]
|
35 |
-
|
36 |
-
# Display result
|
37 |
-
st.subheader("π Result")
|
38 |
-
if sentiment == "Positive":
|
39 |
-
st.success(f"π **Positive Sentiment** ({confidence}%)")
|
40 |
-
elif sentiment == "Negative":
|
41 |
-
st.error(f"π **Negative Sentiment** ({confidence}%)")
|
42 |
-
else:
|
43 |
-
st.warning(f"π **Neutral Sentiment** ({confidence}%)")
|
44 |
|
45 |
-
|
46 |
-
|
47 |
|
48 |
-
st.
|
49 |
-
|
|
|
|
|
|
|
50 |
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import torch
|
3 |
+
from transformers import pipeline
|
|
|
4 |
|
5 |
+
# Set device (GPU if available)
|
6 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
7 |
+
|
8 |
+
# Custom Streamlit Styles
|
9 |
+
st.markdown("""
|
10 |
+
<style>
|
11 |
+
/* Center everything */
|
12 |
+
.block-container {
|
13 |
+
max-width: 650px;
|
14 |
+
text-align: center;
|
15 |
+
}
|
16 |
+
|
17 |
+
/* Title styling */
|
18 |
+
.title {
|
19 |
+
font-size: 2.5rem;
|
20 |
+
font-weight: bold;
|
21 |
+
color: #FF4B4B;
|
22 |
+
text-shadow: 2px 2px 10px rgba(255, 75, 75, 0.5);
|
23 |
+
}
|
24 |
+
|
25 |
+
/* Text input styling */
|
26 |
+
.stTextArea textarea {
|
27 |
+
border-radius: 10px;
|
28 |
+
border: 2px solid #FF4B4B;
|
29 |
+
background-color: #1E1E1E;
|
30 |
+
color: white;
|
31 |
+
font-size: 16px;
|
32 |
+
}
|
33 |
+
|
34 |
+
/* Button styling */
|
35 |
+
div.stButton > button {
|
36 |
+
background-color: #FF4B4B;
|
37 |
+
color: white;
|
38 |
+
border-radius: 10px;
|
39 |
+
font-size: 18px;
|
40 |
+
padding: 10px 20px;
|
41 |
+
transition: 0.3s;
|
42 |
+
}
|
43 |
+
div.stButton > button:hover {
|
44 |
+
background-color: #E63E3E;
|
45 |
+
}
|
46 |
+
|
47 |
+
/* Result display */
|
48 |
+
.result {
|
49 |
+
font-size: 22px;
|
50 |
+
font-weight: bold;
|
51 |
+
color: #FF4B4B;
|
52 |
+
margin-top: 20px;
|
53 |
+
}
|
54 |
+
</style>
|
55 |
+
""", unsafe_allow_html=True)
|
56 |
+
|
57 |
+
# Load Model from Hugging Face
|
58 |
@st.cache_resource
|
59 |
def load_model():
|
60 |
+
model_name = "distilbert-base-uncased-finetuned-sst-2-english" # Replace with your actual HF model
|
61 |
+
classifier = pipeline("text-classification", model=model_name, tokenizer=model_name, device=0 if DEVICE == "cuda" else -1)
|
62 |
+
return classifier
|
63 |
|
64 |
+
# Initialize model
|
65 |
+
classifier = load_model()
|
66 |
|
67 |
# Streamlit UI
|
68 |
+
st.markdown('<p class="title">Sentiment Analysis App π¬</p>', unsafe_allow_html=True)
|
69 |
+
st.write("Enter a review below and let AI analyze its sentiment! π")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
+
# User Input
|
72 |
+
text = st.text_area("Enter text:", "", height=150)
|
73 |
|
74 |
+
if st.button("Analyze"):
|
75 |
+
if text.strip():
|
76 |
+
result = classifier(text)[0]
|
77 |
+
sentiment = result['label']
|
78 |
+
confidence = result['score']
|
79 |
|
80 |
+
# Display sentiment result
|
81 |
+
st.markdown(f'<p class="result">Sentiment: {sentiment} ({confidence:.2%} confidence)</p>', unsafe_allow_html=True)
|
82 |
+
else:
|
83 |
+
st.warning("β οΈ Please enter some text!")
|