Spaces:
Running
Running
Update frontend.py
Browse files- frontend.py +29 -19
frontend.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
import pandas as pd
|
@@ -8,14 +9,13 @@ 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 |
-
# Load logo
|
14 |
logo_path = "logo.png"
|
15 |
if os.path.exists(logo_path):
|
16 |
st.image(logo_path, width=180)
|
17 |
|
18 |
-
# Session State
|
19 |
if "review" not in st.session_state:
|
20 |
st.session_state.review = ""
|
21 |
if "dark_mode" not in st.session_state:
|
@@ -25,7 +25,7 @@ if "intelligence_mode" not in st.session_state:
|
|
25 |
if "trigger_example_analysis" not in st.session_state:
|
26 |
st.session_state.trigger_example_analysis = False
|
27 |
|
28 |
-
#
|
29 |
if st.session_state.dark_mode:
|
30 |
st.markdown("""
|
31 |
<style>
|
@@ -44,7 +44,7 @@ if st.session_state.dark_mode:
|
|
44 |
</style>
|
45 |
""", unsafe_allow_html=True)
|
46 |
|
47 |
-
# Sidebar
|
48 |
with st.sidebar:
|
49 |
st.header("โ๏ธ Global Settings")
|
50 |
st.session_state.dark_mode = st.toggle("๐ Dark Mode", value=st.session_state.dark_mode)
|
@@ -76,10 +76,11 @@ with st.sidebar:
|
|
76 |
use_aspects = st.checkbox("๐ฌ Enable Aspect Analysis")
|
77 |
use_smart_summary = st.checkbox("๐ง Smart Summary (Single)")
|
78 |
use_smart_summary_bulk = st.checkbox("๐ง Smart Summary for Bulk")
|
|
|
79 |
verbosity = st.radio("๐ฃ๏ธ Response Style", ["Brief", "Detailed"])
|
80 |
voice_lang = st.selectbox("๐ Voice Language", ["en", "fr", "es", "de", "hi", "zh"])
|
81 |
|
82 |
-
# TTS
|
83 |
def speak(text, lang='en'):
|
84 |
tts = gTTS(text, lang=lang)
|
85 |
mp3 = BytesIO()
|
@@ -89,13 +90,15 @@ def speak(text, lang='en'):
|
|
89 |
mp3.seek(0)
|
90 |
return mp3
|
91 |
|
|
|
92 |
tab1, tab2 = st.tabs(["๐ง Single Review", "๐ Bulk CSV"])
|
93 |
|
94 |
-
#
|
|
|
|
|
95 |
with tab1:
|
96 |
st.title("๐ง NeuroPulse AI โ Multimodal Review Analyzer")
|
97 |
st.markdown("<div style='font-size:16px;color:#888;'>Minimum 20โ50 words recommended.</div>", unsafe_allow_html=True)
|
98 |
-
|
99 |
review = st.text_area("๐ Enter Review", value=st.session_state.review, height=180)
|
100 |
|
101 |
col1, col2, col3 = st.columns(3)
|
@@ -132,7 +135,8 @@ with tab1:
|
|
132 |
"follow_up": None,
|
133 |
"product_category": product_category,
|
134 |
"verbosity": verbosity,
|
135 |
-
"intelligence": st.session_state.intelligence_mode
|
|
|
136 |
}
|
137 |
headers = {"x-api-key": st.session_state.get("api_token", api_token)}
|
138 |
params = {"smart": "1"} if use_smart_summary else {}
|
@@ -153,14 +157,12 @@ with tab1:
|
|
153 |
st.metric("๐ Sentiment", data["sentiment"]["label"], delta=f"{data['sentiment']['score']:.2%}")
|
154 |
st.info(f"๐ข Emotion: {data['emotion']}")
|
155 |
|
156 |
-
if data.get("
|
157 |
-
st.subheader("
|
158 |
-
|
159 |
-
st.write(f"๐น {a['aspect']}: {a['sentiment']} ({a['score']:.2%})")
|
160 |
|
161 |
-
# --- Follow-Up Section ---
|
162 |
st.markdown("### ๐ Got questions?")
|
163 |
-
st.info("๐ฌ
|
164 |
sample_questions = [
|
165 |
"What did the user like most?",
|
166 |
"Any complaints mentioned?",
|
@@ -178,7 +180,11 @@ with tab1:
|
|
178 |
follow = res.json().get("follow_up")
|
179 |
if follow:
|
180 |
st.subheader("๐ Follow-Up Answer")
|
181 |
-
|
|
|
|
|
|
|
|
|
182 |
else:
|
183 |
st.error(f"โ Follow-up failed: {res.json().get('detail')}")
|
184 |
else:
|
@@ -186,12 +192,14 @@ with tab1:
|
|
186 |
except Exception as e:
|
187 |
st.error(f"๐ซ Exception occurred: {e}")
|
188 |
|
189 |
-
#
|
|
|
|
|
190 |
with tab2:
|
191 |
st.title("๐ Bulk CSV Upload")
|
192 |
st.markdown("""
|
193 |
Upload a CSV with the following columns:<br>
|
194 |
-
<code>review</code> (required), <code>industry</code>, <code>product_category</code>, <code>device</code> (optional)
|
195 |
""", unsafe_allow_html=True)
|
196 |
|
197 |
with st.expander("๐ Sample CSV"):
|
@@ -210,7 +218,7 @@ with tab2:
|
|
210 |
st.error("CSV must contain a `review` column.")
|
211 |
else:
|
212 |
st.success(f"โ
Loaded {len(df)} reviews")
|
213 |
-
for col in ["industry", "product_category", "device"]:
|
214 |
if col not in df.columns:
|
215 |
df[col] = ["Auto-detect"] * len(df)
|
216 |
df[col] = df[col].fillna("Auto-detect").astype(str)
|
@@ -229,6 +237,8 @@ with tab2:
|
|
229 |
"industry": df["industry"].tolist(),
|
230 |
"product_category": df["product_category"].tolist(),
|
231 |
"device": df["device"].tolist(),
|
|
|
|
|
232 |
"intelligence": st.session_state.intelligence_mode,
|
233 |
}
|
234 |
res = requests.post(
|
|
|
1 |
+
# --- import statements remain unchanged ---
|
2 |
import streamlit as st
|
3 |
import requests
|
4 |
import pandas as pd
|
|
|
9 |
import os
|
10 |
import plotly.express as px
|
11 |
|
12 |
+
# --- Config ---
|
13 |
st.set_page_config(page_title="NeuroPulse AI", page_icon="๐ง ", layout="wide")
|
|
|
|
|
14 |
logo_path = "logo.png"
|
15 |
if os.path.exists(logo_path):
|
16 |
st.image(logo_path, width=180)
|
17 |
|
18 |
+
# --- Session State ---
|
19 |
if "review" not in st.session_state:
|
20 |
st.session_state.review = ""
|
21 |
if "dark_mode" not in st.session_state:
|
|
|
25 |
if "trigger_example_analysis" not in st.session_state:
|
26 |
st.session_state.trigger_example_analysis = False
|
27 |
|
28 |
+
# --- Dark Mode Styling ---
|
29 |
if st.session_state.dark_mode:
|
30 |
st.markdown("""
|
31 |
<style>
|
|
|
44 |
</style>
|
45 |
""", unsafe_allow_html=True)
|
46 |
|
47 |
+
# --- Sidebar ---
|
48 |
with st.sidebar:
|
49 |
st.header("โ๏ธ Global Settings")
|
50 |
st.session_state.dark_mode = st.toggle("๐ Dark Mode", value=st.session_state.dark_mode)
|
|
|
76 |
use_aspects = st.checkbox("๐ฌ Enable Aspect Analysis")
|
77 |
use_smart_summary = st.checkbox("๐ง Smart Summary (Single)")
|
78 |
use_smart_summary_bulk = st.checkbox("๐ง Smart Summary for Bulk")
|
79 |
+
use_explain_bulk = st.checkbox("๐ง Generate Explanations (Bulk)")
|
80 |
verbosity = st.radio("๐ฃ๏ธ Response Style", ["Brief", "Detailed"])
|
81 |
voice_lang = st.selectbox("๐ Voice Language", ["en", "fr", "es", "de", "hi", "zh"])
|
82 |
|
83 |
+
# --- TTS ---
|
84 |
def speak(text, lang='en'):
|
85 |
tts = gTTS(text, lang=lang)
|
86 |
mp3 = BytesIO()
|
|
|
90 |
mp3.seek(0)
|
91 |
return mp3
|
92 |
|
93 |
+
# --- Tabs ---
|
94 |
tab1, tab2 = st.tabs(["๐ง Single Review", "๐ Bulk CSV"])
|
95 |
|
96 |
+
# -------------------
|
97 |
+
# SINGLE REVIEW TAB
|
98 |
+
# -------------------
|
99 |
with tab1:
|
100 |
st.title("๐ง NeuroPulse AI โ Multimodal Review Analyzer")
|
101 |
st.markdown("<div style='font-size:16px;color:#888;'>Minimum 20โ50 words recommended.</div>", unsafe_allow_html=True)
|
|
|
102 |
review = st.text_area("๐ Enter Review", value=st.session_state.review, height=180)
|
103 |
|
104 |
col1, col2, col3 = st.columns(3)
|
|
|
135 |
"follow_up": None,
|
136 |
"product_category": product_category,
|
137 |
"verbosity": verbosity,
|
138 |
+
"intelligence": st.session_state.intelligence_mode,
|
139 |
+
"explain": True
|
140 |
}
|
141 |
headers = {"x-api-key": st.session_state.get("api_token", api_token)}
|
142 |
params = {"smart": "1"} if use_smart_summary else {}
|
|
|
157 |
st.metric("๐ Sentiment", data["sentiment"]["label"], delta=f"{data['sentiment']['score']:.2%}")
|
158 |
st.info(f"๐ข Emotion: {data['emotion']}")
|
159 |
|
160 |
+
if data.get("explanation"):
|
161 |
+
st.subheader("๐งฎ Explanation")
|
162 |
+
st.markdown(data["explanation"])
|
|
|
163 |
|
|
|
164 |
st.markdown("### ๐ Got questions?")
|
165 |
+
st.info("๐ฌ Ask a follow-up question about this review.")
|
166 |
sample_questions = [
|
167 |
"What did the user like most?",
|
168 |
"Any complaints mentioned?",
|
|
|
180 |
follow = res.json().get("follow_up")
|
181 |
if follow:
|
182 |
st.subheader("๐ Follow-Up Answer")
|
183 |
+
if isinstance(follow, list):
|
184 |
+
for q in follow:
|
185 |
+
st.write("โก๏ธ", q)
|
186 |
+
else:
|
187 |
+
st.warning(follow)
|
188 |
else:
|
189 |
st.error(f"โ Follow-up failed: {res.json().get('detail')}")
|
190 |
else:
|
|
|
192 |
except Exception as e:
|
193 |
st.error(f"๐ซ Exception occurred: {e}")
|
194 |
|
195 |
+
# -------------------
|
196 |
+
# BULK CSV TAB
|
197 |
+
# -------------------
|
198 |
with tab2:
|
199 |
st.title("๐ Bulk CSV Upload")
|
200 |
st.markdown("""
|
201 |
Upload a CSV with the following columns:<br>
|
202 |
+
<code>review</code> (required), <code>industry</code>, <code>product_category</code>, <code>device</code>, <code>follow_up</code> (optional)
|
203 |
""", unsafe_allow_html=True)
|
204 |
|
205 |
with st.expander("๐ Sample CSV"):
|
|
|
218 |
st.error("CSV must contain a `review` column.")
|
219 |
else:
|
220 |
st.success(f"โ
Loaded {len(df)} reviews")
|
221 |
+
for col in ["industry", "product_category", "device", "follow_up"]:
|
222 |
if col not in df.columns:
|
223 |
df[col] = ["Auto-detect"] * len(df)
|
224 |
df[col] = df[col].fillna("Auto-detect").astype(str)
|
|
|
237 |
"industry": df["industry"].tolist(),
|
238 |
"product_category": df["product_category"].tolist(),
|
239 |
"device": df["device"].tolist(),
|
240 |
+
"follow_up": df["follow_up"].tolist(),
|
241 |
+
"explain": use_explain_bulk,
|
242 |
"intelligence": st.session_state.intelligence_mode,
|
243 |
}
|
244 |
res = requests.post(
|