waleedmohd commited on
Commit
9906480
·
verified ·
1 Parent(s): f68f15d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -79
app.py CHANGED
@@ -1,10 +1,8 @@
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 = {
@@ -19,90 +17,64 @@ ONB_GUIDELINES = {
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}")
47
 
48
- # Check for "Back to Menu" keyword
49
- if "القائمة" in message or "menu" in message.lower():
50
- return history + [[message, "تم العودة إلى القائمة الرئيسية."]]
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
- response = ONB_GUIDELINES.get(intent, "عذرًا، لم يتم التعرف على الخيار المحدد.")
58
- return history + [[message, response]]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
- # Fallback to keyword matching if NLP doesn't recognize the intent
61
- for keyword, key in INTENT_TO_RESPONSE.items():
62
- if keyword in message:
63
- response = ONB_GUIDELINES.get(key, "عذرًا، لم يتم التعرف على الخيار المحدد.")
64
- return history + [[message, response]]
65
 
66
- # Default response if no intent or keyword is matched
67
- response = "عذرًا، لم أفهم سؤالك. الرجاء إعادة الصياغة أو كتابة 'القائمة' للعودة إلى القائمة الرئيسية."
68
- return history + [[message, response]]
69
 
70
- # Main menu with submenus
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
- # Chat interface
86
- chatbot = gr.ChatInterface(
87
- respond,
88
- examples=[
89
- "كيف يمكنني التحقق من رصيدي؟", # Check balance
90
- "أريد الإبلاغ عن فقدان بطاقتي", # Report lost card
91
- "ما هي شروط الحصول على قرض؟", # Loan eligibility
92
- "ما هي ساعات العمل؟", # Working hours
93
- "أين يوجد أقرب فرع؟" # Branch locations
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(