Spaces:
Runtime error
Runtime error
File size: 3,283 Bytes
ef519b9 2b8458f ef519b9 2b8458f ef519b9 2b8458f ef519b9 2b8458f ef519b9 2b8458f ef519b9 2b8458f ef519b9 2b8458f ef519b9 2b8458f ef519b9 2b8458f ef519b9 2b8458f ef519b9 2b8458f ef519b9 2b8458f ef519b9 2b8458f ef519b9 2b8458f ef519b9 2b8458f ef519b9 2b8458f ef519b9 2b8458f ef519b9 2b8458f d23bc15 2b8458f d23bc15 ef519b9 2b8458f | 1 2 3 4 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | import gradio as gr
import random
from datetime import datetime
def chat_response(message, history):
"""Simple chatbot that responds to user messages"""
message_lower = message.lower().strip()
# Greetings
if any(word in message_lower for word in ['hello', 'hi', 'hey', 'greetings']):
responses = [
"Hello! How can I help you today?",
"Hi there! What can I do for you?",
"Hey! Great to see you. What's on your mind?",
]
return random.choice(responses)
# How are you
elif 'how are you' in message_lower or 'how are u' in message_lower:
return "I'm doing great, thank you for asking! How can I help you?"
# Name
elif 'your name' in message_lower or 'who are you' in message_lower:
return "I'm a friendly chatbot! You can call me ChatBot. What's your name?"
# Time
elif 'time' in message_lower:
current_time = datetime.now().strftime("%I:%M %p")
return f"The current time is {current_time}."
# Date
elif 'date' in message_lower or 'today' in message_lower:
current_date = datetime.now().strftime("%B %d, %Y")
return f"Today's date is {current_date}."
# Math
elif 'what is' in message_lower or '+' in message or '-' in message or '*' in message or '/' in message:
try:
import re
match = re.search(r'(\d+\.?\d*)\s*([\+\-\*\/])\s*(\d+\.?\d*)', message)
if match:
num1, op, num2 = match.groups()
num1, num2 = float(num1), float(num2)
if op == '+':
result = num1 + num2
elif op == '-':
result = num1 - num2
elif op == '*':
result = num1 * num2
elif op == '/':
result = num1 / num2 if num2 != 0 else "Can't divide by zero!"
return f"The answer is {result}"
except:
pass
# Jokes
elif 'joke' in message_lower:
jokes = [
"Why don't scientists trust atoms? Because they make up everything! π",
"What do you call a bear with no teeth? A gummy bear! π»",
"Why did the scarecrow win an award? He was outstanding in his field! πΎ",
]
return random.choice(jokes)
# Goodbye
elif any(word in message_lower for word in ['bye', 'goodbye']):
return "Goodbye! Have a wonderful day! π"
# Thanks
elif 'thank' in message_lower:
return "You're welcome! Happy to help! π"
# Default
else:
responses = [
"That's interesting! Can you tell me more?",
"I'm not sure about that. Try asking me about the time, a joke, or some math!",
"Hmm, I'm still learning. What else would you like to know?",
]
return random.choice(responses)
# Create simple chat interface
demo = gr.ChatInterface(
fn=chat_response,
title="π¬ My ChatBot",
description="Try saying hello, asking for the time, requesting a joke, or doing some math!",
examples=["Hello!", "What time is it?", "Tell me a joke", "What is 15 + 27?"]
)
demo.launch()
|