Spaces:
Runtime error
Runtime error
xievic9780 commited on
Commit Β·
2b8458f
1
Parent(s): f48588a
Simplify chatbot code
Browse files- chatbot.py +34 -74
chatbot.py
CHANGED
|
@@ -2,10 +2,8 @@ import gradio as gr
|
|
| 2 |
import random
|
| 3 |
from datetime import datetime
|
| 4 |
|
| 5 |
-
def
|
| 6 |
-
"""
|
| 7 |
-
A simple rule-based chatbot that responds to common queries.
|
| 8 |
-
"""
|
| 9 |
message_lower = message.lower().strip()
|
| 10 |
|
| 11 |
# Greetings
|
|
@@ -14,119 +12,81 @@ def simple_chatbot(message: str, history) -> str:
|
|
| 14 |
"Hello! How can I help you today?",
|
| 15 |
"Hi there! What can I do for you?",
|
| 16 |
"Hey! Great to see you. What's on your mind?",
|
| 17 |
-
"Greetings! How may I assist you?"
|
| 18 |
]
|
| 19 |
return random.choice(responses)
|
| 20 |
|
| 21 |
# How are you
|
| 22 |
-
elif
|
| 23 |
-
|
| 24 |
-
"I'm doing great, thank you for asking! How about you?",
|
| 25 |
-
"I'm functioning perfectly! How can I help you today?",
|
| 26 |
-
"I'm excellent! What brings you here?",
|
| 27 |
-
"I'm doing well! What about yourself?"
|
| 28 |
-
]
|
| 29 |
-
return random.choice(responses)
|
| 30 |
|
| 31 |
-
# Name
|
| 32 |
-
elif
|
| 33 |
-
return "I'm a friendly chatbot
|
| 34 |
|
| 35 |
# Time
|
| 36 |
-
elif 'time' in message_lower
|
| 37 |
current_time = datetime.now().strftime("%I:%M %p")
|
| 38 |
return f"The current time is {current_time}."
|
| 39 |
|
| 40 |
# Date
|
| 41 |
-
elif 'date' in message_lower
|
| 42 |
current_date = datetime.now().strftime("%B %d, %Y")
|
| 43 |
return f"Today's date is {current_date}."
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
elif
|
| 47 |
-
return """I can help you with:
|
| 48 |
-
- General conversation
|
| 49 |
-
- Answering simple questions
|
| 50 |
-
- Telling you the time and date
|
| 51 |
-
- Basic math problems (e.g., "what is 5 + 3")
|
| 52 |
-
- And more! Just ask me anything."""
|
| 53 |
-
|
| 54 |
-
# Math operations
|
| 55 |
-
elif any(op in message_lower for op in ['what is', 'calculate', 'compute']):
|
| 56 |
try:
|
| 57 |
import re
|
| 58 |
-
|
| 59 |
-
match = re.search(math_pattern, message)
|
| 60 |
if match:
|
| 61 |
-
num1,
|
| 62 |
num1, num2 = float(num1), float(num2)
|
| 63 |
|
| 64 |
-
if
|
| 65 |
result = num1 + num2
|
| 66 |
-
elif
|
| 67 |
result = num1 - num2
|
| 68 |
-
elif
|
| 69 |
result = num1 * num2
|
| 70 |
-
elif
|
| 71 |
-
if num2 != 0
|
| 72 |
-
result = num1 / num2
|
| 73 |
-
else:
|
| 74 |
-
return "I can't divide by zero!"
|
| 75 |
|
| 76 |
return f"The answer is {result}"
|
| 77 |
except:
|
| 78 |
pass
|
| 79 |
|
| 80 |
# Jokes
|
| 81 |
-
elif 'joke' in message_lower
|
| 82 |
jokes = [
|
| 83 |
"Why don't scientists trust atoms? Because they make up everything! π",
|
| 84 |
-
"Why did the Python programmer not respond to the chat? Because he was stuck in an infinite loop! π",
|
| 85 |
"What do you call a bear with no teeth? A gummy bear! π»",
|
| 86 |
"Why did the scarecrow win an award? He was outstanding in his field! πΎ",
|
| 87 |
-
"What's a computer's favorite snack? Microchips! π»"
|
| 88 |
]
|
| 89 |
return random.choice(jokes)
|
| 90 |
|
| 91 |
# Goodbye
|
| 92 |
-
elif any(word in message_lower for word in ['bye', 'goodbye'
|
| 93 |
-
|
| 94 |
-
"Goodbye! Have a wonderful day! π",
|
| 95 |
-
"See you later! Take care! π",
|
| 96 |
-
"Bye! Come back anytime!",
|
| 97 |
-
"Farewell! It was nice chatting with you!"
|
| 98 |
-
]
|
| 99 |
-
return random.choice(responses)
|
| 100 |
|
| 101 |
-
#
|
| 102 |
-
elif
|
| 103 |
-
|
| 104 |
-
"You're welcome! Happy to help! π",
|
| 105 |
-
"No problem at all!",
|
| 106 |
-
"My pleasure! Anything else I can help with?",
|
| 107 |
-
"You're very welcome!"
|
| 108 |
-
]
|
| 109 |
-
return random.choice(responses)
|
| 110 |
|
| 111 |
-
# Default
|
| 112 |
else:
|
| 113 |
responses = [
|
| 114 |
-
"That's interesting!
|
| 115 |
-
"I'm not sure
|
| 116 |
-
"Hmm, I'm still learning.
|
| 117 |
-
"I don't have a good answer for that yet. Try asking me about the time, date, or tell me a joke!",
|
| 118 |
-
"That's a good question! Unfortunately, I don't have enough information to answer it properly."
|
| 119 |
]
|
| 120 |
return random.choice(responses)
|
| 121 |
|
| 122 |
-
# Create
|
| 123 |
demo = gr.ChatInterface(
|
| 124 |
-
fn=
|
| 125 |
-
title="π¬
|
| 126 |
-
description="
|
| 127 |
-
examples=["Hello!", "What time is it?", "Tell me a joke", "What is
|
| 128 |
-
theme=gr.themes.Soft()
|
| 129 |
)
|
| 130 |
|
| 131 |
-
|
| 132 |
-
demo.launch()
|
|
|
|
| 2 |
import random
|
| 3 |
from datetime import datetime
|
| 4 |
|
| 5 |
+
def chat_response(message, history):
|
| 6 |
+
"""Simple chatbot that responds to user messages"""
|
|
|
|
|
|
|
| 7 |
message_lower = message.lower().strip()
|
| 8 |
|
| 9 |
# Greetings
|
|
|
|
| 12 |
"Hello! How can I help you today?",
|
| 13 |
"Hi there! What can I do for you?",
|
| 14 |
"Hey! Great to see you. What's on your mind?",
|
|
|
|
| 15 |
]
|
| 16 |
return random.choice(responses)
|
| 17 |
|
| 18 |
# How are you
|
| 19 |
+
elif 'how are you' in message_lower or 'how are u' in message_lower:
|
| 20 |
+
return "I'm doing great, thank you for asking! How can I help you?"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Name
|
| 23 |
+
elif 'your name' in message_lower or 'who are you' in message_lower:
|
| 24 |
+
return "I'm a friendly chatbot! You can call me ChatBot. What's your name?"
|
| 25 |
|
| 26 |
# Time
|
| 27 |
+
elif 'time' in message_lower:
|
| 28 |
current_time = datetime.now().strftime("%I:%M %p")
|
| 29 |
return f"The current time is {current_time}."
|
| 30 |
|
| 31 |
# Date
|
| 32 |
+
elif 'date' in message_lower or 'today' in message_lower:
|
| 33 |
current_date = datetime.now().strftime("%B %d, %Y")
|
| 34 |
return f"Today's date is {current_date}."
|
| 35 |
|
| 36 |
+
# Math
|
| 37 |
+
elif 'what is' in message_lower or '+' in message or '-' in message or '*' in message or '/' in message:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
try:
|
| 39 |
import re
|
| 40 |
+
match = re.search(r'(\d+\.?\d*)\s*([\+\-\*\/])\s*(\d+\.?\d*)', message)
|
|
|
|
| 41 |
if match:
|
| 42 |
+
num1, op, num2 = match.groups()
|
| 43 |
num1, num2 = float(num1), float(num2)
|
| 44 |
|
| 45 |
+
if op == '+':
|
| 46 |
result = num1 + num2
|
| 47 |
+
elif op == '-':
|
| 48 |
result = num1 - num2
|
| 49 |
+
elif op == '*':
|
| 50 |
result = num1 * num2
|
| 51 |
+
elif op == '/':
|
| 52 |
+
result = num1 / num2 if num2 != 0 else "Can't divide by zero!"
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
return f"The answer is {result}"
|
| 55 |
except:
|
| 56 |
pass
|
| 57 |
|
| 58 |
# Jokes
|
| 59 |
+
elif 'joke' in message_lower:
|
| 60 |
jokes = [
|
| 61 |
"Why don't scientists trust atoms? Because they make up everything! π",
|
|
|
|
| 62 |
"What do you call a bear with no teeth? A gummy bear! π»",
|
| 63 |
"Why did the scarecrow win an award? He was outstanding in his field! πΎ",
|
|
|
|
| 64 |
]
|
| 65 |
return random.choice(jokes)
|
| 66 |
|
| 67 |
# Goodbye
|
| 68 |
+
elif any(word in message_lower for word in ['bye', 'goodbye']):
|
| 69 |
+
return "Goodbye! Have a wonderful day! π"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
# Thanks
|
| 72 |
+
elif 'thank' in message_lower:
|
| 73 |
+
return "You're welcome! Happy to help! π"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
# Default
|
| 76 |
else:
|
| 77 |
responses = [
|
| 78 |
+
"That's interesting! Can you tell me more?",
|
| 79 |
+
"I'm not sure about that. Try asking me about the time, a joke, or some math!",
|
| 80 |
+
"Hmm, I'm still learning. What else would you like to know?",
|
|
|
|
|
|
|
| 81 |
]
|
| 82 |
return random.choice(responses)
|
| 83 |
|
| 84 |
+
# Create simple chat interface
|
| 85 |
demo = gr.ChatInterface(
|
| 86 |
+
fn=chat_response,
|
| 87 |
+
title="π¬ My ChatBot",
|
| 88 |
+
description="Try saying hello, asking for the time, requesting a joke, or doing some math!",
|
| 89 |
+
examples=["Hello!", "What time is it?", "Tell me a joke", "What is 15 + 27?"]
|
|
|
|
| 90 |
)
|
| 91 |
|
| 92 |
+
demo.launch()
|
|
|