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 |
-
# Policy
|
| 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,15 +16,13 @@ policy_suggestions = {
|
|
| 16 |
"pension": "π‘ LIC Jeevan Akshay and PM Vaya Vandana Yojana are best for pension seekers."
|
| 17 |
}
|
| 18 |
|
| 19 |
-
# Chat handler
|
| 20 |
def chatbot(history, query):
|
| 21 |
query_lower = query.lower().strip()
|
| 22 |
|
| 23 |
-
#
|
| 24 |
if query_lower in ["hi", "hello", "hey", "good morning", "good evening"]:
|
| 25 |
response = "π Hello! Iβm your LIC Assistant. Ask me anything about policies, claims, onboarding, or commission."
|
| 26 |
else:
|
| 27 |
-
# Semantic match
|
| 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())
|
|
@@ -35,7 +33,6 @@ def chatbot(history, query):
|
|
| 35 |
else:
|
| 36 |
response = faq_df.iloc[best_idx]['answer']
|
| 37 |
|
| 38 |
-
# Add policy suggestion
|
| 39 |
for keyword, suggestion in policy_suggestions.items():
|
| 40 |
if keyword in query_lower:
|
| 41 |
response += f"\n\n{suggestion}"
|
|
@@ -44,7 +41,6 @@ def chatbot(history, query):
|
|
| 44 |
history.append((query, response))
|
| 45 |
return history, history
|
| 46 |
|
| 47 |
-
# 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,17 +48,16 @@ with gr.Blocks(title="LIC Agent Chatbot") as demo:
|
|
| 52 |
)
|
| 53 |
|
| 54 |
chatbot_ui = gr.Chatbot(label="LIC Assistant", height=450, bubble_full_width=False, avatar_images=("π§", "π€"))
|
|
|
|
| 55 |
with gr.Row():
|
| 56 |
-
msg = gr.Textbox(placeholder="Ask your question here...",
|
| 57 |
send = gr.Button("Send", variant="primary", scale=2)
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
clear = gr.Button("Clear Chat")
|
| 61 |
|
| 62 |
state = gr.State([])
|
| 63 |
|
| 64 |
send.click(fn=chatbot, inputs=[state, msg], outputs=[chatbot_ui, state])
|
| 65 |
-
mic.change(lambda x: ("Voice input received (text not yet processed)", x), inputs=mic, outputs=[msg, mic])
|
| 66 |
clear.click(lambda: ([], []), None, [chatbot_ui, state], queue=False)
|
| 67 |
|
| 68 |
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 recommendations
|
| 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 |
def chatbot(history, query):
|
| 20 |
query_lower = query.lower().strip()
|
| 21 |
|
| 22 |
+
# Handle greetings
|
| 23 |
if query_lower in ["hi", "hello", "hey", "good morning", "good evening"]:
|
| 24 |
response = "π Hello! Iβm your LIC Assistant. Ask me anything about policies, claims, onboarding, or commission."
|
| 25 |
else:
|
|
|
|
| 26 |
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 27 |
scores = util.pytorch_cos_sim(query_embedding, faq_embeddings)[0]
|
| 28 |
best_score = float(scores.max())
|
|
|
|
| 33 |
else:
|
| 34 |
response = faq_df.iloc[best_idx]['answer']
|
| 35 |
|
|
|
|
| 36 |
for keyword, suggestion in policy_suggestions.items():
|
| 37 |
if keyword in query_lower:
|
| 38 |
response += f"\n\n{suggestion}"
|
|
|
|
| 41 |
history.append((query, response))
|
| 42 |
return history, history
|
| 43 |
|
|
|
|
| 44 |
with gr.Blocks(title="LIC Agent Chatbot") as demo:
|
| 45 |
gr.Markdown(
|
| 46 |
"<h1 style='text-align:center;color:#0D47A1;'>π§βπΌ LIC Agent Assistant</h1>"
|
|
|
|
| 48 |
)
|
| 49 |
|
| 50 |
chatbot_ui = gr.Chatbot(label="LIC Assistant", height=450, bubble_full_width=False, avatar_images=("π§", "π€"))
|
| 51 |
+
|
| 52 |
with gr.Row():
|
| 53 |
+
msg = gr.Textbox(placeholder="Ask your question here...", show_label=False, scale=8)
|
| 54 |
send = gr.Button("Send", variant="primary", scale=2)
|
| 55 |
+
|
| 56 |
+
clear = gr.Button("Clear Chat")
|
|
|
|
| 57 |
|
| 58 |
state = gr.State([])
|
| 59 |
|
| 60 |
send.click(fn=chatbot, inputs=[state, msg], outputs=[chatbot_ui, state])
|
|
|
|
| 61 |
clear.click(lambda: ([], []), None, [chatbot_ui, state], queue=False)
|
| 62 |
|
| 63 |
demo.launch()
|