File size: 8,438 Bytes
823c3f3
b7008f6
 
823c3f3
b7008f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import random
import re

# Configure page
st.set_page_config(
    page_title="Nepali Chatbot",
    page_icon="🇳🇵",
    layout="centered"
)

class SimpleNepaliChatbot:
    def __init__(self):
        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!"
            ],
            "nepal": [
                "Nepal is a beautiful country in the Himalayas!",
                "I love talking about Nepal! What would you like to know?",
                "Nepal has amazing mountains, culture, and people!"
            ],
            "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": [
                "नमस्कार! राम्रो दिन बिताउनुहोस्!",
                "फेरि भेटौंला! ख्याल गर्नुहोस्!",
                "बाइ बाइ! तपाईंसँग कुरा गर्न राम्रो लाग्यो!"
            ],
            "nepal": [
                "नेपाल हिमालयको एक सुन्दर देश हो!",
                "मलाई नेपालको बारेमा कुरा गर्न मन पर्छ! के जान्न चाहनुहुन्छ?",
                "नेपालमा अद्भुत पहाडहरू, संस्कृति र मानिसहरू छन्!"
            ],
            "default": [
                "त्यो रोचक छ! मलाई थप बताउनुहोस्।",
                "बुझे! अरू के कुरा गर्न चाहनुहुन्छ?",
                "धन्यवाद साझा गर्नुभएकोमा! यसले तपाईंलाई कस्तो लाग्छ?",
                "राम्रो कुरा हो। तपाईं के सोच्नुहुन्छ?",
                "बुझें। यसबारे अलि विस्तारमा भन्न सक्नुहुन्छ?"
            ]
        }

    def is_nepali_text(self, text):
        return bool(re.search(r'[\u0900-\u097F]', text))

    def get_response_type(self, message):
        message_lower = message.lower()
        
        # Pattern matching
        if re.search(r'\b(hello|hi|hey|namaste|namaskar)\b|नमस्ते|नमस्कार|हाल|हलो', message_lower):
            return "greeting"
        elif re.search(r'\b(how are you|kasto cha)\b|कस्तो छ|कस्तो हुनुहुन्छ', message_lower):
            return "how_are_you"
        elif re.search(r'\b(name|naam|who)\b|नाम|को हो', message_lower):
            return "name"
        elif re.search(r'\b(bye|goodbye|jaau)\b|बाइ|जाउँ', message_lower):
            return "goodbye"
        elif re.search(r'\b(nepal|nepali)\b|नेपाल', message_lower):
            return "nepal"
        else:
            return "default"

    def generate_response(self, message):
        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
if 'chatbot' not in st.session_state:
    st.session_state.chatbot = SimpleNepaliChatbot()

if 'messages' not in st.session_state:
    st.session_state.messages = [
        {"role": "assistant", "content": "नमस्ते! Hello! I'm your Nepali chatbot. तपाईंलाई कसरी मद्दत गर्न सक्छु?"}
    ]

# UI
st.title("🇳🇵 नेपाली च्याटबोट")
st.markdown("**Simple Nepali Chatbot - नेपालीमा वा अंग्रेजीमा कुराकानी गर्नुहोस्!**")

# Display chat history
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.write(message["content"])

# Input
if prompt := st.chat_input("यहाँ लेख्नुहोस् / Type here..."):
    # Add user message
    st.session_state.messages.append({"role": "user", "content": prompt})
    with st.chat_message("user"):
        st.write(prompt)
    
    # Generate bot response
    response = st.session_state.chatbot.generate_response(prompt)
    st.session_state.messages.append({"role": "assistant", "content": response})
    with st.chat_message("assistant"):
        st.write(response)

# Sidebar with examples
st.sidebar.header("🔄 Try these examples:")
examples = [
    "नमस्ते!",
    "Hello!",
    "तपाईंको नाम के हो?",
    "How are you?",
    "कस्तो छ?",
    "Tell me about Nepal",
    "धन्यवाद!"
]

for example in examples:
    if st.sidebar.button(example):
        st.session_state.messages.append({"role": "user", "content": example})
        response = st.session_state.chatbot.generate_response(example)
        st.session_state.messages.append({"role": "assistant", "content": response})
        st.rerun()

if st.sidebar.button("🗑️ Clear Chat"):
    st.session_state.messages = [
        {"role": "assistant", "content": "नमस्ते! Hello! I'm your Nepali chatbot. तपाईंलाई कसरी मद्दत गर्न सक्छु?"}
    ]
    st.rerun()

st.sidebar.markdown("---")
st.sidebar.info("This chatbot uses simple pattern matching to respond in Nepali and English.")