import gradio as gr import random import re # Simple rule-based chatbot that doesn't require any heavy dependencies class SimpleNepaliChatbot: def __init__(self): # Simple conversation patterns self.english_responses = { "greeting": [ "Hello! How are you today?", "Hi there! Nice to meet you!", "Hey! How can I help you?", "Hello! What would you like to talk about?" ], "how_are_you": [ "I'm doing great, thank you for asking! How about you?", "I'm fine, thanks! How are things with you?", "I'm well! What brings you here today?" ], "name": [ "I'm your friendly Nepali chatbot! You can call me Bot.", "I'm a chatbot designed to chat in Nepali and English!", "My name is NepaliBot. Nice to meet you!" ], "goodbye": [ "Goodbye! Have a wonderful day!", "See you later! Take care!", "Bye! It was nice chatting with you!" ], "default": [ "That's interesting! Tell me more about it.", "I see! What else would you like to talk about?", "Thanks for sharing! How does that make you feel?", "That's a good point. What do you think about it?", "I understand. Can you elaborate on that?" ] } self.nepali_responses = { "greeting": [ "नमस्ते! तपाईं कस्तो हुनुहुन्छ?", "नमस्कार! तपाईंलाई भेटेर खुशी लाग्यो!", "हाल कस्तो छ? म कसरी मद्दत गर्न सक्छु?", "नमस्ते! के कुरा गर्न चाहनुहुन्छ?" ], "how_are_you": [ "म सञ्चै छु, धन्यवाद! तपाईं कस्तो हुनुहुन्छ?", "म राम्रो छु! तपाईंको हाल कस्तो छ?", "म ठिकै छु! आज के गर्दै हुनुहुन्छ?" ], "name": [ "म तपाईंको नेपाली च्याटबोट हुँ! मलाई बोट भन्न सक्नुहुन्छ।", "म एक च्याटबोट हुँ जसले नेपाली र अंग्रेजी दुवैमा कुरा गर्छु!", "मेरो नाम नेपालीबोट हो। तपाईंलाई भेटेर खुशी लाग्यो!" ], "goodbye": [ "नमस्कार! राम्रो दिन बिताउनुहोस्!", "फेरि भेटौंला! ख्याल गर्नुहोस्!", "बाइ बाइ! तपाईंसँग कुरा गर्न राम्रो लाग्यो!" ], "default": [ "त्यो रोचक छ! मलाई थप बताउनुहोस्।", "बुझे! अरू के कुरा गर्न चाहनुहुन्छ?", "धन्यवाद साझा गर्नुभएकोमा! यसले तपाईंलाई कस्तो लाग्छ?", "राम्रो कुरा हो। तपाईं के सोच्नुहुन्छ?", "बुझें। यसबारे अलि विस्तारमा भन्न सक्नुहुन्छ?" ] } def is_nepali_text(self, text): """Check if text contains Devanagari script""" return bool(re.search(r'[\u0900-\u097F]', text)) def get_response_type(self, message): """Determine the type of response needed""" message_lower = message.lower() # Greeting patterns greeting_patterns = [ r'\b(hello|hi|hey|namaste|namaskar)\b', r'नमस्ते|नमस्कार|हाल|हलो' ] # How are you patterns how_are_you_patterns = [ r'\b(how are you|how do you do|kasto cha|kasto hunuhuncha)\b', r'कस्तो छ|कस्तो हुनुहुन्छ|ठिक छ|राम्रो छ' ] # Name patterns name_patterns = [ r'\b(what is your name|who are you|tapai ko naam|naam ke ho)\b', r'तपाईको नाम|नाम के हो|को हो|को हुनुहुन्छ' ] # Goodbye patterns goodbye_patterns = [ r'\b(bye|goodbye|see you|alvida|jaau)\b', r'बाइ|गुड बाइ|जाउँ|जान्छु|अलविदा' ] # Check patterns all_patterns = greeting_patterns + how_are_you_patterns + name_patterns + goodbye_patterns for pattern in greeting_patterns: if re.search(pattern, message_lower): return "greeting" for pattern in how_are_you_patterns: if re.search(pattern, message_lower): return "how_are_you" for pattern in name_patterns: if re.search(pattern, message_lower): return "name" for pattern in goodbye_patterns: if re.search(pattern, message_lower): return "goodbye" return "default" def generate_response(self, message): """Generate appropriate response""" if not message.strip(): return "Please say something!" is_nepali = self.is_nepali_text(message) response_type = self.get_response_type(message) if is_nepali: responses = self.nepali_responses.get(response_type, self.nepali_responses["default"]) else: responses = self.english_responses.get(response_type, self.english_responses["default"]) return random.choice(responses) # Initialize chatbot chatbot = SimpleNepaliChatbot() def chat_function(message, history): """Main chat interface function""" if not message.strip(): return history, "" # Generate response bot_response = chatbot.generate_response(message) # Add to history history.append([message, bot_response]) return history, "" # Custom CSS css = """ .gradio-container { max-width: 800px !important; margin: auto !important; background-color: #1a1a2e !important; /* Dark background for contrast */ } .message.user { background-color: #e3f2fd !important; border-radius: 15px !important; padding: 10px !important; color: #1e1e1e !important; /* Dark text for user messages */ } .message.bot { background-color: #d1d1d1 !important; /* Slightly darker gray for bot messages */ border-radius: 15px !important; padding: 10px !important; color: #1e1e1e !important; /* Dark text for bot messages */ } .chatbot .message { color: #1e1e1e !important; /* Ensure all messages use dark text */ } .input-container { background: linear-gradient(90deg, #667eea 0%, #764ba2 100%) !important; border-radius: 25px !important; } .input-container input { color: #ffffff !important; /* White text for input field */ background: transparent !important; } .gradio-chatbot { background-color: #16213e !important; /* Darker chat area background */ } """ # Create the Gradio interface with gr.Blocks(css=css, title="Simple Nepali Chatbot", theme=gr.themes.Default()) as demo: gr.HTML("""

🇳🇵 नेपाली च्याटबोट

Simple Nepali Chatbot

नेपालीमा वा अंग्रेजीमा कुराकानी गर्नुहोस्!
Chat in Nepali or English!

""") chatbot_ui = gr.Chatbot( value=None, # Removed initial message to avoid pre-rendered style issues height=400, show_label=False, container=True, bubble_full_width=False, show_copy_button=True ) with gr.Row(): msg_input = gr.Textbox( placeholder="यहाँ लेख्नुहोस् / Type here...", show_label=False, scale=4, lines=1, container=False ) send_btn = gr.Button("📤 Send", scale=1, variant="primary") clear_btn = gr.Button("🗑️ Clear", scale=1, variant="secondary") # Example conversations with gr.Row(): gr.Examples( examples=[ ["नमस्ते!"], ["Hello!"], ["तपाईंको नाम के हो?"], ["How are you?"], ["What is your name?"], ["कस्तो छ?"], ["Thank you!"], ["धन्यवाद!"] ], inputs=msg_input, label="🔄 Try these examples / यी उदाहरणहरू प्रयास गर्नुहोस्" ) # Event handlers msg_input.submit( chat_function, inputs=[msg_input, chatbot_ui], outputs=[chatbot_ui, msg_input] ) send_btn.click( chat_function, inputs=[msg_input, chatbot_ui], outputs=[chatbot_ui, msg_input] ) clear_btn.click( lambda: ([], ""), outputs=[chatbot_ui, msg_input] ) gr.HTML("""

📝 About this Chatbot

This is a simple rule-based chatbot that responds in both Nepali and English.

यो एक सरल नियम-आधारित च्याटबोट हो जसले नेपाली र अंग्रेजी दुवैमा जवाफ दिन्छ।

No heavy AI models required - works instantly! ⚡

""") # Launch the app if __name__ == "__main__": demo.launch( server_name="0.0.0.0", server_port=7860, share=False )