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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -30
app.py CHANGED
@@ -1,7 +1,11 @@
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 = {
7
  "balance": "يمكنك التحقق من رصيدك عبر الإنترنت أو عبر تطبيق الهاتف الخاص ببنك أم درمان الوطني.",
@@ -15,22 +19,28 @@ ONB_GUIDELINES = {
15
  "contact": "الاتصال بنا على الرقم 249-123-456-789 أو عبر البريد الإلكتروني [email protected]."
16
  }
17
 
18
- # Map keywords to responses
19
- KEYWORD_TO_RESPONSE = {
20
- "رصيد": "balance",
21
- "بطاقة": "lost_card",
22
- "قرض": "loan",
23
- "تحويل": "transfer",
24
- "حساب": "new_account",
25
- "فائدة": "interest_rates",
26
- "فرع": "branches",
27
- "ساعات": "working_hours",
28
- "اتصال": "contact"
29
  }
30
 
31
  # Set up logging for analytics
32
  logging.basicConfig(filename='chatbot_queries.log', level=logging.INFO)
33
 
 
 
 
 
 
 
34
  def respond(message: str, history: list):
35
  # Log the query
36
  logging.info(f"Query: {message}")
@@ -39,27 +49,19 @@ def respond(message: str, history: list):
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
 
1
  import gradio as gr
2
  import logging
3
+ from transformers import pipeline # For NLP
4
  from difflib import get_close_matches # For fuzzy matching
5
 
6
+ # Load Arabic NLP model for intent classification
7
+ intent_classifier = pipeline("text-classification", model="aubmindlab/bert-base-arabertv02")
8
+
9
  # Omdurman National Bank-specific guidelines
10
  ONB_GUIDELINES = {
11
  "balance": "يمكنك التحقق من رصيدك عبر الإنترنت أو عبر تطبيق الهاتف الخاص ببنك أم درمان الوطني.",
 
19
  "contact": "الاتصال بنا على الرقم 249-123-456-789 أو عبر البريد الإلكتروني [email protected]."
20
  }
21
 
22
+ # Map intents to responses
23
+ INTENT_TO_RESPONSE = {
24
+ "balance": "balance",
25
+ "lost_card": "lost_card",
26
+ "loan": "loan",
27
+ "transfer": "transfer",
28
+ "new_account": "new_account",
29
+ "interest_rates": "interest_rates",
30
+ "branches": "branches",
31
+ "working_hours": "working_hours",
32
+ "contact": "contact"
33
  }
34
 
35
  # Set up logging for analytics
36
  logging.basicConfig(filename='chatbot_queries.log', level=logging.INFO)
37
 
38
+ def classify_intent(message: str):
39
+ # Use NLP model to classify the user's intent
40
+ result = intent_classifier(message)
41
+ intent = result[0]['label']
42
+ return INTENT_TO_RESPONSE.get(intent, "unknown")
43
+
44
  def respond(message: str, history: list):
45
  # Log the query
46
  logging.info(f"Query: {message}")
 
49
  if "القائمة" in message or "menu" in message.lower():
50
  return "main_menu"
51
 
52
+ # Classify the user's intent using NLP
53
+ intent = classify_intent(message)
 
 
 
 
 
 
 
 
54
 
55
+ # If intent is recognized, return the corresponding response
56
+ if intent != "unknown":
57
+ return ONB_GUIDELINES.get(intent, "عذرًا، لم يتم التعرف على الخيار المحدد.")
58
+
59
+ # Fallback to keyword matching if NLP doesn't recognize the intent
60
+ for keyword, key in INTENT_TO_RESPONSE.items():
61
+ if keyword in message:
62
+ return ONB_GUIDELINES.get(key, "عذرًا، لم يتم التعرف على الخيار المحدد.")
63
 
64
+ # Default response if no intent or keyword is matched
65
  return "عذرًا، لم أفهم سؤالك. الرجاء إعادة الصياغة أو كتابة 'القائمة' للعودة إلى القائمة الرئيسية."
66
 
67
  # Main menu with submenus