Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,8 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
from transformers import pipeline # For NLP
|
4 |
-
from difflib import get_close_matches # For fuzzy matching
|
5 |
|
6 |
-
# Load Arabic
|
7 |
-
|
8 |
|
9 |
# Omdurman National Bank-specific guidelines
|
10 |
ONB_GUIDELINES = {
|
@@ -19,90 +17,64 @@ ONB_GUIDELINES = {
|
|
19 |
"contact": "الاتصال بنا على الرقم 249-123-456-789 أو عبر البريد الإلكتروني [email protected]."
|
20 |
}
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
-
|
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}")
|
47 |
|
48 |
-
#
|
49 |
-
if "
|
50 |
-
return
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
if keyword in message:
|
63 |
-
response = ONB_GUIDELINES.get(key, "عذرًا، لم يتم التعرف على الخيار المحدد.")
|
64 |
-
return history + [[message, response]]
|
65 |
|
66 |
-
# Default response if no intent
|
67 |
-
|
68 |
-
return history + [[message, response]]
|
69 |
|
70 |
-
#
|
71 |
-
main_menu = {
|
72 |
-
"الحسابات": ["التحقق من الرصيد", "فتح حساب جديد"],
|
73 |
-
"القروض": ["شروط الحصول على قرض", "أسعار الفائدة"],
|
74 |
-
"الفروع": ["فروع البنك", "ساعات العمل"],
|
75 |
-
"الدعم": ["الإبلاغ عن فقدان البطاقة", "الاتصال بالبنك"]
|
76 |
-
}
|
77 |
-
|
78 |
-
# Omdurman National Bank-specific interface
|
79 |
with gr.Blocks(css=".gradio-container {direction: rtl;}") as demo:
|
80 |
gr.Markdown("# <center>بنك أم درمان الوطني - المساعد المصرفي</center>")
|
81 |
|
82 |
with gr.Tab("المحادثة"):
|
83 |
-
gr.Markdown("##
|
|
|
|
|
|
|
84 |
|
85 |
-
#
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
95 |
)
|
96 |
-
|
97 |
-
with gr.Tab("القائمة الرئيسية"):
|
98 |
-
gr.Markdown("## القائمة الرئيسية")
|
99 |
-
for category, options in main_menu.items():
|
100 |
-
with gr.Accordion(category):
|
101 |
-
for option in options:
|
102 |
-
gr.Button(option).click(
|
103 |
-
fn=lambda opt=option: respond(opt, []),
|
104 |
-
outputs=chatbot.chatbot
|
105 |
-
)
|
106 |
|
107 |
if __name__ == "__main__":
|
108 |
demo.launch(
|
|
|
1 |
import gradio as gr
|
2 |
+
import spacy
|
|
|
|
|
3 |
|
4 |
+
# Load the spaCy Arabic model
|
5 |
+
nlp = spacy.load("xx_ent_wiki_sm")
|
6 |
|
7 |
# Omdurman National Bank-specific guidelines
|
8 |
ONB_GUIDELINES = {
|
|
|
17 |
"contact": "الاتصال بنا على الرقم 249-123-456-789 أو عبر البريد الإلكتروني [email protected]."
|
18 |
}
|
19 |
|
20 |
+
# NLP-based intent detection function
|
21 |
+
def detect_intent(message: str):
|
22 |
+
doc = nlp(message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
# Example of keyword-based intent detection
|
25 |
+
if "رصيد" in doc.text:
|
26 |
+
return "balance"
|
27 |
+
elif "بطاقة" in doc.text:
|
28 |
+
return "lost_card"
|
29 |
+
elif "قرض" in doc.text:
|
30 |
+
return "loan"
|
31 |
+
elif "تحويل" in doc.text:
|
32 |
+
return "transfer"
|
33 |
+
elif "حساب" in doc.text:
|
34 |
+
return "new_account"
|
35 |
+
elif "فائدة" in doc.text:
|
36 |
+
return "interest_rates"
|
37 |
+
elif "فرع" in doc.text:
|
38 |
+
return "branches"
|
39 |
+
elif "ساعات" in doc.text:
|
40 |
+
return "working_hours"
|
41 |
+
elif "اتصال" in doc.text:
|
42 |
+
return "contact"
|
43 |
+
else:
|
44 |
+
return None
|
45 |
+
|
46 |
+
def respond(message: str):
|
47 |
+
# Detect the intent using NLP
|
48 |
+
intent = detect_intent(message)
|
49 |
|
50 |
+
if intent:
|
51 |
+
return ONB_GUIDELINES.get(intent, "عذرًا، لم يتم التعرف على الخيار المحدد.")
|
|
|
|
|
|
|
52 |
|
53 |
+
# Default response if no intent is matched
|
54 |
+
return "عذرًا، لم أفهم سؤالك. الرجاء إعادة الصياغة أو اختيار أحد الخيارات التالية: " + ", ".join(ONB_GUIDELINES.keys())
|
|
|
55 |
|
56 |
+
# Chat interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
with gr.Blocks(css=".gradio-container {direction: rtl;}") as demo:
|
58 |
gr.Markdown("# <center>بنك أم درمان الوطني - المساعد المصرفي</center>")
|
59 |
|
60 |
with gr.Tab("المحادثة"):
|
61 |
+
gr.Markdown("## اكتب سؤالك هنا:")
|
62 |
+
|
63 |
+
# Text input
|
64 |
+
text_input = gr.Textbox(label="السؤال")
|
65 |
|
66 |
+
# Submit button
|
67 |
+
submit_btn = gr.Button("إرسال")
|
68 |
+
|
69 |
+
# Output textbox for responses
|
70 |
+
output = gr.Textbox(label="الرد", interactive=False)
|
71 |
+
|
72 |
+
# Link inputs and button to response function
|
73 |
+
submit_btn.click(
|
74 |
+
fn=respond,
|
75 |
+
inputs=text_input,
|
76 |
+
outputs=output
|
77 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
if __name__ == "__main__":
|
80 |
demo.launch(
|