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("""

๐Ÿค– Nova AI Assistant

Intelligent Conversations | Instant Responses | Always Here for You

""") # Quick Feature Cards gr.HTML("""
๐Ÿ‘‹ Say Hello
๐Ÿ˜‚ Get a Joke
๐ŸŽฌ Movie Recommendation
๐Ÿ• Food Suggestion
๐Ÿ’ช Motivation
๐Ÿ“š Help Menu
""") # Main Chat Interface gr.ChatInterface( fn=bot.process_message, title="๐Ÿ’ฌ Chat with Nova", description=""" โœจ **Modern AI Assistant** - I understand natural language and respond intelligently! ๐Ÿ’ก **Pro Tip:** Type **'help'** to see all commands | Say **'bye'** to end conversation """, examples=[ ["hello ๐Ÿ‘‹"], ["My name is Sarah"], ["how are you?"], ["tell me a joke ๐Ÿ˜‚"], ["I'm feeling sad today ๐Ÿ’™"], ["recommend an action movie ๐ŸŽฌ"], ["I'm hungry for italian food ๐Ÿ•"], ["motivate me please ๐Ÿ’ช"], ["what can you do?"], ["help ๐Ÿ“š"], ["bye ๐Ÿ‘‹"] ] ) # Footer gr.HTML(""" """) # JavaScript for feature card clicks gr.HTML(""" """) return demo # Launch the app if __name__ == "__main__": demo = create_chatbot() # Apply theme and CSS in launch demo.launch( share=True, theme=gr.themes.Soft(primary_hue="purple", secondary_hue="pink"), css=CUSTOM_CSS )