import streamlit as st import requests import pandas as pd from gtts import gTTS import base64 from io import BytesIO from PIL import Image import os import plotly.express as px st.set_page_config(page_title="NeuroPulse AI", page_icon="๐ง ", layout="wide") # Load logo logo_path = "logo.png" if os.path.exists(logo_path): st.image(logo_path, width=180) # Session State defaults if "review" not in st.session_state: st.session_state.review = "" if "dark_mode" not in st.session_state: st.session_state.dark_mode = False if "intelligence_mode" not in st.session_state: st.session_state.intelligence_mode = True # Apply Dark Mode Styling if st.session_state.dark_mode: st.markdown(""" """, unsafe_allow_html=True) # Sidebar controls with st.sidebar: st.header("โ๏ธ Global Settings") st.session_state.dark_mode = st.toggle("๐ Dark Mode", value=st.session_state.dark_mode) st.session_state.intelligence_mode = st.toggle("๐ง Intelligence Mode", value=st.session_state.intelligence_mode) DEFAULT_DEMO_TOKEN = "my-secret-key" # ๐ก replace with secure demo token api_token = st.text_input("๐ API Token", value=DEFAULT_DEMO_TOKEN, type="password") # ๐ก Insert this warning right after the token field if not api_token or api_token.strip() == "my-secret-key": st.warning("๐งช Running in demo mode โ for full access, enter a valid API key.") backend_url = st.text_input("๐ Backend URL", value="http://localhost:8000") sentiment_model = st.selectbox("๐ Sentiment Model", [ "distilbert-base-uncased-finetuned-sst-2-english", "nlptown/bert-base-multilingual-uncased-sentiment" ]) industry = st.selectbox("๐ญ Industry", [ "Auto-detect", "Generic", "E-commerce", "Healthcare", "Education", "Travel", "Banking", "Insurance", "Gaming", "Food Delivery", "Real Estate", "Fitness", "Entertainment" ]) product_category = st.selectbox("๐งฉ Product Category", [ "Auto-detect", "General", "Mobile Devices", "Laptops", "Healthcare Devices", "Banking App", "Travel Service", "Educational Tool", "Insurance Portal", "Streaming App", "Wearables", "Home Appliances", "Food Apps" ]) use_aspects = st.checkbox("๐ฌ Enable Aspect Analysis") use_smart_summary = st.checkbox("๐ง Smart Summary (Single)") use_smart_summary_bulk = st.checkbox("๐ง Smart Summary for Bulk") verbosity = st.radio("๐ฃ๏ธ Response Style", ["Brief", "Detailed"]) follow_up = st.text_input("๐ Follow-up Question") voice_lang = st.selectbox("๐ Voice Language", ["en", "fr", "es", "de", "hi", "zh"]) # Text-to-Speech def speak(text, lang='en'): tts = gTTS(text, lang=lang) mp3 = BytesIO() tts.write_to_fp(mp3) b64 = base64.b64encode(mp3.getvalue()).decode() st.markdown(f'', unsafe_allow_html=True) mp3.seek(0) return mp3 tab1, tab2 = st.tabs(["๐ง Single Review", "๐ Bulk CSV"]) # --- SINGLE REVIEW --- with tab1: st.title("๐ง NeuroPulse AI โ Multimodal Review Analyzer") st.markdown("
review
(required), industry
, product_category
, device
(optional)
""", unsafe_allow_html=True)
with st.expander("๐ Sample CSV"):
with open("sample_reviews.csv", "rb") as f:
st.download_button("โฌ๏ธ Download sample CSV", f, file_name="sample_reviews.csv")
uploaded_file = st.file_uploader("๐ Upload your CSV", type="csv")
if uploaded_file:
if not api_token:
st.error("๐ Please enter your API token in the sidebar.")
else:
try:
df = pd.read_csv(uploaded_file)
if "review" not in df.columns:
st.error("CSV must contain a `review` column.")
else:
st.success(f"โ
Loaded {len(df)} reviews")
for col in ["industry", "product_category", "device"]:
if col not in df.columns:
df[col] = ["Auto-detect"] * len(df)
df[col] = df[col].fillna("Auto-detect").astype(str)
# Replace "Auto-detect" with fallback
df["industry"] = df["industry"].apply(lambda x: "Generic" if x.lower() == "auto-detect" else x)
df["product_category"] = df["product_category"].apply(lambda x: "General" if x.lower() == "auto-detect" else x)
df["device"] = df["device"].apply(lambda x: "Web" if x.lower() == "auto-detect" else x)
if st.button("๐ Analyze Bulk Reviews", use_container_width=True):
with st.spinner("Processing..."):
try:
payload = {
"reviews": df["review"].tolist(),
"model": sentiment_model,
"aspects": use_aspects,
"industry": df["industry"].tolist(),
"product_category": df["product_category"].tolist(),
"device": df["device"].tolist(),
"intelligence": st.session_state.intelligence_mode,
}
# โ
Updated: Pass token as query param (NOT in headers)
res = requests.post(
f"{backend_url}/bulk/?token={api_token}",
json=payload
)
if res.status_code == 200:
results = pd.DataFrame(res.json()["results"])
st.dataframe(results)
if "sentiment" in results:
fig = px.pie(results, names="sentiment", title="Sentiment Distribution")
st.plotly_chart(fig)
st.download_button("โฌ๏ธ Download Results CSV", results.to_csv(index=False), "results.csv", mime="text/csv")
else:
st.error(f"โ Bulk Error {res.status_code}: {res.json().get('detail', 'Unknown error')}")
except Exception as e:
st.error(f"๐จ Processing Error: {e}")
except Exception as e:
st.error(f"โ File Read Error: {e}")