File size: 5,787 Bytes
ec11884
68361c5
 
ec11884
cd2bc8b
 
 
 
 
 
 
 
 
 
 
 
 
df9b536
 
 
 
 
 
 
 
 
 
 
 
 
b9f9e30
 
 
 
 
 
 
 
 
 
 
 
 
68361c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd2bc8b
 
 
68361c5
 
 
 
 
 
 
 
 
 
 
549f4f9
68361c5
 
 
 
 
b9f9e30
 
68361c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1f9a88
cd2bc8b
 
 
 
 
68361c5
e1f9a88
df9b536
 
 
 
 
 
 
68361c5
 
 
df9b536
 
e1f9a88
 
 
df9b536
68361c5
 
 
 
 
 
 
df9b536
 
68361c5
 
df9b536
ec11884
 
549f4f9
cd2bc8b
 
 
b9f9e30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import gradio as gr
import logging
from transformers import pipeline

# Omdurman National Bank-specific guidelines
ONB_GUIDELINES = {
    "balance": "يمكنك التحقق من رصيدك عبر الإنترنت أو عبر تطبيق الهاتف الخاص ببنك أم درمان الوطني.",
    "lost_card": "في حالة فقدان البطاقة، اتصل بالرقم 249-123-456-789 فورًا.",
    "loan": "شروط القرض تشمل الحد الأدنى للدخل (5000 جنيه سوداني) وتاريخ ائتماني جيد.",
    "transfer": "لتحويل الأموال، استخدم تطبيق الهاتف أو الخدمة المصرفية عبر الإنترنت.",
    "new_account": "لفتح حساب جديد، قم بزيارة أقرب فرع مع جواز سفرك أو هويتك الوطنية.",
    "interest_rates": "أسعار الفائدة على الودائع تتراوح بين 5% إلى 10% سنويًا.",
    "branches": "فروعنا موجودة في أم درمان، الخرطوم، وبورتسودان. زيارة موقعنا للتفاصيل.",
    "working_hours": "ساعات العمل من 8 صباحًا إلى 3 مساءً من الأحد إلى الخميس.",
    "contact": "الاتصال بنا على الرقم 249-123-456-789 أو عبر البريد الإلكتروني [email protected]."
}

# Arabic menu options
menu_options = [
    "التحقق من الرصيد",
    "الإبلاغ عن فقدان البطاقة",
    "شروط الحصول على قرض",
    "تحويل الأموال",
    "فتح حساب جديد",
    "أسعار الفائدة",
    "فروع البنك",
    "ساعات العمل",
    "الاتصال بالبنك"
]

# Map Arabic menu options to English keys
OPTION_TO_KEY = {
    "التحقق من الرصيد": "balance",
    "الإبلاغ عن فقدان البطاقة": "lost_card",
    "شروط الحصول على قرض": "loan",
    "تحويل الأموال": "transfer",
    "فتح حساب جديد": "new_account",
    "أسعار الفائدة": "interest_rates",
    "فروع البنك": "branches",
    "ساعات العمل": "working_hours",
    "الاتصال بالبنك": "contact"
}

# Load Arabic intent classification model
intent_classifier = pipeline("text-classification", model="aubmindlab/bert-base-arabertv02")

# Map intents to menu options
INTENT_TO_OPTION = {
    "balance": "التحقق من الرصيد",
    "lost_card": "الإبلاغ عن فقدان البطاقة",
    "loan": "شروط الحصول على قرض",
    "transfer": "تحويل الأموال",
    "new_account": "فتح حساب جديد",
    "interest_rates": "أسعار الفائدة",
    "branches": "فروع البنك",
    "working_hours": "ساعات العمل",
    "contact": "الاتصال بالبنك"
}

# Set up logging for analytics
logging.basicConfig(filename='chatbot_queries.log', level=logging.INFO)

def classify_intent(message: str):
    # Classify the user's intent
    result = intent_classifier(message)
    intent = result[0]['label']
    return INTENT_TO_OPTION.get(intent, "عذرًا، لم أفهم سؤالك. الرجاء اختيار أحد الخيارات التالية.")

def respond(option: str, message: str, history: list):
    # If the user selects from the dropdown
    if option:
        key = OPTION_TO_KEY.get(option)
        response = ONB_GUIDELINES.get(key, "عذرًا، لم يتم التعرف على الخيار المحدد.")
    
    # If the user types a message
    elif message:
        option = classify_intent(message)
        key = OPTION_TO_KEY.get(option)
        response = ONB_GUIDELINES.get(key, "عذرًا، لم أفهم سؤالك. الرجاء اختيار أحد الخيارات التالية.")
    
    else:
        response = "عذرًا، لم يتم تحديد سؤال."
    
    # Add follow-up questions based on intent
    if key == "loan":
        response += "\n\nما هو نوع القرض الذي تبحث عنه؟"
    elif key == "new_account":
        response += "\n\nهل تريد فتح حساب شخصي أو حساب تجاري؟"
    
    # Add feedback buttons
    feedback_buttons = gr.Row([
        gr.Button("👍", variant="secondary"),
        gr.Button("👎", variant="secondary")
    ])
    
    return response, feedback_buttons

# Omdurman National Bank-specific interface
with gr.Blocks(css=".gradio-container {direction: rtl;}") as demo:
    gr.Markdown("# <center>بنك أم درمان الوطني - المساعد المصرفي</center>")
    
    with gr.Tab("المحادثة"):
        gr.Markdown("## اختر أحد الخيارات التالية أو اكتب سؤالك:")
        
        # Dropdown menu for questions
        dropdown = gr.Dropdown(
            choices=menu_options,
            label="اختر سؤالك",
            interactive=True
        )
        
        # Free-text input
        text_input = gr.Textbox(label="أو اكتب سؤالك هنا")
        
        # Submit button
        submit_btn = gr.Button("إرسال")
        
        # Output textbox for responses
        output = gr.Textbox(label="الرد", interactive=False)
        
        # Feedback buttons
        feedback_buttons = gr.Row([
            gr.Button("👍", variant="secondary"),
            gr.Button("👎", variant="secondary")
        ])
        
        # Link inputs and button to response function
        submit_btn.click(
            fn=respond,
            inputs=[dropdown, text_input],
            outputs=[output, feedback_buttons]
        )

if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=True  # Enable public link
    )