waleedmohd commited on
Commit
68361c5
·
verified ·
1 Parent(s): df9b536

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -19
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
- import logging # For analytics
 
3
 
4
  # Omdurman National Bank-specific guidelines
5
  ONB_GUIDELINES = {
@@ -40,28 +41,66 @@ OPTION_TO_KEY = {
40
  "الاتصال بالبنك": "contact"
41
  }
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  # Set up logging for analytics
44
  logging.basicConfig(filename='chatbot_queries.log', level=logging.INFO)
45
 
46
- def respond(option: str):
47
- # Log the selected option
48
- logging.info(f"Selected Option: {option}")
 
 
 
 
 
 
 
 
49
 
50
- # Map the Arabic option to the English key
51
- key = OPTION_TO_KEY.get(option)
 
 
 
52
 
53
- # Return the corresponding response
54
- if key:
55
- return ONB_GUIDELINES.get(key, "عذرًا، لم يتم التعرف على الخيار المحدد.")
56
  else:
57
- return "عذرًا، لم يتم التعرف على الخيار المحدد."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  # Omdurman National Bank-specific interface
60
  with gr.Blocks(css=".gradio-container {direction: rtl;}") as demo:
61
  gr.Markdown("# <center>بنك أم درمان الوطني - المساعد المصرفي</center>")
62
 
63
  with gr.Tab("المحادثة"):
64
- gr.Markdown("## اختر أحد الخيارات التالية:")
65
 
66
  # Dropdown menu for questions
67
  dropdown = gr.Dropdown(
@@ -70,23 +109,27 @@ with gr.Blocks(css=".gradio-container {direction: rtl;}") as demo:
70
  interactive=True
71
  )
72
 
 
 
 
73
  # Submit button
74
  submit_btn = gr.Button("إرسال")
75
 
76
  # Output textbox for responses
77
  output = gr.Textbox(label="الرد", interactive=False)
78
 
79
- # Link dropdown and button to response function
 
 
 
 
 
 
80
  submit_btn.click(
81
  fn=respond,
82
- inputs=dropdown,
83
- outputs=output
84
  )
85
-
86
- with gr.Tab("الإرشادات المصرفية"):
87
- gr.Markdown("## إرشادات بنك أم درمان الوطني")
88
- for key, value in ONB_GUIDELINES.items():
89
- gr.Markdown(f"**{key.capitalize()}**: {value}")
90
 
91
  if __name__ == "__main__":
92
  demo.launch(
 
1
  import gradio as gr
2
+ import logging
3
+ from transformers import pipeline
4
 
5
  # Omdurman National Bank-specific guidelines
6
  ONB_GUIDELINES = {
 
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:
100
  gr.Markdown("# <center>بنك أم درمان الوطني - المساعد المصرفي</center>")
101
 
102
  with gr.Tab("المحادثة"):
103
+ gr.Markdown("## اختر أحد الخيارات التالية أو اكتب سؤالك:")
104
 
105
  # Dropdown menu for questions
106
  dropdown = gr.Dropdown(
 
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(