Spaces:
Runtime error
Runtime error
Download chatbot.py from CapyMaster/mychatbot: direct link, hf CLI and curl.
- Browser
- Download file 3.28 kB
-
https://huggingface.co/spaces/CapyMaster/mychatbot/resolve/main/chatbot.py
- Command line
-
hf download hf://spaces/CapyMaster/mychatbot/chatbot.py
-
curl -L -o chatbot.py https://huggingface.co/spaces/CapyMaster/mychatbot/resolve/main/chatbot.py
3.28 kB
| 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() | |