LIC_Agent / app.py
dschandra's picture
Update app.py
beb5a28 verified
import gradio as gr
import pandas as pd
from sentence_transformers import SentenceTransformer, util
# Load FAQ
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 recommendations
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()
# Handle greetings
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()