File size: 6,651 Bytes
6bfcd39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6783910
 
 
 
 
6bfcd39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6783910
 
 
6bfcd39
 
 
 
 
6783910
6bfcd39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# 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("<b>Executive Summary</b>", styles["Title"]),
        Paragraph(text.replace("\n", "<br/>"), 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")