Update app.py
Browse files
app.py
CHANGED
|
@@ -9,11 +9,9 @@ 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 |
-
# 'all-MiniLM-L6-v2' is a lightweight model for encoding text into embeddings
|
| 13 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 14 |
|
| 15 |
# Convert FAQ questions into embeddings for similarity comparison
|
| 16 |
-
# This allows the chatbot to match user queries to the closest FAQ question
|
| 17 |
faq_embeddings = model.encode(faq_df['question'].tolist(), convert_to_tensor=True)
|
| 18 |
|
| 19 |
# Define policy suggestions to recommend based on keywords in the user query
|
|
@@ -27,46 +25,33 @@ policy_suggestions = {
|
|
| 27 |
|
| 28 |
# Chatbot function to process user queries and generate responses
|
| 29 |
def chatbot(history, query):
|
| 30 |
-
# Convert query to lowercase and strip whitespace for consistent processing
|
| 31 |
query_lower = query.lower().strip()
|
| 32 |
|
| 33 |
# Handle common greetings with a friendly response
|
| 34 |
if query_lower in ["hi", "hello", "hey", "good morning", "good evening"]:
|
| 35 |
response = "👋 Hello! I’m your LIC Assistant. Ask me anything about policies, claims, onboarding, or commission."
|
| 36 |
else:
|
| 37 |
-
# Encode the user query into an embedding for similarity comparison
|
| 38 |
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 39 |
-
|
| 40 |
-
# Calculate cosine similarity between the query and FAQ embeddings
|
| 41 |
scores = util.pytorch_cos_sim(query_embedding, faq_embeddings)[0]
|
| 42 |
-
|
| 43 |
-
# Find the highest similarity score and its corresponding FAQ index
|
| 44 |
best_score = float(scores.max())
|
| 45 |
best_idx = int(scores.argmax())
|
| 46 |
|
| 47 |
-
# If the similarity score is below 0.6, return a fallback response
|
| 48 |
-
# This ensures only confident matches are used
|
| 49 |
if best_score < 0.6:
|
| 50 |
response = "🤖 I'm not confident I have the right answer for that. Please ask about LIC policies, claims, commissions, onboarding, or KYC."
|
| 51 |
else:
|
| 52 |
-
# Retrieve the answer from the FAQ dataset
|
| 53 |
response = faq_df.iloc[best_idx]['answer']
|
| 54 |
|
| 55 |
-
# Add policy suggestion if the query contains a relevant keyword
|
| 56 |
for keyword, suggestion in policy_suggestions.items():
|
| 57 |
if keyword in query_lower:
|
| 58 |
response += f"\n\n{suggestion}"
|
| 59 |
break
|
| 60 |
|
| 61 |
-
# Append the query and response to the chat history
|
| 62 |
history.append((query, response))
|
| 63 |
-
|
| 64 |
-
# Return updated history for both the chatbot UI and state
|
| 65 |
return history, history
|
| 66 |
|
| 67 |
-
# Custom CSS for a modern, dynamic UI
|
| 68 |
custom_css = """
|
| 69 |
-
/* General container styling
|
| 70 |
.gradio-container {
|
| 71 |
font-family: 'Segoe UI', Arial, sans-serif;
|
| 72 |
background: linear-gradient(135deg, #e3f2fd 0%, #bbdefb 100%);
|
|
@@ -74,7 +59,7 @@ custom_css = """
|
|
| 74 |
padding: 20px;
|
| 75 |
}
|
| 76 |
|
| 77 |
-
/* Header styling
|
| 78 |
h1 {
|
| 79 |
color: #0D47A1 !important;
|
| 80 |
font-size: 2.5em !important;
|
|
@@ -84,7 +69,6 @@ h1 {
|
|
| 84 |
animation: fadeInDown 1s ease-in-out;
|
| 85 |
}
|
| 86 |
|
| 87 |
-
/* Paragraph styling: Centered, slightly smaller text for the subtitle */
|
| 88 |
p {
|
| 89 |
color: #1565C0 !important;
|
| 90 |
font-size: 1.2em !important;
|
|
@@ -92,7 +76,7 @@ p {
|
|
| 92 |
margin-bottom: 20px;
|
| 93 |
}
|
| 94 |
|
| 95 |
-
/* Chatbot container:
|
| 96 |
.chatbot {
|
| 97 |
border-radius: 15px !important;
|
| 98 |
box-shadow: 0 4px 12px rgba(0,0,0,0.1) !important;
|
|
@@ -100,11 +84,20 @@ p {
|
|
| 100 |
padding: 15px !important;
|
| 101 |
max-width: 800px !important;
|
| 102 |
margin: 0 auto !important;
|
| 103 |
-
height:
|
| 104 |
overflow-y: auto !important;
|
| 105 |
}
|
| 106 |
|
| 107 |
-
/*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
.chatbot .bubble {
|
| 109 |
border-radius: 15px !important;
|
| 110 |
padding: 12px 18px !important;
|
|
@@ -114,26 +107,23 @@ p {
|
|
| 114 |
transition: transform 0.2s ease;
|
| 115 |
}
|
| 116 |
|
| 117 |
-
/* Hover effect for chat bubbles: Slightly scale up for a dynamic feel */
|
| 118 |
.chatbot .bubble:hover {
|
| 119 |
transform: scale(1.02);
|
| 120 |
}
|
| 121 |
|
| 122 |
-
/* User bubble: Blue background, right-aligned for user messages */
|
| 123 |
.chatbot .bubble:nth-child(odd) {
|
| 124 |
background: #0D47A1 !important;
|
| 125 |
color: white !important;
|
| 126 |
margin-left: auto !important;
|
| 127 |
}
|
| 128 |
|
| 129 |
-
/* Bot bubble: Light blue background, left-aligned for bot responses */
|
| 130 |
.chatbot .bubble:nth-child(even) {
|
| 131 |
background: #E3F2FD !important;
|
| 132 |
color: #1E1E1E !important;
|
| 133 |
margin-right: auto !important;
|
| 134 |
}
|
| 135 |
|
| 136 |
-
/* Textbox styling
|
| 137 |
input[type="text"] {
|
| 138 |
border: 2px solid #0D47A1 !important;
|
| 139 |
border-radius: 25px !important;
|
|
@@ -141,51 +131,52 @@ input[type="text"] {
|
|
| 141 |
font-size: 1em !important;
|
| 142 |
box-shadow: 0 2px 8px rgba(0,0,0,0.1) !important;
|
| 143 |
transition: border-color 0.3s ease, box-shadow 0.3s ease;
|
|
|
|
| 144 |
}
|
| 145 |
|
| 146 |
-
/* Textbox focus: Highlight border and shadow when typing */
|
| 147 |
input[type="text"]:focus {
|
| 148 |
border-color: #1565C0 !important;
|
| 149 |
box-shadow: 0 4px 12px rgba(21, 101, 192, 0.3) !important;
|
| 150 |
outline: none !important;
|
| 151 |
}
|
| 152 |
|
| 153 |
-
/* Button styling:
|
| 154 |
button {
|
| 155 |
border-radius: 25px !important;
|
| 156 |
background: #0D47A1 !important;
|
| 157 |
color: white !important;
|
| 158 |
-
padding:
|
| 159 |
font-size: 1em !important;
|
| 160 |
border: none !important;
|
| 161 |
box-shadow: 0 2px 8px rgba(0,0,0,0.2) !important;
|
| 162 |
transition: background 0.3s ease, transform 0.2s ease !important;
|
|
|
|
| 163 |
}
|
| 164 |
|
| 165 |
-
/* Button hover: Change color and lift slightly for a dynamic effect */
|
| 166 |
button:hover {
|
| 167 |
background: #1565C0 !important;
|
| 168 |
transform: translateY(-2px) !important;
|
| 169 |
}
|
| 170 |
|
| 171 |
-
/* Button active: Reset lift when clicked */
|
| 172 |
button:active {
|
| 173 |
transform: translateY(0) !important;
|
| 174 |
}
|
| 175 |
|
| 176 |
-
/* Clear button:
|
| 177 |
button[aria-label="Clear Chat"] {
|
| 178 |
background: #B0BEC5 !important;
|
| 179 |
margin: 10px auto !important;
|
| 180 |
display: block !important;
|
|
|
|
|
|
|
|
|
|
| 181 |
}
|
| 182 |
|
| 183 |
-
/* Clear button hover: Slightly darker for feedback */
|
| 184 |
button[aria-label="Clear Chat"]:hover {
|
| 185 |
background: #90A4AE !important;
|
| 186 |
}
|
| 187 |
|
| 188 |
-
/*
|
| 189 |
@keyframes fadeInDown {
|
| 190 |
from {
|
| 191 |
opacity: 0;
|
|
@@ -197,7 +188,6 @@ button[aria-label="Clear Chat"]:hover {
|
|
| 197 |
}
|
| 198 |
}
|
| 199 |
|
| 200 |
-
/* Slide-in animation for chat bubbles */
|
| 201 |
@keyframes slideIn {
|
| 202 |
from {
|
| 203 |
opacity: 0;
|
|
@@ -209,7 +199,7 @@ button[aria-label="Clear Chat"]:hover {
|
|
| 209 |
}
|
| 210 |
}
|
| 211 |
|
| 212 |
-
/* Scrollbar styling
|
| 213 |
.chatbot::-webkit-scrollbar {
|
| 214 |
width: 8px;
|
| 215 |
}
|
|
@@ -229,24 +219,20 @@ button[aria-label="Clear Chat"]:hover {
|
|
| 229 |
}
|
| 230 |
"""
|
| 231 |
|
| 232 |
-
# Create the Gradio interface with custom styling
|
| 233 |
with gr.Blocks(title="LIC Agent Chatbot", css=custom_css) as demo:
|
| 234 |
-
# Display a header with title and subtitle
|
| 235 |
gr.Markdown(
|
| 236 |
"<h1>🧑💼 LIC Agent Assistant</h1>"
|
| 237 |
"<p>Ask me anything about policies, claims, commissions, onboarding, and KYC.</p>"
|
| 238 |
)
|
| 239 |
|
| 240 |
-
# Chatbot UI component with custom height, avatars, and copy button
|
| 241 |
chatbot_ui = gr.Chatbot(
|
| 242 |
label="LIC Assistant",
|
| 243 |
-
height=
|
| 244 |
bubble_full_width=False,
|
| 245 |
avatar_images=("🧑", "🤖"),
|
| 246 |
show_copy_button=True
|
| 247 |
)
|
| 248 |
|
| 249 |
-
# Create a row for the input textbox and send button
|
| 250 |
with gr.Row():
|
| 251 |
msg = gr.Textbox(
|
| 252 |
placeholder="Type your question here...",
|
|
@@ -256,17 +242,19 @@ with gr.Blocks(title="LIC Agent Chatbot", css=custom_css) as demo:
|
|
| 256 |
)
|
| 257 |
send = gr.Button("Send", variant="primary", scale=2)
|
| 258 |
|
| 259 |
-
# Clear button to reset the chat
|
| 260 |
clear = gr.Button("Clear Chat", variant="secondary")
|
| 261 |
|
| 262 |
-
# State to store chat history
|
| 263 |
state = gr.State([])
|
| 264 |
|
| 265 |
-
#
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
clear.click(lambda: ([], []), None, [chatbot_ui, state], queue=False)
|
| 270 |
|
| 271 |
-
# Launch the Gradio app
|
| 272 |
demo.launch()
|
|
|
|
| 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
|
|
|
|
| 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:
|
|
|
|
| 34 |
query_embedding = model.encode(query, convert_to_tensor=True)
|
|
|
|
|
|
|
| 35 |
scores = util.pytorch_cos_sim(query_embedding, faq_embeddings)[0]
|
|
|
|
|
|
|
| 36 |
best_score = float(scores.max())
|
| 37 |
best_idx = int(scores.argmax())
|
| 38 |
|
|
|
|
|
|
|
| 39 |
if best_score < 0.6:
|
| 40 |
response = "🤖 I'm not confident I have the right answer for that. Please ask about LIC policies, claims, commissions, onboarding, or KYC."
|
| 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 modern, dynamic UI with mobile responsiveness
|
| 53 |
custom_css = """
|
| 54 |
+
/* General container styling */
|
| 55 |
.gradio-container {
|
| 56 |
font-family: 'Segoe UI', Arial, sans-serif;
|
| 57 |
background: linear-gradient(135deg, #e3f2fd 0%, #bbdefb 100%);
|
|
|
|
| 59 |
padding: 20px;
|
| 60 |
}
|
| 61 |
|
| 62 |
+
/* Header styling */
|
| 63 |
h1 {
|
| 64 |
color: #0D47A1 !important;
|
| 65 |
font-size: 2.5em !important;
|
|
|
|
| 69 |
animation: fadeInDown 1s ease-in-out;
|
| 70 |
}
|
| 71 |
|
|
|
|
| 72 |
p {
|
| 73 |
color: #1565C0 !important;
|
| 74 |
font-size: 1.2em !important;
|
|
|
|
| 76 |
margin-bottom: 20px;
|
| 77 |
}
|
| 78 |
|
| 79 |
+
/* Chatbot container: Adjusted size and responsive design */
|
| 80 |
.chatbot {
|
| 81 |
border-radius: 15px !important;
|
| 82 |
box-shadow: 0 4px 12px rgba(0,0,0,0.1) !important;
|
|
|
|
| 84 |
padding: 15px !important;
|
| 85 |
max-width: 800px !important;
|
| 86 |
margin: 0 auto !important;
|
| 87 |
+
height: 600px !important; /* Increased chat window height */
|
| 88 |
overflow-y: auto !important;
|
| 89 |
}
|
| 90 |
|
| 91 |
+
/* Mobile adjustments for chat window */
|
| 92 |
+
@media (max-width: 768px) {
|
| 93 |
+
.chatbot {
|
| 94 |
+
max-width: 100% !important;
|
| 95 |
+
height: 400px !important; /* Reduced height for mobile */
|
| 96 |
+
margin: 0 !important;
|
| 97 |
+
}
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
/* Chat bubbles */
|
| 101 |
.chatbot .bubble {
|
| 102 |
border-radius: 15px !important;
|
| 103 |
padding: 12px 18px !important;
|
|
|
|
| 107 |
transition: transform 0.2s ease;
|
| 108 |
}
|
| 109 |
|
|
|
|
| 110 |
.chatbot .bubble:hover {
|
| 111 |
transform: scale(1.02);
|
| 112 |
}
|
| 113 |
|
|
|
|
| 114 |
.chatbot .bubble:nth-child(odd) {
|
| 115 |
background: #0D47A1 !important;
|
| 116 |
color: white !important;
|
| 117 |
margin-left: auto !important;
|
| 118 |
}
|
| 119 |
|
|
|
|
| 120 |
.chatbot .bubble:nth-child(even) {
|
| 121 |
background: #E3F2FD !important;
|
| 122 |
color: #1E1E1E !important;
|
| 123 |
margin-right: auto !important;
|
| 124 |
}
|
| 125 |
|
| 126 |
+
/* Textbox styling */
|
| 127 |
input[type="text"] {
|
| 128 |
border: 2px solid #0D47A1 !important;
|
| 129 |
border-radius: 25px !important;
|
|
|
|
| 131 |
font-size: 1em !important;
|
| 132 |
box-shadow: 0 2px 8px rgba(0,0,0,0.1) !important;
|
| 133 |
transition: border-color 0.3s ease, box-shadow 0.3s ease;
|
| 134 |
+
flex-grow: 1 !important;
|
| 135 |
}
|
| 136 |
|
|
|
|
| 137 |
input[type="text"]:focus {
|
| 138 |
border-color: #1565C0 !important;
|
| 139 |
box-shadow: 0 4px 12px rgba(21, 101, 192, 0.3) !important;
|
| 140 |
outline: none !important;
|
| 141 |
}
|
| 142 |
|
| 143 |
+
/* Button styling: Larger Send button */
|
| 144 |
button {
|
| 145 |
border-radius: 25px !important;
|
| 146 |
background: #0D47A1 !important;
|
| 147 |
color: white !important;
|
| 148 |
+
padding: 15px 30px !important; /* Increased padding for larger size */
|
| 149 |
font-size: 1em !important;
|
| 150 |
border: none !important;
|
| 151 |
box-shadow: 0 2px 8px rgba(0,0,0,0.2) !important;
|
| 152 |
transition: background 0.3s ease, transform 0.2s ease !important;
|
| 153 |
+
min-width: 120px !important; /* Minimum width for Send button */
|
| 154 |
}
|
| 155 |
|
|
|
|
| 156 |
button:hover {
|
| 157 |
background: #1565C0 !important;
|
| 158 |
transform: translateY(-2px) !important;
|
| 159 |
}
|
| 160 |
|
|
|
|
| 161 |
button:active {
|
| 162 |
transform: translateY(0) !important;
|
| 163 |
}
|
| 164 |
|
| 165 |
+
/* Clear button: Reduced size */
|
| 166 |
button[aria-label="Clear Chat"] {
|
| 167 |
background: #B0BEC5 !important;
|
| 168 |
margin: 10px auto !important;
|
| 169 |
display: block !important;
|
| 170 |
+
padding: 8px 20px !important; /* Reduced padding */
|
| 171 |
+
font-size: 0.9em !important;
|
| 172 |
+
min-width: 100px !important; /* Reduced width */
|
| 173 |
}
|
| 174 |
|
|
|
|
| 175 |
button[aria-label="Clear Chat"]:hover {
|
| 176 |
background: #90A4AE !important;
|
| 177 |
}
|
| 178 |
|
| 179 |
+
/* Animations */
|
| 180 |
@keyframes fadeInDown {
|
| 181 |
from {
|
| 182 |
opacity: 0;
|
|
|
|
| 188 |
}
|
| 189 |
}
|
| 190 |
|
|
|
|
| 191 |
@keyframes slideIn {
|
| 192 |
from {
|
| 193 |
opacity: 0;
|
|
|
|
| 199 |
}
|
| 200 |
}
|
| 201 |
|
| 202 |
+
/* Scrollbar styling */
|
| 203 |
.chatbot::-webkit-scrollbar {
|
| 204 |
width: 8px;
|
| 205 |
}
|
|
|
|
| 219 |
}
|
| 220 |
"""
|
| 221 |
|
|
|
|
| 222 |
with gr.Blocks(title="LIC Agent Chatbot", css=custom_css) as demo:
|
|
|
|
| 223 |
gr.Markdown(
|
| 224 |
"<h1>🧑💼 LIC Agent Assistant</h1>"
|
| 225 |
"<p>Ask me anything about policies, claims, commissions, onboarding, and KYC.</p>"
|
| 226 |
)
|
| 227 |
|
|
|
|
| 228 |
chatbot_ui = gr.Chatbot(
|
| 229 |
label="LIC Assistant",
|
| 230 |
+
height=600, # Increased default height
|
| 231 |
bubble_full_width=False,
|
| 232 |
avatar_images=("🧑", "🤖"),
|
| 233 |
show_copy_button=True
|
| 234 |
)
|
| 235 |
|
|
|
|
| 236 |
with gr.Row():
|
| 237 |
msg = gr.Textbox(
|
| 238 |
placeholder="Type your question here...",
|
|
|
|
| 242 |
)
|
| 243 |
send = gr.Button("Send", variant="primary", scale=2)
|
| 244 |
|
|
|
|
| 245 |
clear = gr.Button("Clear Chat", variant="secondary")
|
| 246 |
|
|
|
|
| 247 |
state = gr.State([])
|
| 248 |
|
| 249 |
+
# Send message on Enter key and clear textbox
|
| 250 |
+
def send_message(history, query):
|
| 251 |
+
if query:
|
| 252 |
+
history, _ = chatbot(history, query)
|
| 253 |
+
return history, ""
|
| 254 |
+
return history, query
|
| 255 |
+
|
| 256 |
+
send.click(fn=send_message, inputs=[state, msg], outputs=[chatbot_ui, msg])
|
| 257 |
+
msg.submit(fn=send_message, inputs=[state, msg], outputs=[chatbot_ui, msg]) # Add Enter key support
|
| 258 |
clear.click(lambda: ([], []), None, [chatbot_ui, state], queue=False)
|
| 259 |
|
|
|
|
| 260 |
demo.launch()
|