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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -31
app.py CHANGED
@@ -7,46 +7,50 @@ 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
- def chatbot(query):
 
 
 
 
 
 
 
 
 
11
  query_embedding = model.encode(query, convert_to_tensor=True)
12
  scores = util.pytorch_cos_sim(query_embedding, faq_embeddings)[0]
13
- best_score = float(scores.max()) # Get max similarity score
14
  best_idx = int(scores.argmax())
15
 
16
- if best_score < 0.6: # Threshold can be adjusted
17
- return (
18
- "πŸ€– I'm not confident I have the right answer for that.\n"
19
- "Please ask a question related to LIC policies, claims, onboarding, or commissions."
20
- )
21
  else:
22
- return faq_df.iloc[best_idx]['answer']
23
 
 
 
 
 
 
 
24
 
25
- with gr.Blocks(title="LIC Agent Assistant") as demo:
 
 
 
 
26
  gr.Markdown(
27
- """
28
- <h1 style='text-align: center; color: #1a237e;'>πŸ§‘β€πŸ’Ό LIC Agent Assistant Chatbot</h1>
29
- <p style='text-align: center;'>Ask questions about LIC policies, commissions, claims, onboarding, and more!</p>
30
- """,
31
- elem_id="header"
32
  )
33
 
34
- with gr.Row():
35
- with gr.Column(scale=1):
36
- user_input = gr.Textbox(
37
- label="Your Question",
38
- placeholder="E.g., How do I file a claim?",
39
- lines=2
40
- )
41
- submit_btn = gr.Button("Get Answer", variant="primary")
42
-
43
- with gr.Column(scale=1):
44
- output = gr.Textbox(
45
- label="Answer",
46
- placeholder="Response will appear here...",
47
- lines=6
48
- )
49
-
50
- submit_btn.click(fn=chatbot, inputs=user_input, outputs=output)
51
 
52
  demo.launch()
 
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.",
14
+ "endowment": "πŸ’‘ LIC New Endowment Plan offers savings and insurance benefits together.",
15
+ "ulip": "πŸ’‘ LIC SIIP and Nivesh Plus are good ULIP options with market-linked returns.",
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>"
44
+ "<p style='text-align:center;'>Ask me anything about policies, claims, commissions, onboarding, and KYC.</p>"
 
 
 
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()