Update app/app.py
Browse files- app/app.py +124 -120
app/app.py
CHANGED
|
@@ -1,120 +1,124 @@
|
|
| 1 |
-
# ----------------------------
|
| 2 |
-
# FILE: app.py
|
| 3 |
-
# ----------------------------
|
| 4 |
-
import os
|
| 5 |
-
import streamlit as st
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
os.environ
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
#
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
#
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
""
|
| 49 |
-
|
| 50 |
-
)
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ----------------------------
|
| 2 |
+
# FILE: app.py
|
| 3 |
+
# ----------------------------
|
| 4 |
+
import os
|
| 5 |
+
import streamlit as st
|
| 6 |
+
|
| 7 |
+
os.environ["TRANSFORMERS_NO_TF"] = "1"
|
| 8 |
+
os.environ["STREAMLIT_SERVER_HEADLESS"] = "true"
|
| 9 |
+
|
| 10 |
+
# ----------------------------
|
| 11 |
+
# PAGE CONFIG
|
| 12 |
+
# ----------------------------
|
| 13 |
+
st.set_page_config(
|
| 14 |
+
page_title="AI Customer Feedback Analyzer",
|
| 15 |
+
page_icon="🧠",
|
| 16 |
+
layout="wide"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# ----------------------------
|
| 20 |
+
# HEADER
|
| 21 |
+
# ----------------------------
|
| 22 |
+
st.markdown(
|
| 23 |
+
"""
|
| 24 |
+
<div style='background:linear-gradient(90deg,#00c2b8,#007bff);
|
| 25 |
+
padding:22px;border-radius:12px;color:white;'>
|
| 26 |
+
<h1>🧠 AI Customer Feedback Analyzer</h1>
|
| 27 |
+
<p>Hybrid BERT + RoBERTa • Sentiment Analysis</p>
|
| 28 |
+
</div>
|
| 29 |
+
""",
|
| 30 |
+
unsafe_allow_html=True
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
st.markdown("---")
|
| 34 |
+
|
| 35 |
+
# ----------------------------
|
| 36 |
+
# LOAD MODELS (CACHED)
|
| 37 |
+
# ----------------------------
|
| 38 |
+
@st.cache_resource(show_spinner=True)
|
| 39 |
+
def load_models():
|
| 40 |
+
from transformers import pipeline
|
| 41 |
+
|
| 42 |
+
bert = pipeline(
|
| 43 |
+
"sentiment-analysis",
|
| 44 |
+
model="distilbert-base-uncased-finetuned-sst-2-english"
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
roberta = pipeline(
|
| 48 |
+
"sentiment-analysis",
|
| 49 |
+
model="cardiffnlp/twitter-roberta-base-sentiment"
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
return bert, roberta
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
bert, roberta = load_models()
|
| 56 |
+
|
| 57 |
+
# ----------------------------
|
| 58 |
+
# ROBERTA LABEL FIX ✅
|
| 59 |
+
# ----------------------------
|
| 60 |
+
ROBERTA_LABEL_MAP = {
|
| 61 |
+
"LABEL_0": "Negative",
|
| 62 |
+
"LABEL_1": "Neutral",
|
| 63 |
+
"LABEL_2": "Positive"
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
# ----------------------------
|
| 67 |
+
# ANALYSIS FUNCTIONS
|
| 68 |
+
# ----------------------------
|
| 69 |
+
def analyze_bert(text):
|
| 70 |
+
r = bert(text)[0]
|
| 71 |
+
return r["label"], round(r["score"] * 100, 2)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def analyze_roberta(text):
|
| 75 |
+
r = roberta(text)[0]
|
| 76 |
+
sentiment = ROBERTA_LABEL_MAP.get(r["label"], "Neutral")
|
| 77 |
+
return sentiment, round(r["score"] * 100, 2)
|
| 78 |
+
|
| 79 |
+
# ----------------------------
|
| 80 |
+
# USER INPUT
|
| 81 |
+
# ----------------------------
|
| 82 |
+
st.subheader("✍️ Enter Customer Feedback")
|
| 83 |
+
|
| 84 |
+
text = st.text_area(
|
| 85 |
+
"Feedback",
|
| 86 |
+
placeholder="Example: I love the product but delivery was slow"
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
# ----------------------------
|
| 90 |
+
# ANALYZE
|
| 91 |
+
# ----------------------------
|
| 92 |
+
if st.button("🔍 Analyze Sentiment"):
|
| 93 |
+
|
| 94 |
+
if not text.strip():
|
| 95 |
+
st.warning("Please enter feedback text.")
|
| 96 |
+
else:
|
| 97 |
+
with st.spinner("Analyzing..."):
|
| 98 |
+
bert_sent, bert_conf = analyze_bert(text)
|
| 99 |
+
rob_sent, rob_conf = analyze_roberta(text)
|
| 100 |
+
|
| 101 |
+
col1, col2 = st.columns(2)
|
| 102 |
+
|
| 103 |
+
with col1:
|
| 104 |
+
st.subheader("🤖 BERT")
|
| 105 |
+
st.write(f"Sentiment: **{bert_sent}**")
|
| 106 |
+
st.write(f"Confidence: **{bert_conf}%**")
|
| 107 |
+
|
| 108 |
+
with col2:
|
| 109 |
+
st.subheader("🧠 RoBERTa")
|
| 110 |
+
st.write(f"Sentiment: **{rob_sent}**")
|
| 111 |
+
st.write(f"Confidence: **{rob_conf}%**")
|
| 112 |
+
|
| 113 |
+
st.markdown("---")
|
| 114 |
+
|
| 115 |
+
if bert_sent.lower() == rob_sent.lower():
|
| 116 |
+
st.success(f"✅ Final Verdict: **{bert_sent}**")
|
| 117 |
+
else:
|
| 118 |
+
st.info("⚠️ Final Verdict: **Mixed Sentiment**")
|
| 119 |
+
|
| 120 |
+
# ----------------------------
|
| 121 |
+
# FOOTER
|
| 122 |
+
# ----------------------------
|
| 123 |
+
st.markdown("---")
|
| 124 |
+
st.caption("🚀 Built by Lalit Chaudhari")
|