waleedmohd commited on
Commit
f47a820
·
verified ·
1 Parent(s): 4600387

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -4
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
  import logging
 
3
 
4
  # Omdurman National Bank-specific guidelines
5
  ONB_GUIDELINES = {
@@ -34,13 +35,32 @@ def respond(message: str, history: list):
34
  # Log the query
35
  logging.info(f"Query: {message}")
36
 
37
- # Check for keywords in the message
38
- for keyword, key in KEYWORD_TO_RESPONSE.items():
 
 
 
 
 
39
  if keyword in message:
40
- return ONB_GUIDELINES.get(key, "عذرًا، لم يتم التعرف على الخيار المحدد.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  # Default response if no keyword is matched
43
- return "عذرًا، لم أفهم سؤالك. الرجاء إعادة الصياغة أو اختيار أحد الخيارات التالية: " + ", ".join(KEYWORD_TO_RESPONSE.keys())
44
 
45
  # Main menu with submenus
46
  main_menu = {
 
1
  import gradio as gr
2
  import logging
3
+ from difflib import get_close_matches # For fuzzy matching
4
 
5
  # Omdurman National Bank-specific guidelines
6
  ONB_GUIDELINES = {
 
35
  # Log the query
36
  logging.info(f"Query: {message}")
37
 
38
+ # Check for "Back to Menu" keyword
39
+ if "القائمة" in message or "menu" in message.lower():
40
+ return "main_menu"
41
+
42
+ # Find all matching keywords using fuzzy matching
43
+ matches = []
44
+ for keyword in KEYWORD_TO_RESPONSE.keys():
45
  if keyword in message:
46
+ matches.append(keyword)
47
+ else:
48
+ # Use fuzzy matching for similar words
49
+ close_matches = get_close_matches(keyword, message.split(), n=1, cutoff=0.6)
50
+ if close_matches:
51
+ matches.append(keyword)
52
+
53
+ # If multiple matches are found, return all responses
54
+ if matches:
55
+ responses = []
56
+ for match in matches:
57
+ key = KEYWORD_TO_RESPONSE.get(match)
58
+ if key:
59
+ responses.append(ONB_GUIDELINES.get(key))
60
+ return "\n\n".join(responses)
61
 
62
  # Default response if no keyword is matched
63
+ return "عذرًا، لم أفهم سؤالك. الرجاء إعادة الصياغة أو كتابة 'القائمة' للعودة إلى القائمة الرئيسية."
64
 
65
  # Main menu with submenus
66
  main_menu = {