xievic9780 commited on
Commit
2b8458f
Β·
1 Parent(s): f48588a

Simplify chatbot code

Browse files
Files changed (1) hide show
  1. 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 simple_chatbot(message: str, history) -> str:
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 any(phrase in message_lower for phrase in ['how are you', 'how are u', 'hows it going', "how's it going"]):
23
- responses = [
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 questions
32
- elif any(phrase in message_lower for phrase in ['what is your name', 'your name', 'who are you']):
33
- return "I'm a friendly chatbot created to assist you! You can call me ChatBot. What's your name?"
34
 
35
  # Time
36
- elif 'time' in message_lower and any(word in message_lower for word in ['what', 'current', 'now']):
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 and any(word in message_lower for word in ['what', 'current', 'today']):
42
  current_date = datetime.now().strftime("%B %d, %Y")
43
  return f"Today's date is {current_date}."
44
 
45
- # Help
46
- elif any(word in message_lower for word in ['help', 'assist', 'support']):
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
- math_pattern = r'(\d+\.?\d*)\s*([\+\-\*\/])\s*(\d+\.?\d*)'
59
- match = re.search(math_pattern, message)
60
  if match:
61
- num1, operator, num2 = match.groups()
62
  num1, num2 = float(num1), float(num2)
63
 
64
- if operator == '+':
65
  result = num1 + num2
66
- elif operator == '-':
67
  result = num1 - num2
68
- elif operator == '*':
69
  result = num1 * num2
70
- elif operator == '/':
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 or 'funny' 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', 'see you', 'later', 'exit', 'quit']):
93
- responses = [
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
- # Thank you
102
- elif any(word in message_lower for word in ['thank', 'thanks', 'thx']):
103
- responses = [
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 response
112
  else:
113
  responses = [
114
- "That's interesting! Tell me more.",
115
- "I'm not sure I understand. Can you rephrase that?",
116
- "Hmm, I'm still learning. Can you ask me something else?",
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 the Gradio ChatInterface (simpler and works better with Gradio 5)
123
  demo = gr.ChatInterface(
124
- fn=simple_chatbot,
125
- title="πŸ’¬ Simple ChatBot",
126
- description="A friendly chatbot that can chat, do math, tell jokes, and more!",
127
- examples=["Hello!", "What time is it?", "Tell me a joke", "What is 25 + 17?", "How are you?"],
128
- theme=gr.themes.Soft()
129
  )
130
 
131
- if __name__ == "__main__":
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()