waleedmohd commited on
Commit
9a7118f
·
verified ·
1 Parent(s): 8e0677a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -35
app.py CHANGED
@@ -8,9 +8,11 @@ intent_classifier_ar = pipeline("text-classification", model="aubmindlab/bert-ba
8
  # Load English NLP model for zero-shot classification
9
  intent_classifier_en = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
10
 
11
- # Load language detection model
 
12
  language_detector = pipeline("text-classification", model="papluca/xlm-roberta-base-language-detection")
13
 
 
14
  # Omdurman National Bank-specific guidelines in Arabic
15
  ONB_GUIDELINES_AR = {
16
  "balance": "يمكنك التحقق من رصيدك عبر الإنترنت أو عبر تطبيق الهاتف الخاص ببنك الوطني.",
@@ -37,17 +39,17 @@ ONB_GUIDELINES_EN = {
37
  "contact": "Contact us at 249-123-456-789 or via email at [email protected]."
38
  }
39
 
40
- # Map intents to responses
41
  INTENT_KEYWORDS = {
42
- "balance": ["balance", "رصيد", "حساب"],
43
- "lost_card": ["lost", "card", "stolen", "فقدت", "بطاقة", "مسروقة"],
44
- "loan": ["loan", "borrow", "قرض", "استدانة"],
45
- "transfer": ["transfer", "send money", "تحويل", "ارسال"],
46
- "new_account": ["account", "open", "حساب", "فتح"],
47
- "interest_rates": ["interest", "rate", "فائدة", "نسبة"],
48
- "branches": ["branch", "location", "فرع", "موقع"],
49
- "working_hours": ["hours", "time", "ساعات", "وقت"],
50
- "contact": ["contact", "phone", "email", "اتصال", "هاتف", "بريد"]
51
  }
52
 
53
  def detect_language(text):
@@ -56,26 +58,14 @@ def detect_language(text):
56
  language = result[0]['label']
57
  return language
58
 
59
- def classify_intent(message: str, language: str):
60
- # Use appropriate classifier based on language
61
- if language == "ar":
62
- # For Arabic
63
- result = intent_classifier_ar(message)
64
- intent = result[0]['label']
65
- else:
66
- # For English - using zero-shot classification correctly
67
- result = intent_classifier_en(message, candidate_labels=list(INTENT_KEYWORDS.keys()))
68
- intent = result["labels"][0]
69
-
70
- # Fallback to keyword matching
71
- if intent not in INTENT_KEYWORDS:
72
- for intent_key, keywords in INTENT_KEYWORDS.items():
73
- for keyword in keywords:
74
- if re.search(r'\b' + re.escape(keyword.lower()) + r'\b', message.lower()):
75
- return intent_key
76
- return "unknown"
77
-
78
- return intent
79
 
80
  def respond(message: str):
81
  if not message.strip():
@@ -91,9 +81,9 @@ def respond(message: str):
91
  if language != "ar" and language != "en":
92
  language = "en"
93
 
94
- # Classify the user's intent
95
- intent = classify_intent(message, language)
96
-
97
  # Prepare responses in both languages
98
  responses = {
99
  "ar": "",
@@ -147,7 +137,7 @@ custom_css = """
147
 
148
  .header-section {
149
  background-color: #1a5276;
150
- color: white;
151
  padding: 1rem;
152
  border-radius: 10px;
153
  margin-bottom: 1rem;
 
8
  # Load English NLP model for zero-shot classification
9
  intent_classifier_en = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
10
 
11
+ # Load language detection model only (smaller model)
12
+ from transformers import pipeline
13
  language_detector = pipeline("text-classification", model="papluca/xlm-roberta-base-language-detection")
14
 
15
+
16
  # Omdurman National Bank-specific guidelines in Arabic
17
  ONB_GUIDELINES_AR = {
18
  "balance": "يمكنك التحقق من رصيدك عبر الإنترنت أو عبر تطبيق الهاتف الخاص ببنك الوطني.",
 
39
  "contact": "Contact us at 249-123-456-789 or via email at [email protected]."
40
  }
41
 
42
+ # Map intents to keywords (enhanced)
43
  INTENT_KEYWORDS = {
44
+ "balance": ["balance", "check balance", "account balance", "how much", "رصيد", "حساب", "كم المبلغ"],
45
+ "lost_card": ["lost", "card", "stolen", "missing", "فقدت", "بطاقة", "مسروقة", "ضائعة"],
46
+ "loan": ["loan", "borrow", "borrowing", "credit", "قرض", "استدانة", "إئتمان"],
47
+ "transfer": ["transfer", "send money", "payment", "تحويل", "ارسال", "دفع"],
48
+ "new_account": ["account", "open", "create", "new", "حساب", "فتح", "جديد", "إنشاء"],
49
+ "interest_rates": ["interest", "rate", "rates", "return", "فائدة", "نسبة", "عائد"],
50
+ "branches": ["branch", "location", "where", "office", "فرع", "موقع", "أين", "مكتب"],
51
+ "working_hours": ["hours", "time", "open", "close", "ساعات", "وقت", "مفتوح", "مغلق"],
52
+ "contact": ["contact", "phone", "email", "call", "اتصال", "هاتف", "بريد", "اتصل"]
53
  }
54
 
55
  def detect_language(text):
 
58
  language = result[0]['label']
59
  return language
60
 
61
+ def classify_intent(message: str):
62
+ # Use keyword matching for all languages
63
+ message = message.lower()
64
+ for intent_key, keywords in INTENT_KEYWORDS.items():
65
+ for keyword in keywords:
66
+ if keyword.lower() in message:
67
+ return intent_key
68
+ return "unknown"
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
  def respond(message: str):
71
  if not message.strip():
 
81
  if language != "ar" and language != "en":
82
  language = "en"
83
 
84
+ # Classify the user's intent using keyword matching
85
+ intent = classify_intent(message)
86
+
87
  # Prepare responses in both languages
88
  responses = {
89
  "ar": "",
 
137
 
138
  .header-section {
139
  background-color: #1a5276;
140
+ font-color: white;
141
  padding: 1rem;
142
  border-radius: 10px;
143
  margin-bottom: 1rem;