import gradio as gr import logging from difflib import get_close_matches # For fuzzy matching # 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 أو عبر البريد الإلكتروني info@onb.sd." } # Map keywords to responses KEYWORD_TO_RESPONSE = { "رصيد": "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 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 "main_menu" # Find all matching keywords using fuzzy matching matches = [] for keyword in KEYWORD_TO_RESPONSE.keys(): if keyword in message: matches.append(keyword) else: # Use fuzzy matching for similar words close_matches = get_close_matches(keyword, message.split(), n=1, cutoff=0.6) if close_matches: matches.append(keyword) # If multiple matches are found, return all responses if matches: responses = [] for match in matches: key = KEYWORD_TO_RESPONSE.get(match) if key: responses.append(ONB_GUIDELINES.get(key)) return "\n\n".join(responses) # Default response if no keyword is matched return "عذرًا، لم أفهم سؤالك. الرجاء إعادة الصياغة أو كتابة 'القائمة' للعودة إلى القائمة الرئيسية." # Main menu with submenus main_menu = { "الحسابات": ["التحقق من الرصيد", "فتح حساب جديد"], "القروض": ["شروط الحصول على قرض", "أسعار الفائدة"], "الفروع": ["فروع البنك", "ساعات العمل"], "الدعم": ["الإبلاغ عن فقدان البطاقة", "الاتصال بالبنك"] } # Omdurman National Bank-specific interface with gr.Blocks(css=".gradio-container {direction: rtl;}") as demo: gr.Markdown("#
بنك أم درمان الوطني - المساعد المصرفي
") 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 )