-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
303 lines (253 loc) · 8.79 KB
/
streamlit_app.py
File metadata and controls
303 lines (253 loc) · 8.79 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import asyncio
from dotenv import load_dotenv
import os
# Load environment variables from config/config.env
load_dotenv("config/config.env")
if not os.getenv("GOOGLE_API_KEY"):
raise ValueError("❌ GOOGLE_API_KEY not found in config/config.env!")
# Fix "no running event loop" issue for gRPC
try:
asyncio.get_running_loop()
except RuntimeError:
asyncio.set_event_loop(asyncio.new_event_loop())
import streamlit as st
import pandas as pd
from langchain.chains import ConversationalRetrievalChain
from langchain_google_genai import ChatGoogleGenerativeAI
from utils.vector_store import load_vector_store
from ddgs import DDGS
# ---- CONFIG ----
STORE_PATH = "C:/Users/Admin/Downloads/NeoStats AI Engineer Internship Use Case/AI_UseCase/vectorstore/faiss_store"
st.set_page_config(page_title="FinBuddy", page_icon="💰", layout="wide")
if "history" not in st.session_state:
st.session_state.history = []
if "chat_memory" not in st.session_state:
st.session_state.chat_memory = []
# ---- STYLING ----
st.markdown("""
<style>
html, body {
background: linear-gradient(to right, #e0f7fa, #ffffff);
font-family: 'Segoe UI', sans-serif;
color: #333;
}
h1, h2, h3, h4 {
color: #004d40;
}
.block-container {
padding-top: 1rem;
}
.chat-container {
background: #ffffff;
border-radius: 15px;
padding: 20px;
box-shadow: 0 8px 20px rgba(0,0,0,0.05);
margin-top: 20px;
max-height: 70vh;
overflow-y: auto;
}
.chat-bubble {
padding: 14px 20px;
border-radius: 20px;
margin-bottom: 10px;
animation: fadeIn 0.5s ease-in-out;
line-height: 1.5;
box-shadow: 0 2px 6px rgba(0,0,0,0.1);
}
.user {
background: linear-gradient(to right, #00bcd4, #009688);
color: white;
align-self: flex-end;
border-bottom-right-radius: 5px;
margin-left: auto;
text-align: right;
}
.bot {
background: #f1f8e9;
color: #333;
align-self: flex-start;
border-bottom-left-radius: 5px;
margin-right: auto;
}
@keyframes fadeIn {
from {opacity: 0; transform: translateY(10px);}
to {opacity: 1; transform: translateY(0);}
}
.stButton>button {
background: linear-gradient(135deg, #00bfa5, #00796b);
color: white;
font-weight: 600;
border: none;
border-radius: 25px;
padding: 10px 20px;
font-size: 15px;
box-shadow: 0 4px 12px rgba(0, 121, 107, 0.3);
transition: all 0.3s ease;
}
.stButton>button:hover {
background: linear-gradient(135deg, #004d40, #00695c);
box-shadow: 0 6px 20px rgba(0, 77, 64, 0.5);
transform: scale(1.03);
}
div.stButton > button[kind="primary"] {
background: linear-gradient(to right, #4caf50, #2e7d32);
color: white;
font-weight: 700;
padding: 10px 25px;
font-size: 16px;
border-radius: 30px;
box-shadow: 0 5px 15px rgba(76, 175, 80, 0.4);
}
div.stButton > button[kind="primary"]:hover {
background: linear-gradient(to right, #66bb6a, #388e3c);
box-shadow: 0 8px 25px rgba(56, 142, 60, 0.6);
transform: scale(1.05);
}
.right-column {
background: #ffffff;
border-radius: 18px;
padding: 20px;
box-shadow: 0 6px 20px rgba(0,0,0,0.08);
margin-top: 20px;
}
table {
border-collapse: collapse;
width: 100%;
font-size: 14px;
}
th, td {
border: 1px solid #ddd;
padding: 10px 12px;
}
th {
background-color: #e0f2f1;
color: #00695c;
text-align: left;
}
td {
background-color: #fafafa;
}
.stNumberInput input {
background: #ffffff;
border-radius: 10px;
border: 1px solid #ccc;
padding: 10px;
}
.stAlert > div {
border-radius: 15px;
background-color: #e8f5e9;
color: #2e7d32;
border: 1px solid #a5d6a7;
}
.stChatInput input {
padding: 12px 20px;
border-radius: 25px;
border: 1px solid #ccc;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
</style>
""", unsafe_allow_html=True)
# ---- LOAD VECTOR STORE ----
try:
store = load_vector_store(STORE_PATH)
retriever = store.as_retriever() if store else None
except Exception as e:
st.warning(f"⚠️ Could not load vector store: {e}")
store = None
retriever = None
# ---- GEMINI CHAT MODEL ----
llm = ChatGoogleGenerativeAI(
model="gemini-2.5-flash-lite",
google_api_key=os.getenv("GOOGLE_API_KEY"),
temperature=0
)
qa_chain = None
if retriever:
qa_chain = ConversationalRetrievalChain.from_llm(
llm=llm,
retriever=retriever,
return_source_documents=True
)
# ---- LIVE WEB SEARCH ----
def live_web_search(query, max_results=3):
"""Fetch live search results from DuckDuckGo."""
with DDGS() as ddg:
results = list(ddg.text(query, max_results=max_results))
if results:
combined = "\n".join([f"{r['body']} (Source: {r['href']})" for r in results])
return combined
return "No live data found."
def rewrite_with_llm(query, raw_info):
"""Rewrite search results into a chatbot-friendly answer using Gemini."""
prompt = f"""
You are FinBuddy, a friendly BFSI loan assistant.
The user asked: "{query}"
I found the following web search results:
{raw_info}
Please provide a clear, concise, and helpful answer to the user,
using a natural conversational tone.
Include the source link if relevant.
"""
response = llm.invoke(prompt)
return response.content.strip()
# ---------------- UI ----------------
col1, col2 = st.columns([2, 1])
with col1:
st.title("💬 FinBuddy – Your BFSI Loan Assistant")
st.write("💡 Ask about loans, interest rates, EMIs, and more.")
for chat in st.session_state.history:
st.markdown(f"""
<div class='chat-bubble {'user' if chat['role'] == 'user' else 'bot'}'>
{chat['content']}
</div>
""", unsafe_allow_html=True)
user_input = st.chat_input("Type your question here...")
if user_input:
st.session_state.history.append({"role": "user", "content": user_input})
with st.spinner("💭 Thinking..."):
answer = ""
if qa_chain:
result = qa_chain.invoke({"question": user_input, "chat_history": st.session_state.chat_memory})
answer = result.get("answer", "").strip()
sources = result.get("source_documents", [])
else:
sources = []
no_info_phrases = [
"i don't know", "cannot answer", "doesn't specify", "not specified", "no information",
"no details provided", "i'm sorry, but i don't have", "sorry, but i don't have",
"sorry, i don't have", "no data available", "cannot find"
]
if (not sources or not answer or any(p in answer.lower() for p in no_info_phrases)):
st.warning("No relevant info found in internal DB. Searching live web...")
raw_search_result = live_web_search(user_input)
answer = rewrite_with_llm(user_input, raw_search_result)
st.session_state.chat_memory.append((user_input, answer))
st.session_state.history.append({"role": "assistant", "content": answer})
st.rerun()
if st.button("🗑 Clear Chat"):
st.session_state.history.clear()
st.session_state.chat_memory.clear()
st.rerun()
with col2:
st.markdown('<div class="right-column">', unsafe_allow_html=True)
st.subheader("📈 Interest Rate Trends")
st.table(pd.DataFrame({
"Loan Type": ["Home Loan", "Personal Loan", "Car Loan", "Education Loan"],
"Interest Rate (%)": [8.5, 12.5, 9.0, 10.5]
}))
st.subheader("🧲 EMI Calculator")
loan_amt = st.number_input("Loan Amount (₹)", value=500000, step=10000)
rate = st.number_input("Annual Interest Rate (%)", value=8.5, step=0.1)
tenure = st.number_input("Tenure (Years)", value=5, step=1)
if st.button("Calculate EMI", type="primary"):
monthly_rate = rate / 100 / 12
emi = loan_amt * monthly_rate * (1 + monthly_rate) ** (tenure * 12) / ((1 + monthly_rate) ** (tenure * 12) - 1)
st.success(f"Your EMI: ₹{emi:,.2f} per month")
st.subheader("🏦 Loan Highlights")
st.markdown("""
- **Home Loan:** Low-interest rates starting from 8.5%
- **Personal Loan:** Quick disbursal within 24 hours
- **Car Loan:** Flexible tenure up to 7 years
- **Education Loan:** Special student discounts
""")
st.markdown('</div>', unsafe_allow_html=True)