Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,8 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
2 |
|
3 |
# Omdurman National Bank-specific guidelines
|
4 |
ONB_GUIDELINES = {
|
@@ -13,27 +17,40 @@ ONB_GUIDELINES = {
|
|
13 |
"contact": "الاتصال بنا على الرقم 249-123-456-789 أو عبر البريد الإلكتروني [email protected]."
|
14 |
}
|
15 |
|
16 |
-
# Map
|
17 |
-
|
18 |
-
"
|
19 |
-
"
|
20 |
-
"
|
21 |
-
"
|
22 |
-
"
|
23 |
-
"
|
24 |
-
"
|
25 |
-
"
|
26 |
-
"
|
27 |
}
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
def respond(message: str):
|
30 |
-
#
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
if keyword in message:
|
33 |
return ONB_GUIDELINES.get(key, "عذرًا، لم يتم التعرف على الخيار المحدد.")
|
34 |
|
35 |
-
# Default response if no keyword is matched
|
36 |
-
return "عذرًا، لم أفهم سؤالك. الرجاء إعادة الصياغة أو اختيار أحد الخيارات التالية: " + ", ".join(
|
37 |
|
38 |
# Chat interface
|
39 |
with gr.Blocks(css=".gradio-container {direction: rtl;}") as demo:
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline # For NLP
|
3 |
+
|
4 |
+
# Load Arabic NLP model for intent classification
|
5 |
+
intent_classifier = pipeline("text-classification", model="aubmindlab/bert-base-arabertv02")
|
6 |
|
7 |
# Omdurman National Bank-specific guidelines
|
8 |
ONB_GUIDELINES = {
|
|
|
17 |
"contact": "الاتصال بنا على الرقم 249-123-456-789 أو عبر البريد الإلكتروني [email protected]."
|
18 |
}
|
19 |
|
20 |
+
# Map intents to responses
|
21 |
+
INTENT_TO_RESPONSE = {
|
22 |
+
"balance": "balance",
|
23 |
+
"lost_card": "lost_card",
|
24 |
+
"loan": "loan",
|
25 |
+
"transfer": "transfer",
|
26 |
+
"new_account": "new_account",
|
27 |
+
"interest_rates": "interest_rates",
|
28 |
+
"branches": "branches",
|
29 |
+
"working_hours": "working_hours",
|
30 |
+
"contact": "contact"
|
31 |
}
|
32 |
|
33 |
+
def classify_intent(message: str):
|
34 |
+
# Use NLP model to classify the user's intent
|
35 |
+
result = intent_classifier(message)
|
36 |
+
intent = result[0]['label']
|
37 |
+
return INTENT_TO_RESPONSE.get(intent, "unknown")
|
38 |
+
|
39 |
def respond(message: str):
|
40 |
+
# Classify the user's intent using NLP
|
41 |
+
intent = classify_intent(message)
|
42 |
+
|
43 |
+
# If intent is recognized, return the corresponding response
|
44 |
+
if intent != "unknown":
|
45 |
+
return ONB_GUIDELINES.get(intent, "عذرًا، لم يتم التعرف على الخيار المحدد.")
|
46 |
+
|
47 |
+
# Fallback to keyword matching if NLP doesn't recognize the intent
|
48 |
+
for keyword, key in INTENT_TO_RESPONSE.items():
|
49 |
if keyword in message:
|
50 |
return ONB_GUIDELINES.get(key, "عذرًا، لم يتم التعرف على الخيار المحدد.")
|
51 |
|
52 |
+
# Default response if no intent or keyword is matched
|
53 |
+
return "عذرًا، لم أفهم سؤالك. الرجاء إعادة الصياغة أو اختيار أحد الخيارات التالية: " + ", ".join(INTENT_TO_RESPONSE.keys())
|
54 |
|
55 |
# Chat interface
|
56 |
with gr.Blocks(css=".gradio-container {direction: rtl;}") as demo:
|