Update app.py
Browse files
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
|
19 |
-
|
20 |
-
"
|
21 |
-
"
|
22 |
-
"
|
23 |
-
"
|
24 |
-
"
|
25 |
-
"
|
26 |
-
"
|
27 |
-
"
|
28 |
-
"
|
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 |
-
#
|
43 |
-
|
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
|
54 |
-
if
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
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
|