import gradio as gr import random import time class ModernChatbot: def __init__(self): self.user_name = None self.conversation_count = 0 self.last_question = None self.waiting_for_response = False def process_message(self, message, history): """Process user message and return response.""" if not message or not message.strip(): return "Please type a message! ๐ฌ" user_input = message.strip().lower() # Check for exit command FIRST if user_input in ["bye", "goodbye", "exit", "quit", "see you later", "talk to you later"]: farewell = f"Goodbye {self.user_name if self.user_name else 'friend'}! ๐ Thanks for chatting! Have a great day! ๐" self.user_name = None self.conversation_count = 0 self.last_question = None self.waiting_for_response = False return farewell self.conversation_count += 1 # Extract name from conversation history if not self.user_name and history: for user_msg, bot_msg in history: if user_msg: user_lower = user_msg.lower() if "my name is" in user_lower: parts = user_lower.split("my name is") if len(parts) > 1: self.user_name = parts[-1].strip().title() break elif "call me" in user_lower: parts = user_lower.split("call me") if len(parts) > 1: self.user_name = parts[-1].strip().title() break response = self.get_response(user_input, history) return response def get_response(self, user_input, history): """Get appropriate response based on user input""" # Handle waiting for response state if self.waiting_for_response and self.last_question: response = self.handle_follow_up_response(user_input) self.waiting_for_response = False self.last_question = None return response # === HELP MENU === if user_input in ["help", "commands", "what can you say", "menu", "options"]: return self.show_help_menu() # === GREETINGS === elif user_input in ["hello", "hi", "hey", "greetings", "sup", "hola", "good morning", "good afternoon", "good evening", "yo"]: greetings = [ f"Hey {self.user_name}! ๐ Great to see you! What would you like to chat about?" if self.user_name else "Hey there! ๐ I'm Nova, your AI assistant. What's your name? (Say 'My name is [your name]')", f"Hello {self.user_name}! ๐ How can I make your day better?" if self.user_name else "Hi there! ๐ I'm Nova. What should I call you?", f"Greetings {self.user_name}! โจ Ready for a fun chat?" if self.user_name else "Hello! ๐ Tell me your name so we can chat!" ] return random.choice(greetings) # === NAME HANDLING === elif "my name is" in user_input: parts = user_input.split("my name is") if len(parts) > 1: name = parts[-1].strip().title() name = name.split()[0] self.user_name = name return f"โจ Nice to meet you, {name}! โจ\n\nI'm Nova, your rule-based AI assistant. I can tell jokes, recommend movies, suggest food, and more!\n\nWhat would you like to do first? ๐ซ" elif "call me" in user_input: parts = user_input.split("call me") if len(parts) > 1: name = parts[-1].strip().title() name = name.split()[0] self.user_name = name return f"Got it! I'll call you {name} from now on. ๐ฏ\n\nSo {name}, what's on your mind today?" elif user_input in ["what is my name", "do you know my name", "my name", "whats my name"]: if self.user_name: return f"Of course I remember! Your name is **{self.user_name}**. ๐\n\nWhat would you like to talk about?" else: return "I don't know your name yet! ๐ค\n\nWhat should I call you? (Say 'My name is [your name]')" # === HOW ARE YOU === elif user_input in ["how are you", "how are you doing", "how's it going", "how are you today", "how do you do", "how's everything"]: responses = [ "I'm absolutely fantastic! ๐ Thanks for asking! How are you feeling today?", "Running at 100% efficiency! โก But more importantly, how are YOU doing?", "I'm great! Just enjoying our conversation. Tell me about your day! ๐", "Couldn't be better! ๐ So, what's new with you?", "I'm doing wonderfully! ๐ซ Thanks for checking in!" ] return random.choice(responses) # === EMOTIONAL RESPONSES === elif any(phrase in user_input for phrase in ["i am good", "i am fine", "doing well", "i'm good", "i'm fine", "feeling good", "pretty good"]): responses = [ "That's wonderful to hear! ๐ Positivity is contagious! What's been making you happy lately?", "Awesome! ๐ Keep that positive energy flowing! Want to hear a joke to make you smile more?", "Fantastic! ๐ช Let's keep the good vibes going! What would you like to chat about?" ] return random.choice(responses) elif any(phrase in user_input for phrase in ["i am great", "awesome", "fantastic", "amazing", "wonderful", "excellent"]): responses = [ "Yesss! Love that energy! ๐ฅ Let's keep this positive vibe going! Want something fun?", "That's the spirit! ๐ You're on fire today! What's got you so excited?", "Amazing! ๐ซ I love your enthusiasm! How can I add to your great day?" ] return random.choice(responses) elif any(phrase in user_input for phrase in ["i am sad", "feeling down", "not good", "depressed", "unhappy", "upset", "feeling bad", "miserable"]): responses = [ "๐ I'm really sorry you're feeling this way. Remember, it's okay to not be okay sometimes.\n\nWould you like to:\nโข Hear a joke to cheer up? ๐\nโข Get some motivation? ๐ช\nโข Just talk it out? ๐ฃ๏ธ\n\nJust tell me what you need!", "๐ I hear you. Tough times don't last, but tough people do. Want me to tell you something uplifting?", "๐ Sending you virtual hugs! Remember, every storm runs out of rain. How can I help brighten your day?" ] return random.choice(responses) elif any(phrase in user_input for phrase in ["i am tired", "exhausted", "sleepy", "worn out", "drained"]): responses = [ "Take a break! ๐ด You deserve some rest. Maybe try:\nโข Deep breathing ๐ฌ๏ธ\nโข A short walk ๐ถ\nโข Listening to music ๐ต\n\nWant a fun fact to energize you?", "Rest is productive! ๐ Don't forget to take care of yourself. Need motivation or a laugh to recharge?", "I hear you! ๐ด Self-care is important. How about a quick joke to lift your spirits?" ] return random.choice(responses) # === JOKES & FUN === elif any(phrase in user_input for phrase in ["joke", "funny", "make me laugh", "tell me a joke", "jokes", "funny stuff", "humor"]): jokes = [ "Why don't scientists trust atoms? Because they make up everything! ๐งช", "What do you call fake spaghetti? An impasta! ๐", "Why don't skeletons fight each other? They don't have the guts! ๐", "Why did the scarecrow win an award? Because he was outstanding in his field! ๐พ", "What do you call a bear with no teeth? A gummy bear! ๐ป" ] return f"๐ **Here's a joke for you:**\n\n{random.choice(jokes)}\n\n---\nWant another one? Just say **'another joke'** !" elif "another joke" in user_input: jokes = [ "What's the best thing about Switzerland? I don't know, but the flag is a big plus! ๐จ๐ญ", "Why did the man throw his clock out the window? He wanted to see time fly! ๐", "What do you call a can opener that doesn't work? A can't opener! ๐ฅซ" ] return f"๐ **Another joke coming up:**\n\n{random.choice(jokes)}\n\n---\nKeep the laughter going? Just say **'joke'** again!" # === MOVIE RECOMMENDATIONS === elif any(phrase in user_input for phrase in ["movie", "film", "watch", "recommend a movie", "cinema", "movies"]): response = self.get_movie_recommendation(user_input) if "Want to try another genre?" in response: self.last_question = "movie_genre" self.waiting_for_response = True return response # === FOOD RECOMMENDATIONS === elif any(phrase in user_input for phrase in ["hungry", "food", "eat", "restaurant", "cook", "recipe", "cuisine", "meal", "dinner", "lunch", "breakfast"]): response = self.get_food_recommendation(user_input) if "Try another cuisine?" in response: self.last_question = "food_cuisine" self.waiting_for_response = True return response # === MOTIVATION & QUOTES === elif any(phrase in user_input for phrase in ["motivate", "inspire", "quote", "motivation", "inspiration", "encourage", "empower"]): quotes = [ "๐ช **The only way to do great work is to love what you do.** - Steve Jobs", "๐ **Believe you can and you're halfway there.** - Theodore Roosevelt", "๐ **Don't watch the clock; do what it does. Keep going.** - Sam Levenson" ] return f"โจ **Here's some motivation for you:**\n\n{random.choice(quotes)}\n\n---\nYou've got this! ๐ช Keep going!" # === INTERESTS & HOBBIES === elif any(phrase in user_input for phrase in ["hobby", "interest", "like to do", "free time", "pastime", "activities"]): hobbies = [ "๐จ **Creative hobbies:** Photography ๐ธ, Painting ๐จ, Writing โ๏ธ, Drawing โ๏ธ", "๐ป **Tech hobbies:** Coding ๐ป, Gaming ๐ฎ, Building computers ๐ฅ๏ธ, 3D Printing ๐จ๏ธ" ] response = random.choice(hobbies) response += "\n\nWhat's YOUR favorite hobby? Tell me about it! I'd love to know! ๐ซ" self.last_question = "hobby_response" self.waiting_for_response = True return response # === COMPLIMENTS === elif "you are" in user_input and any(word in user_input for word in ["nice", "smart", "cool", "awesome", "amazing", "great"]): compliments = [ "Aww, you're making me blush! ๐ค๐ Thank you so much! You're pretty amazing yourself!", "You're too kind! ๐ Thanks for the compliment! What can I help you with?" ] return random.choice(compliments) # === THANKS === elif any(phrase in user_input for phrase in ["thank", "thanks", "appreciate", "grateful", "thank you"]): responses = [ "You're absolutely welcome! ๐ Always happy to help!", "My pleasure! ๐ Let me know if there's anything else!" ] return random.choice(responses) # === DEFAULT RESPONSE === else: return self.get_default_response(user_input) def handle_follow_up_response(self, user_input): """Handle responses to follow-up questions""" if self.last_question == "movie_genre": return self.get_movie_recommendation(user_input + " movie") elif self.last_question == "food_cuisine": return self.get_food_recommendation(user_input) elif self.last_question == "hobby_response": return f"That's awesome! ๐ {user_input.title()} is a wonderful hobby! Keep pursuing what makes you happy! ๐ซ\n\nIs there anything else I can help you with today?" else: return f"Interesting! ๐ค Thanks for sharing! \n\nIs there anything specific you'd like to ask me about now?" def get_movie_recommendation(self, user_input): """Enhanced movie recommendation system""" if "action" in user_input: return """๐ฌ **Action Movies:** ๐ฅ โข **Mad Max: Fury Road** - Insane stunts and non-stop action โข **John Wick** - Amazing fight choreography โข **The Dark Knight** - Perfect blend of action and story Which one sounds interesting? Want another genre? (Just say 'comedy movie', 'drama movie', etc.)""" elif "comedy" in user_input: return """๐ **Comedy Movies:** ๐ญ โข **Superbad** - Hilarious coming-of-age story โข **The Hangover** - Wild Vegas adventure โข **Bridesmaids** - Surprisingly funny and heartwarming Want to try another genre? Just say 'action movie', 'drama movie', etc.!""" else: return """๐ฌ **Movie Genres I Know:** ๐ฟ โข **Action** ๐ฅ - Say "action movie" โข **Comedy** ๐ - Say "comedy movie" Just tell me the genre and I'll recommend something awesome! ๐""" def get_food_recommendation(self, user_input): """Food recommendation system""" if "italian" in user_input: return """๐ **Italian Cuisine:** ๐ โข **Pasta Carbonara** - Creamy, savory, delicious โข **Margherita Pizza** - Simple and perfect โข **Lasagna** - Layers of heaven I'm getting hungry just thinking about them! ๐คค Want to try another cuisine? (Just say 'mexican food', 'chinese food', etc.)""" elif "mexican" in user_input: return """๐ฎ **Mexican Cuisine:** ๐ฅ โข **Tacos al Pastor** - Flavorful street tacos โข **Guacamole** - Fresh and creamy โข **Burrito Bowl** - Customizable and hearty Hungry yet? ๐ถ๏ธ Want to try another cuisine?""" else: return """๐ **Feeling Hungry?** ๐ I can recommend food from different cuisines: ๐ฎ๐น **Italian** - Pasta, Pizza, Risotto ๐ฒ๐ฝ **Mexican** - Tacos, Burritos, Guacamole **Just say the cuisine name** (like "italian food" or "mexican cuisine") and I'll give you some tasty options! ๐ฝ๏ธ""" def get_default_response(self, user_input): """Smart default response based on keyword detection""" # Check for various keywords in user input keyword_responses = { "love": "Love is beautiful! ๐ Tell me more about what you love!", "work": "Work-life balance is key! ๐ผ How's your work going today?", "music": "Music heals the soul! ๐ต What's your favorite genre or artist?", "sports": "Staying active is great! โฝ What sports do you enjoy playing or watching?" } for keyword, response in keyword_responses.items(): if keyword in user_input: return response + " ๐ซ" # If no keywords match, return default message with sample questions return """๐ค **Hmm, I'm not sure I understood that completely.** **Here's what you can try:** โจ โข Type **'help'** to see all my commands โข Ask for a **'joke'** to lighten the mood ๐ โข Tell me **'how you feel'** (sad/happy/tired) ๐ญ โข Ask for a **'movie'** or **'food'** recommendation ๐ฌ๐ โข Ask **'what can you do'** to learn about my features โข Just say **'hello'** to start over ๐ **Sample questions you can ask:** ๐ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ ๐ฌ "Tell me a joke" ๐ฌ "How are you?" ๐ฌ "Recommend an action movie" ๐ฌ "I'm hungry for italian food" ๐ฌ "Motivate me" ๐ฌ "What's your favorite hobby?" ๐ฌ "How's the weather?" โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ I'm here to help and chat! What would you like to do? ๐""" def show_help_menu(self): """Beautiful and comprehensive help menu""" return """๐ **COMPLETE COMMAND MENU** ๐ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ **๐ฃ๏ธ BASIC CHAT** โข `hello`, `hi`, `hey` - Start a conversation โข `My name is [name]` - Introduce yourself โข `how are you` - Check in with me โข `bye`, `goodbye` - End conversation โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ **๐ FUN & GAMES** โข `joke`, `tell me a joke` - Get a random joke โข `another joke` - Keep the laughter going โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ **๐ฌ ENTERTAINMENT** โข `movie`, `action movie` - Action film recommendations โข `comedy movie` - Comedy film recommendations โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ **๐ FOOD & CUISINE** โข `food`, `hungry` - General food recommendations โข `italian food` - Italian cuisine suggestions โข `mexican food` - Mexican cuisine suggestions โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ **๐ช MOTIVATION** โข `motivate`, `inspire` - Daily motivation quotes โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ **โน๏ธ INFORMATION** โข `help`, `commands` - Show this menu โข `what can you do` - Learn about my features โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ **What would you like to try first? ๐**""" # Define custom CSS as a global variable CUSTOM_CSS = """ """ # Modern UI with custom styling def create_chatbot(): """Create a beautiful modern UI for the chatbot""" bot = ModernChatbot() with gr.Blocks(title="๐ค Nova AI - Smart Rule-Based Chatbot") as demo: # Header Section gr.HTML("""
Intelligent Conversations | Instant Responses | Always Here for You