File size: 2,763 Bytes
6be3cee
 
 
 
e2d1132
beb5a28
 
 
 
 
6be3cee
 
 
e2d1132
39f9620
 
 
 
 
 
 
 
 
5cfa812
d458480
e2d1132
5cfa812
4855174
d458480
5cfa812
 
 
 
d458480
5cfa812
 
 
 
 
e2d1132
 
 
 
6be3cee
39f9620
 
 
e2d1132
600b0cd
e2d1132
 
600b0cd
 
e2d1132
e216d81
5cfa812
e2d1132
5cfa812
e216d81
e2d1132
39f9620
 
 
e2d1132
39f9620
600b0cd
e2d1132
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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()