Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import pipeline
|
3 |
import re
|
4 |
|
5 |
-
# Load Arabic NLP model for intent classification
|
6 |
-
intent_classifier_ar = pipeline("text-classification", model="aubmindlab/bert-base-arabertv02")
|
7 |
-
|
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,17 +31,44 @@ ONB_GUIDELINES_EN = {
|
|
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):
|
@@ -59,12 +78,20 @@ def detect_language(text):
|
|
59 |
return language
|
60 |
|
61 |
def classify_intent(message: str):
|
62 |
-
#
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
for intent_key, keywords in INTENT_KEYWORDS.items():
|
65 |
for keyword in keywords:
|
66 |
-
if keyword.lower() in
|
67 |
return intent_key
|
|
|
68 |
return "unknown"
|
69 |
|
70 |
def respond(message: str):
|
@@ -83,24 +110,27 @@ def respond(message: str):
|
|
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": "",
|
90 |
"en": ""
|
91 |
}
|
92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
# If intent is recognized, return the corresponding response
|
94 |
if intent != "unknown":
|
95 |
responses["ar"] = ONB_GUIDELINES_AR.get(intent, "عذرًا، لم يتم التعرف على الخيار المحدد.")
|
96 |
responses["en"] = ONB_GUIDELINES_EN.get(intent, "Sorry, the selected option was not recognized.")
|
97 |
else:
|
98 |
-
# Default response if no intent is matched
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
responses["ar"] = f"عذرًا، لم أفهم سؤالك. الرجاء إعادة الصياغة أو اختيار أحد الخيارات التالية: {ar_options}"
|
103 |
-
responses["en"] = f"Sorry, I didn't understand your question. Please rephrase or choose one of the following options: {en_options}"
|
104 |
|
105 |
return responses
|
106 |
|
@@ -137,7 +167,7 @@ custom_css = """
|
|
137 |
|
138 |
.header-section {
|
139 |
background-color: #1a5276;
|
140 |
-
|
141 |
padding: 1rem;
|
142 |
border-radius: 10px;
|
143 |
margin-bottom: 1rem;
|
@@ -155,6 +185,10 @@ custom_css = """
|
|
155 |
text-align: right;
|
156 |
margin-bottom: 1rem;
|
157 |
}
|
|
|
|
|
|
|
|
|
158 |
"""
|
159 |
|
160 |
# Chat interface with enhanced UI
|
@@ -190,6 +224,10 @@ with gr.Blocks(css=custom_css) as demo:
|
|
190 |
with gr.Column(scale=1):
|
191 |
submit_btn = gr.Button("Send | إرسال", variant="primary")
|
192 |
|
|
|
|
|
|
|
|
|
193 |
with gr.Row(elem_classes="footer-section"):
|
194 |
gr.Markdown("© 2025 Omdurman National Bank. All Rights Reserved. | جميع الحقوق محفوظة لبنك أم درمان الوطني ٢٠٢٥ ©")
|
195 |
|
@@ -225,6 +263,19 @@ with gr.Blocks(css=custom_css) as demo:
|
|
225 |
|
226 |
return "", chat_history
|
227 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
228 |
# Link inputs and button to response function
|
229 |
submit_btn.click(
|
230 |
fn=on_submit,
|
@@ -232,6 +283,13 @@ with gr.Blocks(css=custom_css) as demo:
|
|
232 |
outputs=[text_input, chat_box]
|
233 |
)
|
234 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
235 |
# Also trigger on Enter key
|
236 |
text_input.submit(
|
237 |
fn=on_submit,
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import re
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
# Load language detection model only (smaller model)
|
5 |
from transformers import pipeline
|
6 |
language_detector = pipeline("text-classification", model="papluca/xlm-roberta-base-language-detection")
|
7 |
|
|
|
8 |
# Omdurman National Bank-specific guidelines in Arabic
|
9 |
ONB_GUIDELINES_AR = {
|
10 |
"balance": "يمكنك التحقق من رصيدك عبر الإنترنت أو عبر تطبيق الهاتف الخاص ببنك الوطني.",
|
|
|
31 |
"contact": "Contact us at 249-123-456-789 or via email at [email protected]."
|
32 |
}
|
33 |
|
34 |
+
# Menu options in both languages
|
35 |
+
MENU_AR = """
|
36 |
+
قائمة الخدمات المصرفية:
|
37 |
+
1. رصيد - استعلام عن رصيد حسابك
|
38 |
+
2. بطاقة - الإبلاغ عن بطاقة مفقودة
|
39 |
+
3. قرض - معلومات عن القروض
|
40 |
+
4. تحويل - تحويل الأموال
|
41 |
+
5. حساب - فتح حساب جديد
|
42 |
+
6. فائدة - أسعار الفائدة
|
43 |
+
7. فرع - مواقع الفروع
|
44 |
+
8. ساعات - ساعات العمل
|
45 |
+
9. اتصال - معلومات الاتصال
|
46 |
+
"""
|
47 |
+
|
48 |
+
MENU_EN = """
|
49 |
+
Banking Services Menu:
|
50 |
+
1. balance - Check your account balance
|
51 |
+
2. card - Report a lost card
|
52 |
+
3. loan - Information about loans
|
53 |
+
4. transfer - Transfer funds
|
54 |
+
5. account - Open a new account
|
55 |
+
6. interest - Interest rates
|
56 |
+
7. branch - Branch locations
|
57 |
+
8. hours - Working hours
|
58 |
+
9. contact - Contact information
|
59 |
+
"""
|
60 |
+
|
61 |
# Map intents to keywords (enhanced)
|
62 |
INTENT_KEYWORDS = {
|
63 |
+
"balance": ["balance", "check balance", "account balance", "how much", "رصيد", "حساب", "كم المبلغ", "1"],
|
64 |
+
"lost_card": ["lost", "card", "stolen", "missing", "فقدت", "بطاقة", "مسروقة", "ضائعة", "2"],
|
65 |
+
"loan": ["loan", "borrow", "borrowing", "credit", "قرض", "استدانة", "إئتمان", "3"],
|
66 |
+
"transfer": ["transfer", "send money", "payment", "تحويل", "ارسال", "دفع", "4"],
|
67 |
+
"new_account": ["account", "open", "create", "new", "حساب", "فتح", "جديد", "إنشاء", "5"],
|
68 |
+
"interest_rates": ["interest", "rate", "rates", "return", "فائدة", "نسبة", "عائد", "6"],
|
69 |
+
"branches": ["branch", "location", "where", "office", "فرع", "موقع", "أين", "مكتب", "7"],
|
70 |
+
"working_hours": ["hours", "time", "open", "close", "ساعات", "وقت", "مفتوح", "مغلق", "8"],
|
71 |
+
"contact": ["contact", "phone", "email", "call", "اتصال", "هاتف", "بريد", "اتصل", "9"]
|
72 |
}
|
73 |
|
74 |
def detect_language(text):
|
|
|
78 |
return language
|
79 |
|
80 |
def classify_intent(message: str):
|
81 |
+
# Check for menu request
|
82 |
+
menu_keywords = ["menu", "options", "help", "قائمة", "خيارات", "مساعدة"]
|
83 |
+
message_lower = message.lower()
|
84 |
+
|
85 |
+
for keyword in menu_keywords:
|
86 |
+
if keyword in message_lower:
|
87 |
+
return "menu"
|
88 |
+
|
89 |
+
# Use keyword matching for intent classification
|
90 |
for intent_key, keywords in INTENT_KEYWORDS.items():
|
91 |
for keyword in keywords:
|
92 |
+
if keyword.lower() in message_lower:
|
93 |
return intent_key
|
94 |
+
|
95 |
return "unknown"
|
96 |
|
97 |
def respond(message: str):
|
|
|
110 |
|
111 |
# Classify the user's intent using keyword matching
|
112 |
intent = classify_intent(message)
|
113 |
+
|
114 |
# Prepare responses in both languages
|
115 |
responses = {
|
116 |
"ar": "",
|
117 |
"en": ""
|
118 |
}
|
119 |
|
120 |
+
# Special handling for menu request
|
121 |
+
if intent == "menu":
|
122 |
+
responses["ar"] = MENU_AR
|
123 |
+
responses["en"] = MENU_EN
|
124 |
+
return responses
|
125 |
+
|
126 |
# If intent is recognized, return the corresponding response
|
127 |
if intent != "unknown":
|
128 |
responses["ar"] = ONB_GUIDELINES_AR.get(intent, "عذرًا، لم يتم التعرف على الخيار المحدد.")
|
129 |
responses["en"] = ONB_GUIDELINES_EN.get(intent, "Sorry, the selected option was not recognized.")
|
130 |
else:
|
131 |
+
# Default response if no intent is matched - show menu
|
132 |
+
responses["ar"] = "عذرًا، لم أفهم سؤالك. إليك قائمة بالخدمات المتاحة:" + MENU_AR
|
133 |
+
responses["en"] = "Sorry, I didn't understand your question. Here's a menu of available services:" + MENU_EN
|
|
|
|
|
|
|
134 |
|
135 |
return responses
|
136 |
|
|
|
167 |
|
168 |
.header-section {
|
169 |
background-color: #1a5276;
|
170 |
+
color: white;
|
171 |
padding: 1rem;
|
172 |
border-radius: 10px;
|
173 |
margin-bottom: 1rem;
|
|
|
185 |
text-align: right;
|
186 |
margin-bottom: 1rem;
|
187 |
}
|
188 |
+
|
189 |
+
.menu-button {
|
190 |
+
margin-top: 0.5rem;
|
191 |
+
}
|
192 |
"""
|
193 |
|
194 |
# Chat interface with enhanced UI
|
|
|
224 |
with gr.Column(scale=1):
|
225 |
submit_btn = gr.Button("Send | إرسال", variant="primary")
|
226 |
|
227 |
+
with gr.Row():
|
228 |
+
with gr.Column(elem_classes="menu-button"):
|
229 |
+
menu_btn = gr.Button("Show Menu | إظهار القائمة")
|
230 |
+
|
231 |
with gr.Row(elem_classes="footer-section"):
|
232 |
gr.Markdown("© 2025 Omdurman National Bank. All Rights Reserved. | جميع الحقوق محفوظة لبنك أم درمان الوطني ٢٠٢٥ ©")
|
233 |
|
|
|
263 |
|
264 |
return "", chat_history
|
265 |
|
266 |
+
# Handle menu button click
|
267 |
+
def show_menu(chat_history, lang):
|
268 |
+
menu_responses = {
|
269 |
+
"ar": MENU_AR,
|
270 |
+
"en": MENU_EN
|
271 |
+
}
|
272 |
+
|
273 |
+
# Add system message showing the menu
|
274 |
+
menu_text = menu_responses[lang]
|
275 |
+
chat_history.append([None, menu_text])
|
276 |
+
|
277 |
+
return chat_history
|
278 |
+
|
279 |
# Link inputs and button to response function
|
280 |
submit_btn.click(
|
281 |
fn=on_submit,
|
|
|
283 |
outputs=[text_input, chat_box]
|
284 |
)
|
285 |
|
286 |
+
# Link menu button to show menu function
|
287 |
+
menu_btn.click(
|
288 |
+
fn=show_menu,
|
289 |
+
inputs=[chat_box, selected_lang],
|
290 |
+
outputs=[chat_box]
|
291 |
+
)
|
292 |
+
|
293 |
# Also trigger on Enter key
|
294 |
text_input.submit(
|
295 |
fn=on_submit,
|