Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ 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 |
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.",
|
|
@@ -16,28 +16,35 @@ policy_suggestions = {
|
|
| 16 |
"pension": "π‘ LIC Jeevan Akshay and PM Vaya Vandana Yojana are best for pension seekers."
|
| 17 |
}
|
| 18 |
|
|
|
|
| 19 |
def chatbot(history, query):
|
| 20 |
-
|
| 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 |
-
|
| 26 |
-
|
|
|
|
| 27 |
else:
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
history.append((query, response))
|
| 38 |
return history, history
|
| 39 |
|
| 40 |
-
#
|
| 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>"
|
|
@@ -45,12 +52,14 @@ with gr.Blocks(title="LIC Agent Chatbot") as demo:
|
|
| 45 |
)
|
| 46 |
|
| 47 |
chatbot_ui = gr.Chatbot(label="LIC Assistant", height=450)
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
clear = gr.Button("Clear Chat")
|
| 50 |
|
| 51 |
state = gr.State([])
|
| 52 |
|
| 53 |
-
|
| 54 |
clear.click(lambda: ([], []), None, [chatbot_ui, state], queue=False)
|
| 55 |
|
| 56 |
demo.launch()
|
|
|
|
| 7 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 8 |
faq_embeddings = model.encode(faq_df['question'].tolist(), convert_to_tensor=True)
|
| 9 |
|
| 10 |
+
# Policy suggestion mapping
|
| 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.",
|
|
|
|
| 16 |
"pension": "π‘ LIC Jeevan Akshay and PM Vaya Vandana Yojana are best for pension seekers."
|
| 17 |
}
|
| 18 |
|
| 19 |
+
# Handle chat
|
| 20 |
def chatbot(history, query):
|
| 21 |
+
query_lower = query.lower().strip()
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Handle greetings
|
| 24 |
+
if query_lower in ["hi", "hello", "hey", "good morning", "good evening"]:
|
| 25 |
+
response = "π Hello! Iβm your LIC Assistant. Ask me anything about LIC policies, claims, onboarding, or commission."
|
| 26 |
else:
|
| 27 |
+
# Embedding + similarity
|
| 28 |
+
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 29 |
+
scores = util.pytorch_cos_sim(query_embedding, faq_embeddings)[0]
|
| 30 |
+
best_score = float(scores.max())
|
| 31 |
+
best_idx = int(scores.argmax())
|
| 32 |
|
| 33 |
+
if best_score < 0.6:
|
| 34 |
+
response = "π€ I'm not confident I have the right answer for that. Please ask about LIC policies, claims, commissions, onboarding, or KYC."
|
| 35 |
+
else:
|
| 36 |
+
response = faq_df.iloc[best_idx]['answer']
|
| 37 |
+
|
| 38 |
+
# Policy suggestion
|
| 39 |
+
for keyword, suggestion in policy_suggestions.items():
|
| 40 |
+
if keyword in query_lower:
|
| 41 |
+
response += f"\n\n{suggestion}"
|
| 42 |
+
break
|
| 43 |
|
| 44 |
history.append((query, response))
|
| 45 |
return history, history
|
| 46 |
|
| 47 |
+
# Gradio UI
|
| 48 |
with gr.Blocks(title="LIC Agent Chatbot") as demo:
|
| 49 |
gr.Markdown(
|
| 50 |
"<h1 style='text-align:center;color:#0D47A1;'>π§βπΌ LIC Agent Assistant</h1>"
|
|
|
|
| 52 |
)
|
| 53 |
|
| 54 |
chatbot_ui = gr.Chatbot(label="LIC Assistant", height=450)
|
| 55 |
+
with gr.Row():
|
| 56 |
+
msg = gr.Textbox(placeholder="E.g., What is the commission for ULIP?", label="Your Question", scale=8)
|
| 57 |
+
send = gr.Button("Send", variant="primary", scale=2)
|
| 58 |
clear = gr.Button("Clear Chat")
|
| 59 |
|
| 60 |
state = gr.State([])
|
| 61 |
|
| 62 |
+
send.click(fn=chatbot, inputs=[state, msg], outputs=[chatbot_ui, state])
|
| 63 |
clear.click(lambda: ([], []), None, [chatbot_ui, state], queue=False)
|
| 64 |
|
| 65 |
demo.launch()
|