waleedmohd commited on
Commit
4600387
·
verified ·
1 Parent(s): 0465ce0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -100
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import gradio as gr
2
  import logging
3
- from transformers import pipeline
4
 
5
  # Omdurman National Bank-specific guidelines
6
  ONB_GUIDELINES = {
@@ -15,85 +14,41 @@ ONB_GUIDELINES = {
15
  "contact": "الاتصال بنا على الرقم 249-123-456-789 أو عبر البريد الإلكتروني [email protected]."
16
  }
17
 
18
- # Arabic menu options
19
- menu_options = [
20
- "التحقق من الرصيد",
21
- "الإبلاغ عن فقدان البطاقة",
22
- "شروط الحصول على قرض",
23
- "تحويل الأموال",
24
- "فتح حساب جديد",
25
- "أسعار الفائدة",
26
- "فروع البنك",
27
- "ساعات العمل",
28
- "الاتصال بالبنك"
29
- ]
30
-
31
- # Map Arabic menu options to English keys
32
- OPTION_TO_KEY = {
33
- "التحقق من الرصيد": "balance",
34
- "الإبلاغ عن فقدان البطاقة": "lost_card",
35
- "شروط الحصول على قرض": "loan",
36
- "تحويل الأموال": "transfer",
37
- "فتح حساب جديد": "new_account",
38
- "أسعار الفائدة": "interest_rates",
39
- "فروع البنك": "branches",
40
- "ساعات العمل": "working_hours",
41
- "الاتصال بالبنك": "contact"
42
- }
43
-
44
- # Load Arabic intent classification model
45
- intent_classifier = pipeline("text-classification", model="aubmindlab/bert-base-arabertv02")
46
-
47
- # Map intents to menu options
48
- INTENT_TO_OPTION = {
49
- "balance": "التحقق من الرصيد",
50
- "lost_card": "الإبلاغ عن فقدان البطاقة",
51
- "loan": "شروط الحصول على قرض",
52
- "transfer": "تحويل الأموال",
53
- "new_account": "فتح حساب جديد",
54
- "interest_rates": "أسعار الفائدة",
55
- "branches": "فروع البنك",
56
- "working_hours": "ساعات العمل",
57
- "contact": "الاتصال بالبنك"
58
  }
59
 
60
  # Set up logging for analytics
61
  logging.basicConfig(filename='chatbot_queries.log', level=logging.INFO)
62
 
63
- def classify_intent(message: str):
64
- # Classify the user's intent
65
- result = intent_classifier(message)
66
- intent = result[0]['label']
67
- return INTENT_TO_OPTION.get(intent, "عذرًا، لم أفهم سؤالك. الرجاء اختيار أحد الخيارات التالية.")
68
-
69
- def respond(option: str, message: str, history: list):
70
- # If the user selects from the dropdown
71
- if option:
72
- key = OPTION_TO_KEY.get(option)
73
- response = ONB_GUIDELINES.get(key, "عذرًا، لم يتم التعرف على الخيار المحدد.")
74
 
75
- # If the user types a message
76
- elif message:
77
- option = classify_intent(message)
78
- key = OPTION_TO_KEY.get(option)
79
- response = ONB_GUIDELINES.get(key, "عذرًا، لم أفهم سؤالك. الرجاء اختيار أحد الخيارات التالية.")
80
 
81
- else:
82
- response = "عذرًا، لم يتم تحديد سؤال."
83
-
84
- # Add follow-up questions based on intent
85
- if key == "loan":
86
- response += "\n\nما هو نوع القرض الذي تبحث عنه؟"
87
- elif key == "new_account":
88
- response += "\n\nهل تريد فتح حساب شخصي أو حساب تجاري؟"
89
-
90
- # Add feedback buttons
91
- feedback_buttons = gr.Row([
92
- gr.Button("👍", variant="secondary"),
93
- gr.Button("👎", variant="secondary")
94
- ])
95
-
96
- return response, feedback_buttons
97
 
98
  # Omdurman National Bank-specific interface
99
  with gr.Blocks(css=".gradio-container {direction: rtl;}") as demo:
@@ -102,34 +57,27 @@ with gr.Blocks(css=".gradio-container {direction: rtl;}") as demo:
102
  with gr.Tab("المحادثة"):
103
  gr.Markdown("## اختر أحد الخيارات التالية أو اكتب سؤالك:")
104
 
105
- # Dropdown menu for questions
106
- dropdown = gr.Dropdown(
107
- choices=menu_options,
108
- label="اختر سؤالك",
109
- interactive=True
110
- )
111
-
112
- # Free-text input
113
- text_input = gr.Textbox(label="أو اكتب سؤالك هنا")
114
-
115
- # Submit button
116
- submit_btn = gr.Button("إرسال")
117
-
118
- # Output textbox for responses
119
- output = gr.Textbox(label="الرد", interactive=False)
120
-
121
- # Feedback buttons
122
- feedback_buttons = gr.Row([
123
- gr.Button("👍", variant="secondary"),
124
- gr.Button("👎", variant="secondary")
125
- ])
126
-
127
- # Link inputs and button to response function
128
- submit_btn.click(
129
- fn=respond,
130
- inputs=[dropdown, text_input],
131
- outputs=[output, feedback_buttons]
132
  )
 
 
 
 
 
 
 
 
 
 
133
 
134
  if __name__ == "__main__":
135
  demo.launch(
 
1
  import gradio as gr
2
  import logging
 
3
 
4
  # Omdurman National Bank-specific guidelines
5
  ONB_GUIDELINES = {
 
14
  "contact": "الاتصال بنا على الرقم 249-123-456-789 أو عبر البريد الإلكتروني [email protected]."
15
  }
16
 
17
+ # Map keywords to responses
18
+ KEYWORD_TO_RESPONSE = {
19
+ "رصيد": "balance",
20
+ "بطاقة": "lost_card",
21
+ "قرض": "loan",
22
+ "تحويل": "transfer",
23
+ "حساب": "new_account",
24
+ "فائدة": "interest_rates",
25
+ "فرع": "branches",
26
+ "ساعات": "working_hours",
27
+ "اتصال": "contact"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  }
29
 
30
  # Set up logging for analytics
31
  logging.basicConfig(filename='chatbot_queries.log', level=logging.INFO)
32
 
33
+ def respond(message: str, history: list):
34
+ # Log the query
35
+ logging.info(f"Query: {message}")
 
 
 
 
 
 
 
 
36
 
37
+ # Check for keywords in the message
38
+ for keyword, key in KEYWORD_TO_RESPONSE.items():
39
+ if keyword in message:
40
+ return ONB_GUIDELINES.get(key, "عذرًا، لم يتم التعرف على الخيار المحدد.")
 
41
 
42
+ # Default response if no keyword is matched
43
+ return "عذرًا، لم أفهم سؤالك. الرجاء إعادة الصياغة أو اختيار أحد الخيارات التالية: " + ", ".join(KEYWORD_TO_RESPONSE.keys())
44
+
45
+ # Main menu with submenus
46
+ main_menu = {
47
+ "الحسابات": ["التحقق من الرصيد", "فتح حساب جديد"],
48
+ "القروض": ["شروط الحصول على قرض", "أسعار الفائدة"],
49
+ "الفروع": ["فروع البنك", "ساعات العمل"],
50
+ "الدعم": ["الإبلاغ عن فقدان البطاقة", "الاتصال بالبنك"]
51
+ }
 
 
 
 
 
 
52
 
53
  # Omdurman National Bank-specific interface
54
  with gr.Blocks(css=".gradio-container {direction: rtl;}") as demo:
 
57
  with gr.Tab("المحادثة"):
58
  gr.Markdown("## اختر أحد الخيارات التالية أو اكتب سؤالك:")
59
 
60
+ # Chat interface
61
+ chatbot = gr.ChatInterface(
62
+ respond,
63
+ examples=[
64
+ "كيف يمكنني التحقق من رصيدي؟", # Check balance
65
+ "أريد الإبلاغ عن فقدان بطاقتي", # Report lost card
66
+ "ما هي شروط الحصول على قرض؟", # Loan eligibility
67
+ "ما هي ساعات العمل؟", # Working hours
68
+ "أين يوجد أقرب فرع؟" # Branch locations
69
+ ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  )
71
+
72
+ with gr.Tab("القائمة الرئيسية"):
73
+ gr.Markdown("## القائمة الرئيسية")
74
+ for category, options in main_menu.items():
75
+ with gr.Accordion(category):
76
+ for option in options:
77
+ gr.Button(option).click(
78
+ fn=lambda opt=option: respond(opt, []),
79
+ outputs=chatbot.chatbot
80
+ )
81
 
82
  if __name__ == "__main__":
83
  demo.launch(