File size: 10,968 Bytes
8524303
2519b2c
 
8524303
2519b2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78eab1a
2519b2c
 
 
78eab1a
2519b2c
 
 
78eab1a
2519b2c
 
 
 
 
78eab1a
2519b2c
 
 
 
 
ea3ae00
2519b2c
 
 
 
 
78eab1a
2519b2c
 
 
 
 
78eab1a
2519b2c
 
78eab1a
2519b2c
 
 
 
 
 
 
78eab1a
2519b2c
 
 
 
 
 
 
78eab1a
2519b2c
 
 
 
 
 
 
 
 
78eab1a
2519b2c
 
 
 
 
 
44617e0
2519b2c
 
44617e0
2519b2c
 
78eab1a
 
 
2519b2c
 
44617e0
 
 
2519b2c
44617e0
 
2519b2c
44617e0
 
 
 
6388e9c
44617e0
 
 
2519b2c
 
947e3a3
44617e0
 
6388e9c
2519b2c
78eab1a
947e3a3
2519b2c
6388e9c
 
 
2519b2c
 
 
78eab1a
6388e9c
 
 
 
 
 
 
44617e0
 
2519b2c
6388e9c
44617e0
2519b2c
 
 
 
 
 
 
 
 
44617e0
 
2519b2c
6388e9c
44617e0
 
 
78eab1a
 
44617e0
 
 
2519b2c
 
44617e0
 
2519b2c
 
44617e0
2519b2c
 
44617e0
2519b2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44617e0
78eab1a
2519b2c
 
 
 
 
44617e0
2519b2c
 
 
 
 
44617e0
2519b2c
 
 
 
78eab1a
2519b2c
6388e9c
2519b2c
 
 
 
 
44617e0
 
2519b2c
8524303
44617e0
 
 
78eab1a
44617e0
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
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("""
    <div style="text-align: center; padding: 20px;">
        <h1>🇳🇵 नेपाली च्याटबोट</h1>
        <h2>Simple Nepali Chatbot</h2>
        <p style="font-size: 18px;">
            <strong>नेपालीमा वा अंग्रेजीमा कुराकानी गर्नुहोस्!</strong><br>
            <em>Chat in Nepali or English!</em>
        </p>
    </div>
    """)
    
    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("""
    <div style="text-align: center; margin-top: 20px; padding: 20px; background: #16213e; border-radius: 10px; color: #ffffff;">
        <h3>📝 About this Chatbot</h3>
        <p>This is a simple rule-based chatbot that responds in both Nepali and English.</p>
        <p><strong>यो एक सरल नियम-आधारित च्याटबोट हो जसले नेपाली र अंग्रेजी दुवैमा जवाफ दिन्छ।</strong></p>
        <p><em>No heavy AI models required - works instantly! ⚡</em></p>
    </div>
    """)

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