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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -11
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 data
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 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,15 +16,15 @@ policy_suggestions = {
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())
@@ -35,7 +35,7 @@ def chatbot(history, query):
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}"
@@ -44,22 +44,25 @@ def chatbot(history, query):
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>"
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="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()
 
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()