Spaces:
Running
Running
Update frontend.py
Browse files- frontend.py +30 -27
frontend.py
CHANGED
@@ -12,19 +12,20 @@ st.set_page_config(page_title="NeuroPulse AI", page_icon="π§ ", layout="wide")
|
|
12 |
if os.path.exists("logo.png"):
|
13 |
st.image("logo.png", width=180)
|
14 |
|
15 |
-
# Session
|
16 |
-
|
17 |
"review": "",
|
18 |
"dark_mode": False,
|
19 |
"intelligence_mode": True,
|
20 |
"trigger_example_analysis": False,
|
21 |
"last_response": None,
|
22 |
-
"followup_answer": None
|
23 |
-
}
|
24 |
-
|
25 |
-
|
|
|
26 |
|
27 |
-
# Dark mode
|
28 |
if st.session_state.dark_mode:
|
29 |
st.markdown("""
|
30 |
<style>
|
@@ -43,7 +44,7 @@ if st.session_state.dark_mode:
|
|
43 |
</style>
|
44 |
""", unsafe_allow_html=True)
|
45 |
|
46 |
-
# Sidebar
|
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)
|
@@ -52,18 +53,21 @@ with st.sidebar:
|
|
52 |
api_token = st.text_input("π API Token", value="my-secret-key", type="password")
|
53 |
if not api_token or api_token.strip() == "my-secret-key":
|
54 |
st.warning("π§ͺ Running in demo mode β for full access, enter a valid API key.")
|
|
|
55 |
backend_url = st.text_input("π Backend URL", value="http://localhost:8000")
|
56 |
|
57 |
sentiment_model = st.selectbox("π Sentiment Model", [
|
58 |
-
"distilbert-base-uncased-finetuned-sst-2-english",
|
59 |
"nlptown/bert-base-multilingual-uncased-sentiment"
|
60 |
])
|
61 |
industry = st.selectbox("π Industry", ["Auto-detect", "Generic", "E-commerce", "Healthcare", "Education"])
|
62 |
product_category = st.selectbox("π§© Product Category", ["Auto-detect", "General", "Mobile Devices", "Laptops"])
|
|
|
|
|
63 |
verbosity = st.radio("π£οΈ Response Style", ["Brief", "Detailed"])
|
64 |
voice_lang = st.selectbox("π Voice Language", ["en", "fr", "es", "de", "hi", "zh"])
|
65 |
|
66 |
-
# TTS
|
67 |
def speak(text, lang='en'):
|
68 |
tts = gTTS(text, lang=lang)
|
69 |
mp3 = BytesIO()
|
@@ -73,10 +77,9 @@ def speak(text, lang='en'):
|
|
73 |
mp3.seek(0)
|
74 |
return mp3
|
75 |
|
76 |
-
# Tabs
|
77 |
tab1, tab2 = st.tabs(["π§ Single Review", "π Bulk CSV"])
|
78 |
|
79 |
-
#
|
80 |
with tab1:
|
81 |
st.title("π§ NeuroPulse AI β Multimodal Review Analyzer")
|
82 |
st.markdown("<div style='font-size:16px;color:#888;'>Minimum 20β50 words recommended.</div>", unsafe_allow_html=True)
|
@@ -106,12 +109,14 @@ with tab1:
|
|
106 |
st.session_state.followup_answer = None
|
107 |
with st.spinner("Analyzing..."):
|
108 |
try:
|
|
|
109 |
payload = {
|
110 |
"text": st.session_state.review,
|
111 |
-
"model":
|
112 |
"industry": industry,
|
113 |
"product_category": product_category,
|
114 |
"verbosity": verbosity,
|
|
|
115 |
"intelligence": st.session_state.intelligence_mode
|
116 |
}
|
117 |
headers = {"x-api-key": api_token}
|
@@ -127,7 +132,7 @@ with tab1:
|
|
127 |
if data:
|
128 |
st.subheader("π Summary")
|
129 |
st.info(data["summary"])
|
130 |
-
st.caption(f"π§ Summary
|
131 |
st.markdown(f"**Context:** `{data['industry']}` | `{data['product_category']}` | `Web`")
|
132 |
|
133 |
st.metric("π Sentiment", data["sentiment"]["label"], delta=f"{data['sentiment']['score']:.2%}")
|
@@ -149,7 +154,7 @@ with tab1:
|
|
149 |
"question": custom_q,
|
150 |
"verbosity": verbosity
|
151 |
}
|
152 |
-
headers = {"x-api-key":
|
153 |
res = requests.post(f"{backend_url}/followup/", json=follow_payload, headers=headers)
|
154 |
if res.status_code == 200:
|
155 |
st.session_state.followup_answer = res.json().get("answer")
|
@@ -162,12 +167,12 @@ with tab1:
|
|
162 |
st.subheader("π Follow-Up Answer")
|
163 |
st.success(st.session_state.followup_answer)
|
164 |
|
165 |
-
#
|
166 |
with tab2:
|
167 |
st.title("π Bulk CSV Upload")
|
168 |
st.markdown("""
|
169 |
-
|
170 |
-
|
171 |
""", unsafe_allow_html=True)
|
172 |
|
173 |
with st.expander("π Sample CSV"):
|
@@ -185,7 +190,6 @@ with tab2:
|
|
185 |
if "review" not in df.columns:
|
186 |
st.error("CSV must contain a `review` column.")
|
187 |
else:
|
188 |
-
st.success(f"β
Loaded {len(df)} reviews")
|
189 |
for col in ["industry", "product_category", "device", "follow_up"]:
|
190 |
if col not in df.columns:
|
191 |
df[col] = ["Auto-detect"] * len(df)
|
@@ -200,17 +204,16 @@ with tab2:
|
|
200 |
try:
|
201 |
payload = {
|
202 |
"reviews": df["review"].tolist(),
|
203 |
-
"model": sentiment_model,
|
204 |
"industry": df["industry"].tolist(),
|
205 |
"product_category": df["product_category"].tolist(),
|
206 |
"device": df["device"].tolist(),
|
207 |
"follow_up": df["follow_up"].tolist(),
|
208 |
-
"
|
|
|
|
|
209 |
}
|
210 |
-
res = requests.post(
|
211 |
-
f"{backend_url}/bulk/?token={api_token}",
|
212 |
-
json=payload
|
213 |
-
)
|
214 |
if res.status_code == 200:
|
215 |
results = pd.DataFrame(res.json()["results"])
|
216 |
st.dataframe(results)
|
@@ -219,8 +222,8 @@ with tab2:
|
|
219 |
st.plotly_chart(fig)
|
220 |
st.download_button("β¬οΈ Download Results CSV", results.to_csv(index=False), "results.csv", mime="text/csv")
|
221 |
else:
|
222 |
-
st.error(f"β Bulk Error {res.status_code}: {res.json().get('detail'
|
223 |
except Exception as e:
|
224 |
-
st.error(f"π¨ Processing Error: {e}")
|
225 |
except Exception as e:
|
226 |
st.error(f"β File Read Error: {e}")
|
|
|
12 |
if os.path.exists("logo.png"):
|
13 |
st.image("logo.png", width=180)
|
14 |
|
15 |
+
# Session state setup
|
16 |
+
defaults = {
|
17 |
"review": "",
|
18 |
"dark_mode": False,
|
19 |
"intelligence_mode": True,
|
20 |
"trigger_example_analysis": False,
|
21 |
"last_response": None,
|
22 |
+
"followup_answer": None
|
23 |
+
}
|
24 |
+
for k, v in defaults.items():
|
25 |
+
if k not in st.session_state:
|
26 |
+
st.session_state[k] = v
|
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 settings
|
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)
|
|
|
53 |
api_token = st.text_input("π API Token", value="my-secret-key", type="password")
|
54 |
if not api_token or api_token.strip() == "my-secret-key":
|
55 |
st.warning("π§ͺ Running in demo mode β for full access, enter a valid API key.")
|
56 |
+
|
57 |
backend_url = st.text_input("π Backend URL", value="http://localhost:8000")
|
58 |
|
59 |
sentiment_model = st.selectbox("π Sentiment Model", [
|
60 |
+
"Auto-detect", "distilbert-base-uncased-finetuned-sst-2-english",
|
61 |
"nlptown/bert-base-multilingual-uncased-sentiment"
|
62 |
])
|
63 |
industry = st.selectbox("π Industry", ["Auto-detect", "Generic", "E-commerce", "Healthcare", "Education"])
|
64 |
product_category = st.selectbox("π§© Product Category", ["Auto-detect", "General", "Mobile Devices", "Laptops"])
|
65 |
+
use_aspects = st.checkbox("π¬ Enable Aspect Analysis")
|
66 |
+
use_explain_bulk = st.checkbox("π§ Generate Explanations (Bulk)")
|
67 |
verbosity = st.radio("π£οΈ Response Style", ["Brief", "Detailed"])
|
68 |
voice_lang = st.selectbox("π Voice Language", ["en", "fr", "es", "de", "hi", "zh"])
|
69 |
|
70 |
+
# TTS
|
71 |
def speak(text, lang='en'):
|
72 |
tts = gTTS(text, lang=lang)
|
73 |
mp3 = BytesIO()
|
|
|
77 |
mp3.seek(0)
|
78 |
return mp3
|
79 |
|
|
|
80 |
tab1, tab2 = st.tabs(["π§ Single Review", "π Bulk CSV"])
|
81 |
|
82 |
+
# ==== SINGLE REVIEW ====
|
83 |
with tab1:
|
84 |
st.title("π§ NeuroPulse AI β Multimodal Review Analyzer")
|
85 |
st.markdown("<div style='font-size:16px;color:#888;'>Minimum 20β50 words recommended.</div>", unsafe_allow_html=True)
|
|
|
109 |
st.session_state.followup_answer = None
|
110 |
with st.spinner("Analyzing..."):
|
111 |
try:
|
112 |
+
model = None if sentiment_model == "Auto-detect" else sentiment_model
|
113 |
payload = {
|
114 |
"text": st.session_state.review,
|
115 |
+
"model": model or "distilbert-base-uncased-finetuned-sst-2-english",
|
116 |
"industry": industry,
|
117 |
"product_category": product_category,
|
118 |
"verbosity": verbosity,
|
119 |
+
"aspects": use_aspects,
|
120 |
"intelligence": st.session_state.intelligence_mode
|
121 |
}
|
122 |
headers = {"x-api-key": api_token}
|
|
|
132 |
if data:
|
133 |
st.subheader("π Summary")
|
134 |
st.info(data["summary"])
|
135 |
+
st.caption(f"π§ Summary | {verbosity} response")
|
136 |
st.markdown(f"**Context:** `{data['industry']}` | `{data['product_category']}` | `Web`")
|
137 |
|
138 |
st.metric("π Sentiment", data["sentiment"]["label"], delta=f"{data['sentiment']['score']:.2%}")
|
|
|
154 |
"question": custom_q,
|
155 |
"verbosity": verbosity
|
156 |
}
|
157 |
+
headers = {"x-api-key": api_token}
|
158 |
res = requests.post(f"{backend_url}/followup/", json=follow_payload, headers=headers)
|
159 |
if res.status_code == 200:
|
160 |
st.session_state.followup_answer = res.json().get("answer")
|
|
|
167 |
st.subheader("π Follow-Up Answer")
|
168 |
st.success(st.session_state.followup_answer)
|
169 |
|
170 |
+
# ==== BULK CSV ====
|
171 |
with tab2:
|
172 |
st.title("π Bulk CSV Upload")
|
173 |
st.markdown("""
|
174 |
+
Upload a CSV with columns:<br>
|
175 |
+
<code>review</code>, <code>industry</code>, <code>product_category</code>, <code>device</code>, <code>follow_up</code> (optional)
|
176 |
""", unsafe_allow_html=True)
|
177 |
|
178 |
with st.expander("π Sample CSV"):
|
|
|
190 |
if "review" not in df.columns:
|
191 |
st.error("CSV must contain a `review` column.")
|
192 |
else:
|
|
|
193 |
for col in ["industry", "product_category", "device", "follow_up"]:
|
194 |
if col not in df.columns:
|
195 |
df[col] = ["Auto-detect"] * len(df)
|
|
|
204 |
try:
|
205 |
payload = {
|
206 |
"reviews": df["review"].tolist(),
|
207 |
+
"model": None if sentiment_model == "Auto-detect" else sentiment_model,
|
208 |
"industry": df["industry"].tolist(),
|
209 |
"product_category": df["product_category"].tolist(),
|
210 |
"device": df["device"].tolist(),
|
211 |
"follow_up": df["follow_up"].tolist(),
|
212 |
+
"explain": use_explain_bulk,
|
213 |
+
"aspects": use_aspects,
|
214 |
+
"intelligence": st.session_state.intelligence_mode
|
215 |
}
|
216 |
+
res = requests.post(f"{backend_url}/bulk/?token={api_token}", json=payload)
|
|
|
|
|
|
|
217 |
if res.status_code == 200:
|
218 |
results = pd.DataFrame(res.json()["results"])
|
219 |
st.dataframe(results)
|
|
|
222 |
st.plotly_chart(fig)
|
223 |
st.download_button("β¬οΈ Download Results CSV", results.to_csv(index=False), "results.csv", mime="text/csv")
|
224 |
else:
|
225 |
+
st.error(f"β Bulk Error {res.status_code}: {res.json().get('detail')}")
|
226 |
except Exception as e:
|
227 |
+
st.error(f"π¨ Bulk Processing Error: {e}")
|
228 |
except Exception as e:
|
229 |
st.error(f"β File Read Error: {e}")
|