waleedmohd commited on
Commit
6b5edbe
·
verified ·
1 Parent(s): 4b00a79

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -1
app.py CHANGED
@@ -206,6 +206,86 @@ def respond(message: str):
206
  log_interaction(message, responses[language], intent, language)
207
  return responses
208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
209
  # Gradio Interface
210
  with gr.Blocks() as demo:
211
  selected_lang = gr.State(value="ar")
@@ -217,7 +297,7 @@ with gr.Blocks() as demo:
217
  with gr.Row():
218
  language_btn = gr.Radio(["العربية", "English"], value="العربية", label="Language")
219
 
220
- chatbot = gr.Chatbot(height=400)
221
 
222
  with gr.Row():
223
  text_input = gr.Textbox(placeholder="Type your question here", label="")
@@ -227,6 +307,7 @@ with gr.Blocks() as demo:
227
  menu_btn = gr.Button("Show Menu")
228
  live_agent_btn = gr.Button("Connect to Live Agent")
229
  survey_btn = gr.Button("Feedback")
 
230
 
231
  # Link inputs and buttons to response functions
232
  submit_btn.click(
@@ -253,6 +334,12 @@ with gr.Blocks() as demo:
253
  outputs=[chatbot]
254
  )
255
 
 
 
 
 
 
 
256
  # Initialize chat
257
  demo.load(
258
  fn=init_chat,
 
206
  log_interaction(message, responses[language], intent, language)
207
  return responses
208
 
209
+ # Function to handle message submission
210
+ def on_submit(message, chat_history, lang, name):
211
+ if not message.strip():
212
+ return "", chat_history, name
213
+
214
+ # Check if this is a name introduction
215
+ name_patterns = [
216
+ r"my name is (\w+)",
217
+ r"i am (\w+)",
218
+ r"i'm (\w+)",
219
+ r"اسمي (\w+)",
220
+ r"أنا (\w+)"
221
+ ]
222
+
223
+ for pattern in name_patterns:
224
+ match = re.search(pattern, message.lower())
225
+ if match:
226
+ name = match.group(1)
227
+ break
228
+
229
+ # Add user message to chat history
230
+ chat_history = chat_history + [[message, None]]
231
+
232
+ # Get response
233
+ responses = respond(message)
234
+
235
+ # Select response based on language
236
+ response = responses[lang]
237
+
238
+ # Personalize response if name is available
239
+ if name and CUSTOMER_SERVICE_ENHANCEMENTS_AVAILABLE:
240
+ if lang == "ar":
241
+ response = response.replace("مرحبًا", f"مرحبًا {name}")
242
+ else:
243
+ response = response.replace("Welcome", f"Welcome {name}")
244
+ response = response.replace("Hello", f"Hello {name}")
245
+
246
+ # Update bot response in chat history
247
+ chat_history[-1][1] = response
248
+
249
+ return "", chat_history, name
250
+
251
+ # Function to show menu
252
+ def show_menu(chat_history, lang):
253
+ menu_responses = {
254
+ "ar": MENU_AR,
255
+ "en": MENU_EN
256
+ }
257
+
258
+ # Get menu text
259
+ menu_text = menu_responses[lang]
260
+
261
+ # Add system message showing the menu
262
+ chat_history = chat_history + [[None, menu_text.replace("\n", "<br>")]]
263
+
264
+ return chat_history
265
+
266
+ # Function to connect to live agent
267
+ def connect_to_live_agent(chat_history, lang):
268
+ message = "Connecting to a live customer service agent. Please wait a moment..." if lang == "en" else "جاري الاتصال بوكيل خدمة العملاء. يرجى الانتظار لحظة..."
269
+ chat_history = chat_history + [[None, message]]
270
+ return chat_history
271
+
272
+ # Function to show satisfaction survey
273
+ def show_satisfaction_survey(chat_history, lang):
274
+ if CUSTOMER_SERVICE_ENHANCEMENTS_AVAILABLE:
275
+ survey_html = offer_satisfaction_survey(lang)
276
+ chat_history = chat_history + [[None, survey_html]]
277
+ else:
278
+ # Simple survey message if enhancements not available
279
+ title = "استطلاع رضا العملاء" if lang == "ar" else "Customer Satisfaction Survey"
280
+ message = f"<div class='satisfaction-survey'><h3>{title}</h3><p>{'نشكرك على استخدام المساعد المصرفي الافتراضي!' if lang == 'ar' else 'Thank you for using our virtual banking assistant!'}</p></div>"
281
+ chat_history = chat_history + [[None, message]]
282
+
283
+ return chat_history
284
+
285
+ # Function to reset chat
286
+ def reset_chat():
287
+ return [], None # Clear chat history and reset user name
288
+
289
  # Gradio Interface
290
  with gr.Blocks() as demo:
291
  selected_lang = gr.State(value="ar")
 
297
  with gr.Row():
298
  language_btn = gr.Radio(["العربية", "English"], value="العربية", label="Language")
299
 
300
+ chatbot = gr.Chatbot(height=400, type="messages") # Updated to use the new format
301
 
302
  with gr.Row():
303
  text_input = gr.Textbox(placeholder="Type your question here", label="")
 
307
  menu_btn = gr.Button("Show Menu")
308
  live_agent_btn = gr.Button("Connect to Live Agent")
309
  survey_btn = gr.Button("Feedback")
310
+ reset_btn = gr.Button("Reset Chat")
311
 
312
  # Link inputs and buttons to response functions
313
  submit_btn.click(
 
334
  outputs=[chatbot]
335
  )
336
 
337
+ reset_btn.click(
338
+ fn=reset_chat,
339
+ inputs=[],
340
+ outputs=[chatbot, user_name]
341
+ )
342
+
343
  # Initialize chat
344
  demo.load(
345
  fn=init_chat,