dschandra commited on
Commit
5cfa812
Β·
verified Β·
1 Parent(s): 39f9620

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -17
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
- # Sample policies to suggest (add more as needed)
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,28 +16,35 @@ policy_suggestions = {
16
  "pension": "πŸ’‘ LIC Jeevan Akshay and PM Vaya Vandana Yojana are best for pension seekers."
17
  }
18
 
 
19
  def chatbot(history, query):
20
- query_embedding = model.encode(query, convert_to_tensor=True)
21
- scores = util.pytorch_cos_sim(query_embedding, faq_embeddings)[0]
22
- best_score = float(scores.max())
23
- best_idx = int(scores.argmax())
24
 
25
- if best_score < 0.6:
26
- response = "πŸ€– I'm not confident I have the right answer for that. Please ask about LIC policies, claims, commissions, onboarding, or KYC."
 
27
  else:
28
- response = faq_df.iloc[best_idx]['answer']
 
 
 
 
29
 
30
- # Policy recommendation based on keywords
31
- query_lower = query.lower()
32
- for keyword, suggestion in policy_suggestions.items():
33
- if keyword in query_lower:
34
- response += f"\n\n{suggestion}"
35
- break
 
 
 
 
36
 
37
  history.append((query, response))
38
  return history, history
39
 
40
- # UI using Gradio Blocks
41
  with gr.Blocks(title="LIC Agent Chatbot") as demo:
42
  gr.Markdown(
43
  "<h1 style='text-align:center;color:#0D47A1;'>πŸ§‘β€πŸ’Ό LIC Agent Assistant</h1>"
@@ -45,12 +52,14 @@ with gr.Blocks(title="LIC Agent Chatbot") as demo:
45
  )
46
 
47
  chatbot_ui = gr.Chatbot(label="LIC Assistant", height=450)
48
- msg = gr.Textbox(label="Your Question", placeholder="E.g., What is the commission for ULIP?")
 
 
49
  clear = gr.Button("Clear Chat")
50
 
51
  state = gr.State([])
52
 
53
- msg.submit(chatbot, [state, msg], [chatbot_ui, state])
54
  clear.click(lambda: ([], []), None, [chatbot_ui, state], queue=False)
55
 
56
  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 suggestion mapping
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
+ # Handle chat
20
  def chatbot(history, query):
21
+ query_lower = query.lower().strip()
 
 
 
22
 
23
+ # Handle 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 LIC policies, claims, onboarding, or commission."
26
  else:
27
+ # Embedding + similarity
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())
31
+ best_idx = int(scores.argmax())
32
 
33
+ if best_score < 0.6:
34
+ response = "πŸ€– I'm not confident I have the right answer for that. Please ask about LIC policies, claims, commissions, onboarding, or KYC."
35
+ else:
36
+ response = faq_df.iloc[best_idx]['answer']
37
+
38
+ # Policy suggestion
39
+ for keyword, suggestion in policy_suggestions.items():
40
+ if keyword in query_lower:
41
+ response += f"\n\n{suggestion}"
42
+ break
43
 
44
  history.append((query, response))
45
  return history, history
46
 
47
+ # Gradio 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
  )
53
 
54
  chatbot_ui = gr.Chatbot(label="LIC Assistant", height=450)
55
+ with gr.Row():
56
+ msg = gr.Textbox(placeholder="E.g., What is the commission for ULIP?", label="Your Question", scale=8)
57
+ send = gr.Button("Send", variant="primary", scale=2)
58
  clear = gr.Button("Clear Chat")
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()