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 st.set_page_config(page_title="NeuroPulse AI", page_icon="๐Ÿง ", layout="wide") logo_path = os.path.join("app", "static", "logo.png") if os.path.exists(logo_path): st.image(logo_path, width=160) # Session state if "history" not in st.session_state: st.session_state.history = [] if "dark_mode" not in st.session_state: st.session_state.dark_mode = False # Sidebar with st.sidebar: st.header("โš™๏ธ Settings") st.session_state.dark_mode = st.toggle("๐ŸŒ™ Dark Mode", value=st.session_state.dark_mode) sentiment_model = st.selectbox("๐Ÿ“Š Sentiment Model", [ "distilbert-base-uncased-finetuned-sst-2-english", "nlptown/bert-base-multilingual-uncased-sentiment" ]) industry = st.selectbox("๐Ÿญ Industry Context", [ "Generic", "E-commerce", "Healthcare", "Education", "Travel", "Banking", "Insurance" ]) product_category = st.selectbox("๐Ÿงฉ Product Category", [ "General", "Mobile Devices", "Laptops", "Healthcare Devices", "Banking App", "Travel Service", "Educational Tool", "Insurance Portal" ]) device_type = st.selectbox("๐Ÿ’ป Device Type", [ "Web", "Android", "iOS", "Desktop", "Smartwatch", "Kiosk" ]) use_aspects = st.checkbox("๐Ÿ“ˆ Enable Aspect-Based Analysis") use_smart_summary = st.checkbox("๐Ÿง  Use Smart Summary (clustered key points)") use_smart_summary_bulk = st.checkbox("๐Ÿง  Smart Summary for Bulk CSV") follow_up = st.text_input("๐Ÿ” Follow-up Question") voice_lang = st.selectbox("๐Ÿ”ˆ Voice Language", ["en", "fr", "es", "de", "hi", "zh"]) backend_url = st.text_input("๐Ÿ–ฅ๏ธ Backend URL", value="http://0.0.0.0:8000") api_token = st.text_input("๐Ÿ” API Token", type="password") # Tabs tab1, tab2 = st.tabs(["๐Ÿง  Single Review", "๐Ÿ“š Bulk CSV"]) 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 # Tab: Single Review with tab1: st.title("๐Ÿง  NeuroPulse AI โ€“ Multimodal Review Analyzer") review = st.session_state.get("review", "") review = st.text_area("๐Ÿ“ Enter a Review", value=review, height=160) col1, col2, col3 = st.columns(3) with col1: analyze = st.button("๐Ÿ” Analyze") with col2: if st.button("๐ŸŽฒ Example"): st.session_state["review"] = "App was smooth, but the transaction failed twice on Android." st.rerun() with col3: if st.button("๐Ÿงน Clear"): st.session_state["review"] = "" st.rerun() if analyze and review: with st.spinner("Analyzing..."): try: payload = { "text": review, "model": sentiment_model, "industry": industry, "aspects": use_aspects, "follow_up": follow_up, "product_category": product_category, "device": device_type } headers = {"X-API-Key": api_token} if api_token else {} params = {"smart": "1"} if use_smart_summary else {} res = requests.post(f"{backend_url}/analyze/", json=payload, headers=headers, params=params) if res.status_code == 200: data = res.json() st.success("โœ… Analysis Complete") st.subheader("๐Ÿ“Œ Summary") st.info(data["summary"]) st.caption(f"๐Ÿง  Summary Type: {'Smart Summary' if use_smart_summary else 'Standard Model'}") st.subheader("๐Ÿ”Š Audio") audio = speak(data["summary"], lang=voice_lang) st.download_button("โฌ‡๏ธ Download Summary Audio", audio.read(), "summary.mp3", mime="audio/mp3") st.metric("๐Ÿ“Š Sentiment", data["sentiment"]["label"], delta=f"{data['sentiment']['score']:.2%}") st.info(f"๐Ÿ’ข Emotion: {data['emotion']}") if data.get("aspects"): st.subheader("๐Ÿ”ฌ Aspects") for a in data["aspects"]: st.write(f"๐Ÿ”น {a['aspect']}: {a['sentiment']} ({a['score']:.2%})") if data.get("follow_up"): st.subheader("๐Ÿค– Follow-Up Response") st.warning(data["follow_up"]) else: st.error(f"โŒ API Error: {res.status_code}") except Exception as e: st.error(f"๐Ÿšซ {e}") # Tab: Bulk CSV with tab2: st.title("๐Ÿ“š Bulk CSV Upload") uploaded_file = st.file_uploader("Upload CSV with `review` column", type="csv") if uploaded_file: try: df = pd.read_csv(uploaded_file) if "review" in df.columns: st.success(f"โœ… Loaded {len(df)} reviews") for col in ["industry", "product_category", "device"]: if col not in df.columns: df[col] = [""] * len(df) df[col] = df[col].fillna("").astype(str) if st.button("๐Ÿ“Š Analyze Bulk Reviews"): with st.spinner("Processing..."): 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() } headers = {"X-API-Key": api_token} if api_token else {} params = {"smart": "1"} if use_smart_summary_bulk else {} res = requests.post(f"{backend_url}/bulk/", json=payload, headers=headers, params=params) if res.status_code == 200: results = pd.DataFrame(res.json()["results"]) results["summary_type"] = "Smart" if use_smart_summary_bulk else "Standard" st.dataframe(results) st.download_button("โฌ‡๏ธ Download Results CSV", results.to_csv(index=False), "bulk_results.csv", mime="text/csv") else: st.error(f"โŒ Bulk Analysis Failed: {res.status_code}") else: st.error("CSV must contain a column named `review`.") except Exception as e: st.error(f"โŒ File Error: {e}")