Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
|
| 3 |
+
class ChatBot:
|
| 4 |
+
def __init__(self, name="PyBot"):
|
| 5 |
+
self.name = name
|
| 6 |
+
self.memory = {}
|
| 7 |
+
self.responses = {
|
| 8 |
+
"greeting": [
|
| 9 |
+
"Hey there! 👋",
|
| 10 |
+
"Hello, how’s your day going?",
|
| 11 |
+
"Hi! What’s on your mind?"
|
| 12 |
+
],
|
| 13 |
+
"farewell": [
|
| 14 |
+
"Goodbye 👋",
|
| 15 |
+
"Catch you later!",
|
| 16 |
+
"See you soon!"
|
| 17 |
+
],
|
| 18 |
+
"default": [
|
| 19 |
+
"Interesting… tell me more.",
|
| 20 |
+
"Hmm, I see. Go on.",
|
| 21 |
+
"That’s something to think about."
|
| 22 |
+
]
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
def understand(self, user_input):
|
| 26 |
+
"""Simple NLP-style intent recognition"""
|
| 27 |
+
text = user_input.lower()
|
| 28 |
+
if any(word in text for word in ["hi", "hello", "hey"]):
|
| 29 |
+
return "greeting"
|
| 30 |
+
elif any(word in text for word in ["bye", "goodnight", "see you"]):
|
| 31 |
+
return "farewell"
|
| 32 |
+
else:
|
| 33 |
+
return "default"
|
| 34 |
+
|
| 35 |
+
def respond(self, user_input):
|
| 36 |
+
intent = self.understand(user_input)
|
| 37 |
+
return random.choice(self.responses[intent])
|
| 38 |
+
|
| 39 |
+
def chat(self):
|
| 40 |
+
print(f"{self.name}: Hi! I'm {self.name}. Type 'quit' to end.")
|
| 41 |
+
while True:
|
| 42 |
+
user_input = input("You: ")
|
| 43 |
+
if user_input.lower() in ["quit", "exit"]:
|
| 44 |
+
print(f"{self.name}: It was nice chatting with you. Bye!")
|
| 45 |
+
break
|
| 46 |
+
reply = self.respond(user_input)
|
| 47 |
+
print(f"{self.name}: {reply}")
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
bot = ChatBot("Sophia")
|
| 52 |
+
bot.chat()
|