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( "

šŸ§‘ā€šŸ’¼ LIC Agent Assistant

" "

Ask me anything about policies, claims, commissions, onboarding, and KYC.

" ) 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()