|
import gradio as gr |
|
import pandas as pd |
|
from sentence_transformers import SentenceTransformer, util |
|
|
|
|
|
try: |
|
faq_df = pd.read_csv("lic_faq.csv", encoding="utf-8") |
|
except UnicodeDecodeError: |
|
faq_df = pd.read_csv("lic_faq.csv", encoding="ISO-8859-1") |
|
|
|
model = SentenceTransformer('all-MiniLM-L6-v2') |
|
faq_embeddings = model.encode(faq_df['question'].tolist(), convert_to_tensor=True) |
|
|
|
|
|
policy_suggestions = { |
|
"term": "π‘ You might consider LIC Tech Term Plan for pure protection at low cost.", |
|
"money back": "π‘ LIC Money Back Policy is great for periodic returns along with insurance.", |
|
"endowment": "π‘ LIC New Endowment Plan offers savings and insurance benefits together.", |
|
"ulip": "π‘ LIC SIIP and Nivesh Plus are good ULIP options with market-linked returns.", |
|
"pension": "π‘ LIC Jeevan Akshay and PM Vaya Vandana Yojana are best for pension seekers." |
|
} |
|
|
|
def chatbot(history, query): |
|
query_lower = query.lower().strip() |
|
|
|
|
|
if query_lower in ["hi", "hello", "hey", "good morning", "good evening"]: |
|
response = "π Hello! Iβm your LIC Assistant. Ask me anything about policies, claims, onboarding, or commission." |
|
else: |
|
query_embedding = model.encode(query, convert_to_tensor=True) |
|
scores = util.pytorch_cos_sim(query_embedding, faq_embeddings)[0] |
|
best_score = float(scores.max()) |
|
best_idx = int(scores.argmax()) |
|
|
|
if best_score < 0.6: |
|
response = "π€ I'm not confident I have the right answer for that. Please ask about LIC policies, claims, commissions, onboarding, or KYC." |
|
else: |
|
response = faq_df.iloc[best_idx]['answer'] |
|
|
|
for keyword, suggestion in policy_suggestions.items(): |
|
if keyword in query_lower: |
|
response += f"\n\n{suggestion}" |
|
break |
|
|
|
history.append((query, response)) |
|
return history, history |
|
|
|
with gr.Blocks(title="LIC Agent Chatbot") as demo: |
|
gr.Markdown( |
|
"<h1 style='text-align:center;color:#0D47A1;'>π§βπΌ LIC Agent Assistant</h1>" |
|
"<p style='text-align:center;'>Ask me anything about policies, claims, commissions, onboarding, and KYC.</p>" |
|
) |
|
|
|
chatbot_ui = gr.Chatbot(label="LIC Assistant", height=450, bubble_full_width=False, avatar_images=("π§", "π€")) |
|
|
|
with gr.Row(): |
|
msg = gr.Textbox(placeholder="Ask your question here...", show_label=False, scale=8) |
|
send = gr.Button("Send", variant="primary", scale=2) |
|
|
|
clear = gr.Button("Clear Chat") |
|
|
|
state = gr.State([]) |
|
|
|
send.click(fn=chatbot, inputs=[state, msg], outputs=[chatbot_ui, state]) |
|
clear.click(lambda: ([], []), None, [chatbot_ui, state], queue=False) |
|
|
|
demo.launch() |
|
|