dschandra commited on
Commit
e2d1132
Β·
verified Β·
1 Parent(s): c4bb6ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -223
app.py CHANGED
@@ -2,19 +2,12 @@ import gradio as gr
2
  import pandas as pd
3
  from sentence_transformers import SentenceTransformer, util
4
 
5
- # Load FAQ data from a CSV file containing questions and answers
6
- try:
7
- faq_df = pd.read_csv("lic_faq.csv", encoding="utf-8")
8
- except UnicodeDecodeError:
9
- faq_df = pd.read_csv("lic_faq.csv", encoding="ISO-8859-1")
10
-
11
- # Initialize the SentenceTransformer model for semantic text similarity
12
  model = SentenceTransformer('all-MiniLM-L6-v2')
13
-
14
- # Convert FAQ questions into embeddings for similarity comparison
15
  faq_embeddings = model.encode(faq_df['question'].tolist(), convert_to_tensor=True)
16
 
17
- # Define policy suggestions to recommend based on keywords in the user query
18
  policy_suggestions = {
19
  "term": "πŸ’‘ You might consider LIC Tech Term Plan for pure protection at low cost.",
20
  "money back": "πŸ’‘ LIC Money Back Policy is great for periodic returns along with insurance.",
@@ -23,11 +16,10 @@ policy_suggestions = {
23
  "pension": "πŸ’‘ LIC Jeevan Akshay and PM Vaya Vandana Yojana are best for pension seekers."
24
  }
25
 
26
- # Chatbot function to process user queries and generate responses
27
  def chatbot(history, query):
28
  query_lower = query.lower().strip()
29
 
30
- # Handle common greetings with a friendly response
31
  if query_lower in ["hi", "hello", "hey", "good morning", "good evening"]:
32
  response = "πŸ‘‹ Hello! I’m your LIC Assistant. Ask me anything about policies, claims, onboarding, or commission."
33
  else:
@@ -41,230 +33,31 @@ def chatbot(history, query):
41
  else:
42
  response = faq_df.iloc[best_idx]['answer']
43
 
44
- for keyword, suggestion in policy_suggestions.items():
45
- if keyword in query_lower:
46
- response += f"\n\n{suggestion}"
47
- break
48
 
49
  history.append((query, response))
50
  return history, history
51
 
52
- # Custom CSS for a normal chatbot visual design with adjusted bubbles
53
- custom_css = """
54
- /* General container styling */
55
- .gradio-container {
56
- font-family: 'Arial', sans-serif;
57
- background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
58
- min-height: 100vh;
59
- padding: 0;
60
- margin: 0;
61
- font-size: 14px;
62
- }
63
-
64
- /* Header styling */
65
- h1 {
66
- color: #2c3e50 !important;
67
- font-size: 24px !important;
68
- text-align: center;
69
- margin: 15px 0;
70
- text-shadow: none;
71
- }
72
-
73
- p {
74
- color: #34495e !important;
75
- font-size: 14px !important;
76
- text-align: center;
77
- margin-bottom: 15px;
78
- }
79
-
80
- /* Chatbot container */
81
- .chatbot {
82
- border-radius: 8px !important;
83
- box-shadow: 0 2px 10px rgba(0,0,0,0.1) !important;
84
- background: #ffffff !important;
85
- padding: 10px !important;
86
- max-width: 800px !important;
87
- margin: 0 auto !important;
88
- height: 70vh !important;
89
- overflow-y: auto !important;
90
- display: block;
91
- box-sizing: border-box !important; /* Ensure consistent sizing */
92
- }
93
-
94
- /* Chat bubbles: Normal design without border */
95
- .chatbot .bubble {
96
- border: none !important; /* Explicitly remove border */
97
- border-radius: 8px !important;
98
- padding: 6px 10px !important; /* Reduced padding */
99
- margin: 5px 0 !important;
100
- max-width: 70% !important;
101
- font-size: 14px !important;
102
- min-height: 20px !important; /* Reduced minimum height */
103
- min-width: 50px !important; /* Reduced minimum width */
104
- word-wrap: break-word !important;
105
- line-height: 1.4 !important;
106
- color: #2c3e50 !important;
107
- box-sizing: border-box !important; /* Include padding in size */
108
- }
109
-
110
- /* User bubble: Right-aligned, light blue */
111
- .chatbot .bubble:nth-child(odd) {
112
- background: #3498db !important;
113
- color: #ffffff !important;
114
- margin-left: auto !important;
115
- align-self: flex-end !important;
116
- }
117
-
118
- /* Bot bubble: Left-aligned, light gray */
119
- .chatbot .bubble:nth-child(even) {
120
- background: #ecf0f1 !important;
121
- margin-right: auto !important;
122
- align-self: flex-start !important;
123
- }
124
-
125
- /* Input area */
126
- .gradio-row:last-child {
127
- width: 100% !important;
128
- max-width: 800px !important;
129
- margin: 10px auto !important;
130
- padding: 10px !important;
131
- display: flex !important;
132
- align-items: center !important;
133
- background: transparent !important;
134
- box-sizing: border-box !important;
135
- }
136
-
137
- /* Textbox styling */
138
- input[type="text"] {
139
- border: 1px solid #bdc3c7 !important;
140
- border-radius: 4px !important;
141
- padding: 8px 12px !important;
142
- font-size: 14px !important;
143
- box-shadow: 0 1px 3px rgba(0,0,0,0.1) !important;
144
- flex-grow: 1 !important;
145
- margin-right: 10px !important;
146
- color: #2c3e50 !important;
147
- box-sizing: border-box !important;
148
- }
149
-
150
- input[type="text"]::placeholder {
151
- color: #7f8c8d !important;
152
- font-size: 14px !important;
153
- }
154
-
155
- input[type="text"]:focus {
156
- border-color: #3498db !important;
157
- outline: none !important;
158
- }
159
-
160
- /* Send button */
161
- button {
162
- border-radius: 4px !important;
163
- background: #3498db !important;
164
- color: white !important;
165
- padding: 8px 20px !important;
166
- font-size: 14px !important;
167
- border: none !important;
168
- min-width: 80px !important;
169
- transition: background 0.3s ease !important;
170
- text-align: center !important;
171
- box-sizing: border-box !important;
172
- }
173
-
174
- button:hover {
175
- background: #2980b9 !important;
176
- }
177
-
178
- /* Clear button */
179
- button[aria-label="Clear Chat"] {
180
- background: #bdc3c7 !important;
181
- padding: 6px 15px !important;
182
- font-size: 14px !important;
183
- min-width: 90px !important;
184
- margin-top: 10px !important;
185
- align-self: center !important;
186
- color: #ffffff !important;
187
- box-sizing: border-box !important;
188
- }
189
-
190
- button[aria-label="Clear Chat"]:hover {
191
- background: #95a5a6 !important;
192
- }
193
-
194
- /* Scrollbar styling */
195
- .chatbot::-webkit-scrollbar {
196
- width: 6px;
197
- }
198
-
199
- .chatbot::-webkit-scrollbar-track {
200
- background: #f5f7fa;
201
- }
202
-
203
- .chatbot::-webkit-scrollbar-thumb {
204
- background: #3498db;
205
- border-radius: 3px;
206
- }
207
-
208
- .chatbot::-webkit-scrollbar-thumb:hover {
209
- background: #2980b9;
210
- }
211
-
212
- /* Mobile-specific adjustments */
213
- @media (max-width: 768px) {
214
- .chatbot {
215
- max-width: 100% !important;
216
- height: 65vh !important;
217
- margin: 0 !important;
218
- }
219
- .gradio-row:last-child {
220
- max-width: 100% !important;
221
- padding: 5px !important;
222
- }
223
- input[type="text"] {
224
- padding: 6px 10px !important;
225
- }
226
- button {
227
- padding: 6px 15px !important;
228
- }
229
- }
230
- """
231
-
232
- with gr.Blocks(title="LIC Agent Chatbot", css=custom_css) as demo:
233
  gr.Markdown(
234
- "<h1>πŸ§‘β€πŸ’Ό LIC Agent Assistant</h1>"
235
- "<p>Ask me anything about policies, claims, commissions, onboarding, and KYC.</p>"
236
  )
237
 
238
- chatbot_ui = gr.Chatbot(
239
- label="LIC Assistant",
240
- height=600,
241
- bubble_full_width=False,
242
- avatar_images=("πŸ§‘", "πŸ€–"),
243
- show_copy_button=True
244
- )
245
 
246
  with gr.Row():
247
- msg = gr.Textbox(
248
- placeholder="Type your question here...",
249
- show_label=False,
250
- scale=8,
251
- elem_classes="chat-input"
252
- )
253
  send = gr.Button("Send", variant="primary", scale=2)
254
 
255
- clear = gr.Button("Clear Chat", variant="secondary")
256
 
257
  state = gr.State([])
258
 
259
- # Send message on Enter key and clear textbox
260
- def send_message(history, query):
261
- if query:
262
- history, _ = chatbot(history, query)
263
- return history, ""
264
- return history, query
265
-
266
- send.click(fn=send_message, inputs=[state, msg], outputs=[chatbot_ui, msg])
267
- msg.submit(fn=send_message, inputs=[state, msg], outputs=[chatbot_ui, msg])
268
  clear.click(lambda: ([], []), None, [chatbot_ui, state], queue=False)
269
 
270
- 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 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:
 
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}"
39
+ break
40
 
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>"
47
+ "<p style='text-align:center;'>Ask me anything about policies, claims, commissions, onboarding, and KYC.</p>"
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()