Spaces:
Running
Running
Update frontend.py
Browse files- frontend.py +96 -149
frontend.py
CHANGED
@@ -4,92 +4,60 @@ import pandas as pd
|
|
4 |
from gtts import gTTS
|
5 |
import base64
|
6 |
from io import BytesIO
|
7 |
-
from PIL import Image
|
8 |
import os
|
9 |
import plotly.express as px
|
10 |
|
11 |
st.set_page_config(page_title="NeuroPulse AI", page_icon="π§ ", layout="wide")
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
if
|
26 |
-
|
27 |
-
|
28 |
-
# Dark
|
29 |
if st.session_state.dark_mode:
|
30 |
st.markdown("""
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
""", unsafe_allow_html=True)
|
46 |
|
47 |
-
# Sidebar
|
48 |
with st.sidebar:
|
49 |
st.header("βοΈ Global Settings")
|
|
|
|
|
50 |
|
51 |
-
|
52 |
-
if st.toggle("π Dark Mode", value=st.session_state.dark_mode):
|
53 |
-
if not st.session_state.dark_mode:
|
54 |
-
st.session_state.dark_mode = True
|
55 |
-
st.rerun()
|
56 |
-
else:
|
57 |
-
if st.session_state.dark_mode:
|
58 |
-
st.session_state.dark_mode = False
|
59 |
-
st.rerun()
|
60 |
-
|
61 |
-
if st.toggle("π§ Intelligence Mode", value=st.session_state.intelligence_mode):
|
62 |
-
st.session_state.intelligence_mode = True
|
63 |
-
else:
|
64 |
-
st.session_state.intelligence_mode = False
|
65 |
-
|
66 |
-
DEFAULT_DEMO_TOKEN = "my-secret-key"
|
67 |
-
api_token = st.text_input("π API Token", value=DEFAULT_DEMO_TOKEN, type="password")
|
68 |
-
if not api_token or api_token.strip() == "my-secret-key":
|
69 |
-
st.warning("π§ͺ Running in demo mode β for full access, enter a valid API key.")
|
70 |
-
|
71 |
backend_url = st.text_input("π Backend URL", value="http://localhost:8000")
|
72 |
|
73 |
sentiment_model = st.selectbox("π Sentiment Model", [
|
74 |
"distilbert-base-uncased-finetuned-sst-2-english",
|
75 |
"nlptown/bert-base-multilingual-uncased-sentiment"
|
76 |
])
|
77 |
-
|
78 |
-
|
79 |
-
"Auto-detect", "Generic", "E-commerce", "Healthcare", "Education", "Travel", "Banking", "Insurance",
|
80 |
-
"Gaming", "Food Delivery", "Real Estate", "Fitness", "Entertainment"
|
81 |
-
])
|
82 |
-
|
83 |
-
product_category = st.selectbox("π§© Product Category", [
|
84 |
-
"Auto-detect", "General", "Mobile Devices", "Laptops", "Healthcare Devices", "Banking App",
|
85 |
-
"Travel Service", "Educational Tool", "Insurance Portal", "Streaming App", "Wearables",
|
86 |
-
"Home Appliances", "Food Apps"
|
87 |
-
])
|
88 |
-
|
89 |
-
use_aspects = st.checkbox("π¬ Enable Aspect Analysis")
|
90 |
-
use_smart_summary = st.checkbox("π§ Smart Summary (Single)")
|
91 |
-
use_smart_summary_bulk = st.checkbox("π§ Smart Summary for Bulk")
|
92 |
-
use_explain_bulk = st.checkbox("π§ Generate Explanations (Bulk)")
|
93 |
verbosity = st.radio("π£οΈ Response Style", ["Brief", "Detailed"])
|
94 |
voice_lang = st.selectbox("π Voice Language", ["en", "fr", "es", "de", "hi", "zh"])
|
95 |
|
@@ -103,20 +71,22 @@ def speak(text, lang='en'):
|
|
103 |
mp3.seek(0)
|
104 |
return mp3
|
105 |
|
|
|
106 |
tab1, tab2 = st.tabs(["π§ Single Review", "π Bulk CSV"])
|
107 |
|
108 |
-
#
|
109 |
with tab1:
|
110 |
st.title("π§ NeuroPulse AI β Multimodal Review Analyzer")
|
111 |
st.markdown("<div style='font-size:16px;color:#888;'>Minimum 20β50 words recommended.</div>", unsafe_allow_html=True)
|
112 |
|
113 |
review = st.text_area("π Enter Review", value=st.session_state.review, height=180)
|
|
|
114 |
|
115 |
col1, col2, col3 = st.columns(3)
|
116 |
with col1:
|
117 |
-
analyze = st.button("π Analyze"
|
118 |
with col2:
|
119 |
-
if st.button("π² Example"
|
120 |
st.session_state.review = (
|
121 |
"I love this phone! Super fast performance, great battery, and smooth UI. "
|
122 |
"Camera is awesome too, though the price is a bit high. Overall, very happy."
|
@@ -124,90 +94,69 @@ with tab1:
|
|
124 |
st.session_state.trigger_example_analysis = True
|
125 |
st.rerun()
|
126 |
with col3:
|
127 |
-
if st.button("π§Ή Clear"
|
128 |
-
|
|
|
129 |
st.rerun()
|
130 |
|
131 |
-
if st.session_state.trigger_example_analysis and st.session_state.review:
|
132 |
-
analyze = True
|
133 |
st.session_state.trigger_example_analysis = False
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
try:
|
141 |
-
|
142 |
-
"text": review,
|
143 |
-
"
|
144 |
-
"
|
145 |
-
"aspects": use_aspects,
|
146 |
-
"follow_up": None,
|
147 |
-
"product_category": product_category,
|
148 |
-
"verbosity": verbosity,
|
149 |
-
"intelligence": st.session_state.intelligence_mode
|
150 |
}
|
151 |
-
|
152 |
-
params = {"smart": "1"} if use_smart_summary else {}
|
153 |
-
res = requests.post(f"{backend_url}/analyze/", json=payload, headers=headers, params=params)
|
154 |
-
|
155 |
if res.status_code == 200:
|
156 |
-
|
157 |
-
st.success("β
Analysis Complete")
|
158 |
-
st.subheader("π Summary")
|
159 |
-
st.info(data["summary"])
|
160 |
-
st.caption(f"π§ Summary Type: {'Smart' if use_smart_summary else 'Standard'} | {verbosity} Response")
|
161 |
-
st.markdown(f"**Context:** `{data['industry']}` | `{data['product_category']}` | `Web`")
|
162 |
-
|
163 |
-
st.subheader("π Audio")
|
164 |
-
audio = speak(data["summary"], lang=voice_lang)
|
165 |
-
st.download_button("β¬οΈ Download Summary Audio", audio.read(), "summary.mp3", mime="audio/mp3")
|
166 |
-
|
167 |
-
st.metric("π Sentiment", data["sentiment"]["label"], delta=f"{data['sentiment']['score']:.2%}")
|
168 |
-
st.info(f"π’ Emotion: {data['emotion']}")
|
169 |
-
|
170 |
-
# Follow-up Section
|
171 |
-
st.markdown("### π Got questions?")
|
172 |
-
st.info("π¬ Ask a follow-up question about this review.")
|
173 |
-
sample_questions = [
|
174 |
-
"What did the user like most?",
|
175 |
-
"Any complaints mentioned?",
|
176 |
-
"Is it positive overall?",
|
177 |
-
"What are the improvement areas?"
|
178 |
-
]
|
179 |
-
selected_q = st.selectbox("π‘ Sample Questions", ["Type your own..."] + sample_questions)
|
180 |
-
|
181 |
-
if selected_q != "Type your own...":
|
182 |
-
custom_q = selected_q
|
183 |
-
else:
|
184 |
-
custom_q = st.text_input("π Ask a follow-up")
|
185 |
-
|
186 |
-
if custom_q:
|
187 |
-
with st.spinner("Thinking..."):
|
188 |
-
try:
|
189 |
-
follow_payload = {
|
190 |
-
"text": review,
|
191 |
-
"question": custom_q,
|
192 |
-
"verbosity": verbosity
|
193 |
-
}
|
194 |
-
res = requests.post(f"{backend_url}/followup/", json=follow_payload, headers=headers)
|
195 |
-
if res.status_code == 200:
|
196 |
-
follow = res.json().get("answer")
|
197 |
-
st.subheader("π Follow-Up Answer")
|
198 |
-
st.warning(follow)
|
199 |
-
else:
|
200 |
-
st.error(f"β Follow-up failed: {res.json().get('detail', 'Unknown error')}")
|
201 |
-
except Exception as e:
|
202 |
-
st.error(f"β οΈ Follow-up error: {e}")
|
203 |
else:
|
204 |
-
st.error(f"β
|
205 |
except Exception as e:
|
206 |
-
st.error(f"
|
|
|
|
|
|
|
|
|
207 |
|
208 |
-
#
|
209 |
-
# BULK CSV TAB
|
210 |
-
# -------------------
|
211 |
with tab2:
|
212 |
st.title("π Bulk CSV Upload")
|
213 |
st.markdown("""
|
@@ -246,16 +195,14 @@ with tab2:
|
|
246 |
payload = {
|
247 |
"reviews": df["review"].tolist(),
|
248 |
"model": sentiment_model,
|
249 |
-
"aspects": use_aspects,
|
250 |
"industry": df["industry"].tolist(),
|
251 |
"product_category": df["product_category"].tolist(),
|
252 |
"device": df["device"].tolist(),
|
253 |
"follow_up": df["follow_up"].tolist(),
|
254 |
-
"explain": use_explain_bulk,
|
255 |
"intelligence": st.session_state.intelligence_mode,
|
256 |
}
|
257 |
res = requests.post(
|
258 |
-
f"{backend_url}/bulk/?token={
|
259 |
json=payload
|
260 |
)
|
261 |
if res.status_code == 200:
|
|
|
4 |
from gtts import gTTS
|
5 |
import base64
|
6 |
from io import BytesIO
|
|
|
7 |
import os
|
8 |
import plotly.express as px
|
9 |
|
10 |
st.set_page_config(page_title="NeuroPulse AI", page_icon="π§ ", layout="wide")
|
11 |
|
12 |
+
if os.path.exists("logo.png"):
|
13 |
+
st.image("logo.png", width=180)
|
14 |
+
|
15 |
+
# Session variables
|
16 |
+
for key, default in {
|
17 |
+
"review": "",
|
18 |
+
"dark_mode": False,
|
19 |
+
"intelligence_mode": True,
|
20 |
+
"trigger_example_analysis": False,
|
21 |
+
"last_response": None,
|
22 |
+
"followup_answer": None,
|
23 |
+
}.items():
|
24 |
+
if key not in st.session_state:
|
25 |
+
st.session_state[key] = default
|
26 |
+
|
27 |
+
# Dark mode CSS
|
28 |
if st.session_state.dark_mode:
|
29 |
st.markdown("""
|
30 |
+
<style>
|
31 |
+
html, body, [class*="st-"] {
|
32 |
+
background-color: #121212;
|
33 |
+
color: #f5f5f5;
|
34 |
+
}
|
35 |
+
.stTextInput > div > div > input,
|
36 |
+
.stTextArea > div > textarea,
|
37 |
+
.stSelectbox div div,
|
38 |
+
.stDownloadButton > button,
|
39 |
+
.stButton > button {
|
40 |
+
background-color: #1e1e1e;
|
41 |
+
color: white;
|
42 |
+
}
|
43 |
+
</style>
|
44 |
""", unsafe_allow_html=True)
|
45 |
|
46 |
+
# Sidebar controls
|
47 |
with st.sidebar:
|
48 |
st.header("βοΈ Global Settings")
|
49 |
+
st.session_state.dark_mode = st.toggle("π Dark Mode", value=st.session_state.dark_mode)
|
50 |
+
st.session_state.intelligence_mode = st.toggle("π§ Intelligence Mode", value=st.session_state.intelligence_mode)
|
51 |
|
52 |
+
api_token = st.text_input("π API Token", value="my-secret-key", type="password")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
backend_url = st.text_input("π Backend URL", value="http://localhost:8000")
|
54 |
|
55 |
sentiment_model = st.selectbox("π Sentiment Model", [
|
56 |
"distilbert-base-uncased-finetuned-sst-2-english",
|
57 |
"nlptown/bert-base-multilingual-uncased-sentiment"
|
58 |
])
|
59 |
+
industry = st.selectbox("π Industry", ["Auto-detect", "Generic", "E-commerce", "Healthcare", "Education"])
|
60 |
+
product_category = st.selectbox("π§© Product Category", ["Auto-detect", "General", "Mobile Devices", "Laptops"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
verbosity = st.radio("π£οΈ Response Style", ["Brief", "Detailed"])
|
62 |
voice_lang = st.selectbox("π Voice Language", ["en", "fr", "es", "de", "hi", "zh"])
|
63 |
|
|
|
71 |
mp3.seek(0)
|
72 |
return mp3
|
73 |
|
74 |
+
# Tabs
|
75 |
tab1, tab2 = st.tabs(["π§ Single Review", "π Bulk CSV"])
|
76 |
|
77 |
+
# ---- SINGLE REVIEW ----
|
78 |
with tab1:
|
79 |
st.title("π§ NeuroPulse AI β Multimodal Review Analyzer")
|
80 |
st.markdown("<div style='font-size:16px;color:#888;'>Minimum 20β50 words recommended.</div>", unsafe_allow_html=True)
|
81 |
|
82 |
review = st.text_area("π Enter Review", value=st.session_state.review, height=180)
|
83 |
+
st.session_state.review = review
|
84 |
|
85 |
col1, col2, col3 = st.columns(3)
|
86 |
with col1:
|
87 |
+
analyze = st.button("π Analyze")
|
88 |
with col2:
|
89 |
+
if st.button("π² Example"):
|
90 |
st.session_state.review = (
|
91 |
"I love this phone! Super fast performance, great battery, and smooth UI. "
|
92 |
"Camera is awesome too, though the price is a bit high. Overall, very happy."
|
|
|
94 |
st.session_state.trigger_example_analysis = True
|
95 |
st.rerun()
|
96 |
with col3:
|
97 |
+
if st.button("π§Ή Clear"):
|
98 |
+
for key in ["review", "last_response", "followup_answer"]:
|
99 |
+
st.session_state[key] = ""
|
100 |
st.rerun()
|
101 |
|
102 |
+
if (analyze or st.session_state.trigger_example_analysis) and st.session_state.review:
|
|
|
103 |
st.session_state.trigger_example_analysis = False
|
104 |
+
st.session_state.followup_answer = None
|
105 |
+
with st.spinner("Analyzing..."):
|
106 |
+
try:
|
107 |
+
payload = {
|
108 |
+
"text": st.session_state.review,
|
109 |
+
"model": sentiment_model,
|
110 |
+
"industry": industry,
|
111 |
+
"product_category": product_category,
|
112 |
+
"verbosity": verbosity,
|
113 |
+
"intelligence": st.session_state.intelligence_mode
|
114 |
+
}
|
115 |
+
headers = {"x-api-key": api_token}
|
116 |
+
res = requests.post(f"{backend_url}/analyze/", json=payload, headers=headers)
|
117 |
+
if res.status_code == 200:
|
118 |
+
st.session_state.last_response = res.json()
|
119 |
+
else:
|
120 |
+
st.error(f"API error: {res.status_code} - {res.json().get('detail')}")
|
121 |
+
except Exception as e:
|
122 |
+
st.error(f"π« Exception: {e}")
|
123 |
+
|
124 |
+
data = st.session_state.last_response
|
125 |
+
if data:
|
126 |
+
st.subheader("π Summary")
|
127 |
+
st.info(data["summary"])
|
128 |
+
st.metric("π Sentiment", data["sentiment"]["label"], delta=f"{data['sentiment']['score']:.2%}")
|
129 |
+
st.info(f"π’ Emotion: {data['emotion']}")
|
130 |
+
st.subheader("π Audio")
|
131 |
+
audio = speak(data["summary"], lang=voice_lang)
|
132 |
+
st.download_button("β¬οΈ Download Summary Audio", audio.read(), "summary.mp3")
|
133 |
+
|
134 |
+
st.markdown("### π Got questions?")
|
135 |
+
sample_questions = ["What did the user like most?", "Any complaints mentioned?", "Is it positive overall?"]
|
136 |
+
selected_q = st.selectbox("π‘ Sample Questions", ["Type your own..."] + sample_questions)
|
137 |
+
custom_q = selected_q if selected_q != "Type your own..." else st.text_input("π Ask a follow-up")
|
138 |
+
|
139 |
+
if custom_q:
|
140 |
+
with st.spinner("Thinking..."):
|
141 |
try:
|
142 |
+
follow_payload = {
|
143 |
+
"text": st.session_state.review,
|
144 |
+
"question": custom_q,
|
145 |
+
"verbosity": verbosity
|
|
|
|
|
|
|
|
|
|
|
146 |
}
|
147 |
+
res = requests.post(f"{backend_url}/followup/", json=follow_payload, headers=headers)
|
|
|
|
|
|
|
148 |
if res.status_code == 200:
|
149 |
+
st.session_state.followup_answer = res.json().get("answer")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
else:
|
151 |
+
st.error(f"β Follow-up failed: {res.json().get('detail')}")
|
152 |
except Exception as e:
|
153 |
+
st.error(f"β οΈ Follow-up error: {e}")
|
154 |
+
|
155 |
+
if st.session_state.followup_answer:
|
156 |
+
st.subheader("π Follow-Up Answer")
|
157 |
+
st.success(st.session_state.followup_answer)
|
158 |
|
159 |
+
# ---- BULK CSV ----
|
|
|
|
|
160 |
with tab2:
|
161 |
st.title("π Bulk CSV Upload")
|
162 |
st.markdown("""
|
|
|
195 |
payload = {
|
196 |
"reviews": df["review"].tolist(),
|
197 |
"model": sentiment_model,
|
|
|
198 |
"industry": df["industry"].tolist(),
|
199 |
"product_category": df["product_category"].tolist(),
|
200 |
"device": df["device"].tolist(),
|
201 |
"follow_up": df["follow_up"].tolist(),
|
|
|
202 |
"intelligence": st.session_state.intelligence_mode,
|
203 |
}
|
204 |
res = requests.post(
|
205 |
+
f"{backend_url}/bulk/?token={api_token}",
|
206 |
json=payload
|
207 |
)
|
208 |
if res.status_code == 200:
|