LalitChaudhari3 commited on
Commit
6b3309d
·
verified ·
1 Parent(s): 53066bf

Update app/app.py

Browse files
Files changed (1) hide show
  1. app/app.py +124 -120
app/app.py CHANGED
@@ -1,120 +1,124 @@
1
- # ----------------------------
2
- # FILE: app.py (FINAL CLEAN VERSION)
3
- # ----------------------------
4
- import os
5
- import streamlit as st
6
-
7
- # Avoid TensorFlow warnings
8
- os.environ.setdefault("TRANSFORMERS_NO_TF", "1")
9
- os.environ["STREAMLIT_SERVER_HEADLESS"] = "true" # Required for HuggingFace Spaces
10
-
11
- # ----------------------------------------------------
12
- # MAIN PAGE CONFIG (ONLY HERE) — required by Streamlit
13
- # ----------------------------------------------------
14
- st.set_page_config(
15
- page_title="AI Customer Feedback Analyzer — SaaS Pro",
16
- page_icon="🧠",
17
- layout="wide"
18
- )
19
-
20
- # ----------------------------------------------------
21
- # GLOBAL SESSION STATE
22
- # ----------------------------------------------------
23
- if "logged_in" not in st.session_state:
24
- st.session_state.logged_in = False
25
-
26
- if "username" not in st.session_state:
27
- st.session_state.username = ""
28
-
29
- # ----------------------------------------------------
30
- # PREMIUM SAAS LANDING HEADER ✔ FIXED HTML, NO RAW TEXT
31
- # ----------------------------------------------------
32
- st.markdown(
33
- """
34
- <div style='background:linear-gradient(90deg,#00c2b8,#007bff);
35
- padding:22px;border-radius:12px;margin-bottom:15px; color:white;'>
36
-
37
- <h1 style='margin:0;'>🧠 AI Customer Feedback Analyzer</h1>
38
- <p style='opacity:0.9;'>Hybrid BERT + RoBERTa • Explainable AI (LIME) • Premium SaaS UI</p>
39
-
40
- <div style='text-align:right; margin-top:-40px;'>
41
- <strong>Built by Lalit Chaudhari</strong><br>
42
- <a href='https://github.com/LalitChaudhari851' style='color:white;text-decoration:none;'>GitHub</a> •
43
- <a href='https://www.linkedin.com/in/lalit-chaudhari-9a62b8190/'
44
- style='color:white;text-decoration:none;'>LinkedIn</a>
45
- </div>
46
-
47
- </div>
48
- """,
49
- unsafe_allow_html=True
50
- )
51
-
52
- # ----------------------------------------------------
53
- # CTA — open login page
54
- # ----------------------------------------------------
55
- st.markdown("### 👇 Start using the app")
56
-
57
- if st.button("🔐 Open Login Page"):
58
- st.switch_page("pages/login.py")
59
-
60
- st.markdown("---")
61
-
62
- # ----------------------------------------------------
63
- # SHARE MODELS WITH PAGES (CACHE = FAST)
64
- # ----------------------------------------------------
65
- @st.cache_resource(show_spinner=True)
66
- def get_shared_resources():
67
- """Load ML models only once, share across pages."""
68
- resources = {
69
- "bert": None,
70
- "roberta": None,
71
- "has_transformers": False,
72
- "has_wordcloud": False,
73
- "has_lime": False
74
- }
75
-
76
- try:
77
- from transformers import pipeline
78
-
79
- resources["bert"] = pipeline(
80
- "sentiment-analysis",
81
- model="distilbert-base-uncased-finetuned-sst-2-english"
82
- )
83
-
84
- resources["roberta"] = pipeline(
85
- "sentiment-analysis",
86
- model="cardiffnlp/twitter-roberta-base-sentiment"
87
- )
88
-
89
- resources["has_transformers"] = True
90
-
91
- except Exception as e:
92
- print("Transformer load issue:", e)
93
-
94
- try:
95
- import wordcloud
96
- resources["has_wordcloud"] = True
97
- except:
98
- pass
99
-
100
- try:
101
- import lime
102
- resources["has_lime"] = True
103
- except:
104
- pass
105
-
106
- return resources
107
-
108
- # ----------------------------------------------------
109
- # PRELOAD MODELS BUTTON
110
- # ----------------------------------------------------
111
- if st.button(" Preload ML Models (Optional)"):
112
-
113
- _ = get_shared_resources()
114
- st.success("Models preloaded (if available)!")
115
-
116
- # ----------------------------------------------------
117
- # FOOTER
118
- # ----------------------------------------------------
119
- st.markdown("---")
120
- st.caption("🚀 AI Customer Feedback Analyzer • Hybrid BERT + RoBERTa • Premium SaaS UI")
 
 
 
 
 
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")