Spaces:
Runtime error
Runtime error
File size: 5,370 Bytes
ec11884 68361c5 edd8287 f47a820 ec11884 edd8287 cd2bc8b edd8287 68361c5 cd2bc8b edd8287 4600387 549f4f9 f47a820 77dc0f3 f47a820 edd8287 f47a820 edd8287 cbe4b29 77dc0f3 edd8287 cbe4b29 77dc0f3 b9f9e30 edd8287 cbe4b29 77dc0f3 4600387 e1f9a88 cd2bc8b 68361c5 e1f9a88 4600387 df9b536 4600387 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 |
import gradio as gr
import logging
from transformers import pipeline # For NLP
from difflib import get_close_matches # For fuzzy matching
# Load Arabic NLP model for intent classification
intent_classifier = pipeline("text-classification", model="aubmindlab/bert-base-arabertv02")
# 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]."
}
# Map intents to responses
INTENT_TO_RESPONSE = {
"balance": "balance",
"lost_card": "lost_card",
"loan": "loan",
"transfer": "transfer",
"new_account": "new_account",
"interest_rates": "interest_rates",
"branches": "branches",
"working_hours": "working_hours",
"contact": "contact"
}
# Set up logging for analytics
logging.basicConfig(filename='chatbot_queries.log', level=logging.INFO)
def classify_intent(message: str):
# Use NLP model to classify the user's intent
result = intent_classifier(message)
intent = result[0]['label']
return INTENT_TO_RESPONSE.get(intent, "unknown")
def respond(message: str, history: list):
# Log the query
logging.info(f"Query: {message}")
# Check for "Back to Menu" keyword
if "القائمة" in message or "menu" in message.lower():
return history + [[message, "تم العودة إلى القائمة الرئيسية."]]
# Classify the user's intent using NLP
intent = classify_intent(message)
# If intent is recognized, return the corresponding response
if intent != "unknown":
response = ONB_GUIDELINES.get(intent, "عذرًا، لم يتم التعرف على الخيار المحدد.")
return history + [[message, response]]
# Fallback to keyword matching if NLP doesn't recognize the intent
for keyword, key in INTENT_TO_RESPONSE.items():
if keyword in message:
response = ONB_GUIDELINES.get(key, "عذرًا، لم يتم التعرف على الخيار المحدد.")
return history + [[message, response]]
# Default response if no intent or keyword is matched
response = "عذرًا، لم أفهم سؤالك. الرجاء إعادة الصياغة أو كتابة 'القائمة' للعودة إلى القائمة الرئيسية."
return history + [[message, response]]
# Main menu with submenus
main_menu = {
"الحسابات": ["التحقق من الرصيد", "فتح حساب جديد"],
"القروض": ["شروط الحصول على قرض", "أسعار الفائدة"],
"الفروع": ["فروع البنك", "ساعات العمل"],
"الدعم": ["الإبلاغ عن فقدان البطاقة", "الاتصال بالبنك"]
}
# Omdurman National Bank-specific interface
with gr.Blocks(css=".gradio-container {direction: rtl;}") as demo:
gr.Markdown("# <center>بنك أم درمان الوطني - المساعد المصرفي</center>")
with gr.Tab("المحادثة"):
gr.Markdown("## اختر أحد الخيارات التالية أو اكتب سؤالك:")
# Chat interface
chatbot = gr.ChatInterface(
respond,
examples=[
"كيف يمكنني التحقق من رصيدي؟", # Check balance
"أريد الإبلاغ عن فقدان بطاقتي", # Report lost card
"ما هي شروط الحصول على قرض؟", # Loan eligibility
"ما هي ساعات العمل؟", # Working hours
"أين يوجد أقرب فرع؟" # Branch locations
]
)
with gr.Tab("القائمة الرئيسية"):
gr.Markdown("## القائمة الرئيسية")
for category, options in main_menu.items():
with gr.Accordion(category):
for option in options:
gr.Button(option).click(
fn=lambda opt=option: respond(opt, []),
outputs=chatbot.chatbot
)
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=True # Enable public link
) |