Update app.py
Browse files
app.py
CHANGED
@@ -2,12 +2,12 @@ import gradio as gr
|
|
2 |
import pandas as pd
|
3 |
from sentence_transformers import SentenceTransformer, util
|
4 |
|
5 |
-
# Load FAQ
|
6 |
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,15 @@ policy_suggestions = {
|
|
16 |
"pension": "π‘ LIC Jeevan Akshay and PM Vaya Vandana Yojana are best for pension seekers."
|
17 |
}
|
18 |
|
19 |
-
#
|
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
|
26 |
else:
|
27 |
-
#
|
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 +35,7 @@ def chatbot(history, query):
|
|
35 |
else:
|
36 |
response = faq_df.iloc[best_idx]['answer']
|
37 |
|
38 |
-
#
|
39 |
for keyword, suggestion in policy_suggestions.items():
|
40 |
if keyword in query_lower:
|
41 |
response += f"\n\n{suggestion}"
|
@@ -44,22 +44,25 @@ def chatbot(history, query):
|
|
44 |
history.append((query, response))
|
45 |
return history, history
|
46 |
|
47 |
-
#
|
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>"
|
51 |
"<p style='text-align:center;'>Ask me anything about policies, claims, commissions, onboarding, and KYC.</p>"
|
52 |
)
|
53 |
|
54 |
-
chatbot_ui = gr.Chatbot(label="LIC Assistant", height=450)
|
55 |
with gr.Row():
|
56 |
-
msg = gr.Textbox(placeholder="
|
57 |
send = gr.Button("Send", variant="primary", scale=2)
|
58 |
-
|
|
|
|
|
59 |
|
60 |
state = gr.State([])
|
61 |
|
62 |
send.click(fn=chatbot, inputs=[state, msg], outputs=[chatbot_ui, state])
|
|
|
63 |
clear.click(lambda: ([], []), None, [chatbot_ui, state], queue=False)
|
64 |
|
65 |
demo.launch()
|
|
|
2 |
import pandas as pd
|
3 |
from sentence_transformers import SentenceTransformer, util
|
4 |
|
5 |
+
# Load FAQ
|
6 |
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 tips
|
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 |
+
# Chat handler
|
20 |
def chatbot(history, query):
|
21 |
query_lower = query.lower().strip()
|
22 |
|
23 |
+
# Greetings
|
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 |
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 |
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>"
|
51 |
"<p style='text-align:center;'>Ask me anything about policies, claims, commissions, onboarding, and KYC.</p>"
|
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...", label=None, scale=8, show_label=False)
|
57 |
send = gr.Button("Send", variant="primary", scale=2)
|
58 |
+
with gr.Row():
|
59 |
+
mic = gr.Audio(source="microphone", type="filepath", label="π€ Voice Input (optional)")
|
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()
|