# Paste your full Streamlit code here # Include: # - Chat UI # - Preset questions # - Filters by rating # - Analytics charts (Rating & Sentiment) # - RAG chain with Groq LLM # - Executive summary generation # - PDF and CSV download import streamlit as st import os import pandas as pd import tempfile from langchain_groq import ChatGroq from langchain_community.vectorstores import FAISS from langchain_community.embeddings import HuggingFaceEmbeddings from langchain_core.prompts import PromptTemplate from langchain_core.runnables import RunnablePassthrough from reportlab.platypus import SimpleDocTemplate, Paragraph from reportlab.lib.styles import getSampleStyleSheet # ================================ # Constants / Config # ================================ VECTORSTORE_PATH = "vectorstore" # -------------------------------------------------- # PAGE CONFIG # -------------------------------------------------- st.set_page_config( page_title="E-Commerce Review Intelligence", layout="wide" ) st.title("🛍️ Customer Review Intelligence System") st.caption("GenAI-powered insights using RAG (Groq + FAISS)") # -------------------------------------------------- # SIDEBAR # -------------------------------------------------- st.sidebar.header("🔐 Configuration") groq_key = st.sidebar.text_input("Groq API Key", type="password") if not groq_key: st.warning("Please enter your Groq API key") st.stop() os.environ["GROQ_API_KEY"] = groq_key rating_filter = st.sidebar.selectbox( "Filter by Rating", ["All", "Low (1–2)", "Medium (3)", "High (4–5)"] ) # -------------------------------------------------- # LOAD DATA # -------------------------------------------------- @st.cache_data def load_data(): df = pd.read_csv("Womens Clothing E-Commerce Reviews.csv") df = df.dropna(subset=["Review Text"]) return df df = load_data() def map_sentiment(r): if r <= 2: return "Negative" elif r == 3: return "Neutral" else: return "Positive" df["Sentiment"] = df["Rating"].apply(map_sentiment) # -------------------------------------------------- # ANALYTICS # -------------------------------------------------- st.subheader("📊 Customer Sentiment Overview") col1, col2 = st.columns(2) with col1: st.bar_chart(df["Rating"].value_counts().sort_index()) with col2: st.bar_chart(df["Sentiment"].value_counts()) # -------------------------------------------------- # LOAD VECTOR STORE # -------------------------------------------------- @st.cache_resource from langchain_community.embeddings import HuggingFaceEmbeddings from langchain_community.vectorstores import FAISS def load_vectorstore(): embeddings = HuggingFaceEmbeddings( model_name="sentence-transformers/all-MiniLM-L6-v2" ) return FAISS.load_local( VECTORSTORE_PATH, embeddings, allow_dangerous_deserialization=True ) vectorstore = load_vectorstore() retriever = vectorstore.as_retriever(search_kwargs={"k": 5}) # -------------------------------------------------- # LOAD LLM # -------------------------------------------------- llm = ChatGroq( model="llama-3.1-8b-instant", temperature=0 ) # -------------------------------------------------- # RAG PROMPT # -------------------------------------------------- rag_prompt = PromptTemplate( input_variables=["context", "question", "rating"], template=""" You are an AI assistant for an e-commerce company. Rating Filter: {rating} Customer Reviews: {context} Question: {question} Provide clear, actionable business insights. """ ) rag_chain = ( { "context": retriever, "question": RunnablePassthrough(), "rating": RunnablePassthrough() } | rag_prompt | llm ) # -------------------------------------------------- # CHAT UI # -------------------------------------------------- if "messages" not in st.session_state: st.session_state.messages = [] st.subheader("💬 Ask Questions") preset = st.selectbox( "Quick Business Questions", [ "What are the most common customer complaints?", "What do customers say about sizing?", "Are customers satisfied with fabric quality?", "What improvements should the business prioritize?" ] ) user_input = st.chat_input("Ask your own question") question = user_input if user_input else preset if question: st.session_state.messages.append( {"role": "user", "content": question} ) response = rag_chain.invoke( {"question": question, "rating": rating_filter} ) st.session_state.messages.append( {"role": "assistant", "content": response.content} ) for msg in st.session_state.messages: st.chat_message(msg["role"]).write(msg["content"]) # -------------------------------------------------- # EXECUTIVE SUMMARY # -------------------------------------------------- st.subheader("📄 Executive Summary") exec_prompt = PromptTemplate( input_variables=["reviews"], template=""" Generate an executive summary covering: - Overall sentiment - Top complaints - Key strengths - Business recommendations Reviews: {reviews} """ ) summary_text = None if st.button("Generate Executive Summary"): sample_reviews = " ".join(df["Review Text"].sample(30)) summary_text = (exec_prompt | llm).invoke( {"reviews": sample_reviews} ).content st.success(summary_text) # -------------------------------------------------- # PDF EXPORT # -------------------------------------------------- def generate_pdf(text): styles = getSampleStyleSheet() temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") doc = SimpleDocTemplate(temp_file.name) doc.build([ Paragraph("Executive Summary", styles["Title"]), Paragraph(text.replace("\n", "
"), styles["Normal"]) ]) return temp_file.name if summary_text: pdf_path = generate_pdf(summary_text) with open(pdf_path, "rb") as f: st.download_button( "📥 Download Executive Summary (PDF)", f, file_name="executive_summary.pdf", mime="application/pdf" ) # -------------------------------------------------- # CSV EXPORT # -------------------------------------------------- st.subheader("📥 Download Review Data") csv = df[["Review Text", "Rating", "Sentiment"]].to_csv(index=False).encode("utf-8") st.download_button( "Download CSV", csv, "customer_reviews.csv", "text/csv" ) st.write("STREAMLIT APP IS RUNNING SUCCESSFULLY")