Update app.py
Browse files
app.py
CHANGED
@@ -7,46 +7,50 @@ faq_df = pd.read_csv("lic_faq.csv")
|
|
7 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
8 |
faq_embeddings = model.encode(faq_df['question'].tolist(), convert_to_tensor=True)
|
9 |
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
query_embedding = model.encode(query, convert_to_tensor=True)
|
12 |
scores = util.pytorch_cos_sim(query_embedding, faq_embeddings)[0]
|
13 |
-
best_score = float(scores.max())
|
14 |
best_idx = int(scores.argmax())
|
15 |
|
16 |
-
if best_score < 0.6:
|
17 |
-
|
18 |
-
"π€ I'm not confident I have the right answer for that.\n"
|
19 |
-
"Please ask a question related to LIC policies, claims, onboarding, or commissions."
|
20 |
-
)
|
21 |
else:
|
22 |
-
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
|
|
|
|
|
|
|
|
|
26 |
gr.Markdown(
|
27 |
-
""
|
28 |
-
<
|
29 |
-
<p style='text-align: center;'>Ask questions about LIC policies, commissions, claims, onboarding, and more!</p>
|
30 |
-
""",
|
31 |
-
elem_id="header"
|
32 |
)
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
with gr.Column(scale=1):
|
44 |
-
output = gr.Textbox(
|
45 |
-
label="Answer",
|
46 |
-
placeholder="Response will appear here...",
|
47 |
-
lines=6
|
48 |
-
)
|
49 |
-
|
50 |
-
submit_btn.click(fn=chatbot, inputs=user_input, outputs=output)
|
51 |
|
52 |
demo.launch()
|
|
|
7 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
8 |
faq_embeddings = model.encode(faq_df['question'].tolist(), convert_to_tensor=True)
|
9 |
|
10 |
+
# Sample policies to suggest (add more as needed)
|
11 |
+
policy_suggestions = {
|
12 |
+
"term": "π‘ You might consider LIC Tech Term Plan for pure protection at low cost.",
|
13 |
+
"money back": "π‘ LIC Money Back Policy is great for periodic returns along with insurance.",
|
14 |
+
"endowment": "π‘ LIC New Endowment Plan offers savings and insurance benefits together.",
|
15 |
+
"ulip": "π‘ LIC SIIP and Nivesh Plus are good ULIP options with market-linked returns.",
|
16 |
+
"pension": "π‘ LIC Jeevan Akshay and PM Vaya Vandana Yojana are best for pension seekers."
|
17 |
+
}
|
18 |
+
|
19 |
+
def chatbot(history, query):
|
20 |
query_embedding = model.encode(query, convert_to_tensor=True)
|
21 |
scores = util.pytorch_cos_sim(query_embedding, faq_embeddings)[0]
|
22 |
+
best_score = float(scores.max())
|
23 |
best_idx = int(scores.argmax())
|
24 |
|
25 |
+
if best_score < 0.6:
|
26 |
+
response = "π€ I'm not confident I have the right answer for that. Please ask about LIC policies, claims, commissions, onboarding, or KYC."
|
|
|
|
|
|
|
27 |
else:
|
28 |
+
response = faq_df.iloc[best_idx]['answer']
|
29 |
|
30 |
+
# Policy recommendation based on keywords
|
31 |
+
query_lower = query.lower()
|
32 |
+
for keyword, suggestion in policy_suggestions.items():
|
33 |
+
if keyword in query_lower:
|
34 |
+
response += f"\n\n{suggestion}"
|
35 |
+
break
|
36 |
|
37 |
+
history.append((query, response))
|
38 |
+
return history, history
|
39 |
+
|
40 |
+
# UI using Gradio Blocks
|
41 |
+
with gr.Blocks(title="LIC Agent Chatbot") as demo:
|
42 |
gr.Markdown(
|
43 |
+
"<h1 style='text-align:center;color:#0D47A1;'>π§βπΌ LIC Agent Assistant</h1>"
|
44 |
+
"<p style='text-align:center;'>Ask me anything about policies, claims, commissions, onboarding, and KYC.</p>"
|
|
|
|
|
|
|
45 |
)
|
46 |
|
47 |
+
chatbot_ui = gr.Chatbot(label="LIC Assistant", height=450)
|
48 |
+
msg = gr.Textbox(label="Your Question", placeholder="E.g., What is the commission for ULIP?")
|
49 |
+
clear = gr.Button("Clear Chat")
|
50 |
+
|
51 |
+
state = gr.State([])
|
52 |
+
|
53 |
+
msg.submit(chatbot, [state, msg], [chatbot_ui, state])
|
54 |
+
clear.click(lambda: ([], []), None, [chatbot_ui, state], queue=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
demo.launch()
|