Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import random
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
# Page config
|
| 6 |
+
st.set_page_config(
|
| 7 |
+
page_title="Nepali Chatbot",
|
| 8 |
+
page_icon="🇳🇵"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
# Simple responses
|
| 12 |
+
nepali_greetings = ["नमस्ते! कस्तो छ?", "नमस्कार! राम्रो छु।", "हाल कस्तो छ?"]
|
| 13 |
+
english_greetings = ["Hello! How are you?", "Hi there!", "Hey! How's it going?"]
|
| 14 |
+
|
| 15 |
+
nepali_responses = [
|
| 16 |
+
"त्यो राम्रो छ!",
|
| 17 |
+
"बुझें। अरू के भन्न चाहनुहुन्छ?",
|
| 18 |
+
"धन्यवाद साझा गर्नुभएकोमा!",
|
| 19 |
+
"त्यो दिलचस्प छ!"
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
english_responses = [
|
| 23 |
+
"That's great!",
|
| 24 |
+
"I understand. What else would you like to say?",
|
| 25 |
+
"Thanks for sharing!",
|
| 26 |
+
"That's interesting!"
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
def is_nepali(text):
|
| 30 |
+
return bool(re.search(r'[\u0900-\u097F]', text))
|
| 31 |
+
|
| 32 |
+
def get_response(message):
|
| 33 |
+
if not message:
|
| 34 |
+
return "Please say something!"
|
| 35 |
+
|
| 36 |
+
message_lower = message.lower()
|
| 37 |
+
|
| 38 |
+
# Check for greetings
|
| 39 |
+
if any(word in message_lower for word in ["hello", "hi", "hey"]) or "नमस्ते" in message or "नमस्कार" in message:
|
| 40 |
+
return random.choice(nepali_greetings if is_nepali(message) else english_greetings)
|
| 41 |
+
|
| 42 |
+
# Regular responses
|
| 43 |
+
if is_nepali(message):
|
| 44 |
+
return random.choice(nepali_responses)
|
| 45 |
+
else:
|
| 46 |
+
return random.choice(english_responses)
|
| 47 |
+
|
| 48 |
+
# Title
|
| 49 |
+
st.title("🇳🇵 Simple Nepali Chatbot")
|
| 50 |
+
st.write("**Chat in Nepali or English! / नेपाली वा अंग्रेजीमा कुरा गर्नुहोस्!**")
|
| 51 |
+
|
| 52 |
+
# Initialize chat history
|
| 53 |
+
if "messages" not in st.session_state:
|
| 54 |
+
st.session_state.messages = []
|
| 55 |
+
|
| 56 |
+
# Display chat messages
|
| 57 |
+
for message in st.session_state.messages:
|
| 58 |
+
with st.chat_message(message["role"]):
|
| 59 |
+
st.write(message["content"])
|
| 60 |
+
|
| 61 |
+
# Chat input
|
| 62 |
+
if prompt := st.chat_input("Type here... / यहाँ लेख्नुहोस्..."):
|
| 63 |
+
# Add user message
|
| 64 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 65 |
+
with st.chat_message("user"):
|
| 66 |
+
st.write(prompt)
|
| 67 |
+
|
| 68 |
+
# Generate response
|
| 69 |
+
response = get_response(prompt)
|
| 70 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 71 |
+
with st.chat_message("assistant"):
|
| 72 |
+
st.write(response)
|
| 73 |
+
|
| 74 |
+
# Sidebar with examples
|
| 75 |
+
with st.sidebar:
|
| 76 |
+
st.header("Examples / उदाहरण")
|
| 77 |
+
|
| 78 |
+
if st.button("नमस्ते!"):
|
| 79 |
+
st.session_state.messages.append({"role": "user", "content": "नमस्ते!"})
|
| 80 |
+
st.session_state.messages.append({"role": "assistant", "content": get_response("नमस्ते!")})
|
| 81 |
+
st.rerun()
|
| 82 |
+
|
| 83 |
+
if st.button("Hello!"):
|
| 84 |
+
st.session_state.messages.append({"role": "user", "content": "Hello!"})
|
| 85 |
+
st.session_state.messages.append({"role": "assistant", "content": get_response("Hello!")})
|
| 86 |
+
st.rerun()
|
| 87 |
+
|
| 88 |
+
if st.button("Clear / सफा गर्नुहोस्"):
|
| 89 |
+
st.session_state.messages = []
|
| 90 |
+
st.rerun()
|