Spaces:
Build error
Build error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +175 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,177 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import random
|
| 3 |
+
import re
|
| 4 |
|
| 5 |
+
# Configure page
|
| 6 |
+
st.set_page_config(
|
| 7 |
+
page_title="Nepali Chatbot",
|
| 8 |
+
page_icon="🇳🇵",
|
| 9 |
+
layout="centered"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
class SimpleNepaliChatbot:
|
| 13 |
+
def __init__(self):
|
| 14 |
+
self.english_responses = {
|
| 15 |
+
"greeting": [
|
| 16 |
+
"Hello! How are you today?",
|
| 17 |
+
"Hi there! Nice to meet you!",
|
| 18 |
+
"Hey! How can I help you?",
|
| 19 |
+
"Hello! What would you like to talk about?"
|
| 20 |
+
],
|
| 21 |
+
"how_are_you": [
|
| 22 |
+
"I'm doing great, thank you for asking! How about you?",
|
| 23 |
+
"I'm fine, thanks! How are things with you?",
|
| 24 |
+
"I'm well! What brings you here today?"
|
| 25 |
+
],
|
| 26 |
+
"name": [
|
| 27 |
+
"I'm your friendly Nepali chatbot! You can call me Bot.",
|
| 28 |
+
"I'm a chatbot designed to chat in Nepali and English!",
|
| 29 |
+
"My name is NepaliBot. Nice to meet you!"
|
| 30 |
+
],
|
| 31 |
+
"goodbye": [
|
| 32 |
+
"Goodbye! Have a wonderful day!",
|
| 33 |
+
"See you later! Take care!",
|
| 34 |
+
"Bye! It was nice chatting with you!"
|
| 35 |
+
],
|
| 36 |
+
"nepal": [
|
| 37 |
+
"Nepal is a beautiful country in the Himalayas!",
|
| 38 |
+
"I love talking about Nepal! What would you like to know?",
|
| 39 |
+
"Nepal has amazing mountains, culture, and people!"
|
| 40 |
+
],
|
| 41 |
+
"default": [
|
| 42 |
+
"That's interesting! Tell me more about it.",
|
| 43 |
+
"I see! What else would you like to talk about?",
|
| 44 |
+
"Thanks for sharing! How does that make you feel?",
|
| 45 |
+
"That's a good point. What do you think about it?",
|
| 46 |
+
"I understand. Can you elaborate on that?"
|
| 47 |
+
]
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
self.nepali_responses = {
|
| 51 |
+
"greeting": [
|
| 52 |
+
"नमस्ते! तपाईं कस्तो हुनुहुन्छ?",
|
| 53 |
+
"नमस्कार! तपाईंलाई भेटेर खुशी लाग्यो!",
|
| 54 |
+
"हाल कस्तो छ? म कसरी मद्दत गर्न सक्छु?",
|
| 55 |
+
"नमस्ते! के कुरा गर्न चाहनुहुन्छ?"
|
| 56 |
+
],
|
| 57 |
+
"how_are_you": [
|
| 58 |
+
"म सञ्चै छु, धन्यवाद! तपाईं कस्तो हुनुहुन्छ?",
|
| 59 |
+
"म राम्रो छु! तपाईंको हाल कस्तो छ?",
|
| 60 |
+
"म ठिकै छु! आज के गर्दै हुनुहुन्छ?"
|
| 61 |
+
],
|
| 62 |
+
"name": [
|
| 63 |
+
"म तपाईंको नेपाली च्याटबोट हुँ! मलाई बोट भन्न सक्नुहुन्छ।",
|
| 64 |
+
"म एक च्याटबोट हुँ जसले नेपाली र अंग्रेजी दुवैमा कुरा गर्छु!",
|
| 65 |
+
"मेरो नाम नेपालीबोट हो। तपाईंलाई भेटेर खुशी लाग्यो!"
|
| 66 |
+
],
|
| 67 |
+
"goodbye": [
|
| 68 |
+
"नमस्कार! राम्रो दिन बिताउनुहोस्!",
|
| 69 |
+
"फेरि भेटौंला! ख्याल गर्नुहोस्!",
|
| 70 |
+
"बाइ बाइ! तपाईंसँग कुरा गर्न राम्रो लाग्यो!"
|
| 71 |
+
],
|
| 72 |
+
"nepal": [
|
| 73 |
+
"नेपाल हिमालयको एक सुन्दर देश हो!",
|
| 74 |
+
"मलाई नेपालको बारेमा कुरा गर्न मन पर्छ! के जान्न चा���नुहुन्छ?",
|
| 75 |
+
"नेपालमा अद्भुत पहाडहरू, संस्कृति र मानिसहरू छन्!"
|
| 76 |
+
],
|
| 77 |
+
"default": [
|
| 78 |
+
"त्यो रोचक छ! मलाई थप बताउनुहोस्।",
|
| 79 |
+
"बुझे! अरू के कुरा गर्न चाहनुहुन्छ?",
|
| 80 |
+
"धन्यवाद साझा गर्नुभएकोमा! यसले तपाईंलाई कस्तो लाग्छ?",
|
| 81 |
+
"राम्रो कुरा हो। तपाईं के सोच्नुहुन्छ?",
|
| 82 |
+
"बुझें। यसबारे अलि विस्तारमा भन्न सक्नुहुन्छ?"
|
| 83 |
+
]
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
def is_nepali_text(self, text):
|
| 87 |
+
return bool(re.search(r'[\u0900-\u097F]', text))
|
| 88 |
+
|
| 89 |
+
def get_response_type(self, message):
|
| 90 |
+
message_lower = message.lower()
|
| 91 |
+
|
| 92 |
+
# Pattern matching
|
| 93 |
+
if re.search(r'\b(hello|hi|hey|namaste|namaskar)\b|नमस्ते|नमस्कार|हाल|हलो', message_lower):
|
| 94 |
+
return "greeting"
|
| 95 |
+
elif re.search(r'\b(how are you|kasto cha)\b|कस्तो छ|कस्तो हुनुहुन्छ', message_lower):
|
| 96 |
+
return "how_are_you"
|
| 97 |
+
elif re.search(r'\b(name|naam|who)\b|नाम|को हो', message_lower):
|
| 98 |
+
return "name"
|
| 99 |
+
elif re.search(r'\b(bye|goodbye|jaau)\b|बाइ|जाउँ', message_lower):
|
| 100 |
+
return "goodbye"
|
| 101 |
+
elif re.search(r'\b(nepal|nepali)\b|नेपाल', message_lower):
|
| 102 |
+
return "nepal"
|
| 103 |
+
else:
|
| 104 |
+
return "default"
|
| 105 |
+
|
| 106 |
+
def generate_response(self, message):
|
| 107 |
+
if not message.strip():
|
| 108 |
+
return "Please say something!"
|
| 109 |
+
|
| 110 |
+
is_nepali = self.is_nepali_text(message)
|
| 111 |
+
response_type = self.get_response_type(message)
|
| 112 |
+
|
| 113 |
+
if is_nepali:
|
| 114 |
+
responses = self.nepali_responses.get(response_type, self.nepali_responses["default"])
|
| 115 |
+
else:
|
| 116 |
+
responses = self.english_responses.get(response_type, self.english_responses["default"])
|
| 117 |
+
|
| 118 |
+
return random.choice(responses)
|
| 119 |
+
|
| 120 |
+
# Initialize chatbot
|
| 121 |
+
if 'chatbot' not in st.session_state:
|
| 122 |
+
st.session_state.chatbot = SimpleNepaliChatbot()
|
| 123 |
+
|
| 124 |
+
if 'messages' not in st.session_state:
|
| 125 |
+
st.session_state.messages = [
|
| 126 |
+
{"role": "assistant", "content": "नमस्ते! Hello! I'm your Nepali chatbot. तपाईंलाई कसरी मद्दत गर्न सक्छु?"}
|
| 127 |
+
]
|
| 128 |
+
|
| 129 |
+
# UI
|
| 130 |
+
st.title("🇳🇵 नेपाली च्याटबोट")
|
| 131 |
+
st.markdown("**Simple Nepali Chatbot - नेपालीमा वा अंग्रेजीमा कुराकानी गर्नुहोस्!**")
|
| 132 |
+
|
| 133 |
+
# Display chat history
|
| 134 |
+
for message in st.session_state.messages:
|
| 135 |
+
with st.chat_message(message["role"]):
|
| 136 |
+
st.write(message["content"])
|
| 137 |
+
|
| 138 |
+
# Input
|
| 139 |
+
if prompt := st.chat_input("यहाँ लेख्नुहोस् / Type here..."):
|
| 140 |
+
# Add user message
|
| 141 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 142 |
+
with st.chat_message("user"):
|
| 143 |
+
st.write(prompt)
|
| 144 |
+
|
| 145 |
+
# Generate bot response
|
| 146 |
+
response = st.session_state.chatbot.generate_response(prompt)
|
| 147 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 148 |
+
with st.chat_message("assistant"):
|
| 149 |
+
st.write(response)
|
| 150 |
+
|
| 151 |
+
# Sidebar with examples
|
| 152 |
+
st.sidebar.header("🔄 Try these examples:")
|
| 153 |
+
examples = [
|
| 154 |
+
"नमस्ते!",
|
| 155 |
+
"Hello!",
|
| 156 |
+
"तपाईंको नाम के हो?",
|
| 157 |
+
"How are you?",
|
| 158 |
+
"कस्तो छ?",
|
| 159 |
+
"Tell me about Nepal",
|
| 160 |
+
"धन्यवाद!"
|
| 161 |
+
]
|
| 162 |
+
|
| 163 |
+
for example in examples:
|
| 164 |
+
if st.sidebar.button(example):
|
| 165 |
+
st.session_state.messages.append({"role": "user", "content": example})
|
| 166 |
+
response = st.session_state.chatbot.generate_response(example)
|
| 167 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 168 |
+
st.rerun()
|
| 169 |
+
|
| 170 |
+
if st.sidebar.button("🗑️ Clear Chat"):
|
| 171 |
+
st.session_state.messages = [
|
| 172 |
+
{"role": "assistant", "content": "नमस्ते! Hello! I'm your Nepali chatbot. तपाईंलाई कसरी मद्दत गर्न सक्छु?"}
|
| 173 |
+
]
|
| 174 |
+
st.rerun()
|
| 175 |
+
|
| 176 |
+
st.sidebar.markdown("---")
|
| 177 |
+
st.sidebar.info("This chatbot uses simple pattern matching to respond in Nepali and English.")
|