Download app.py from ShoaibAI/Jarvis: direct link, hf CLI and curl.
- Browser
- Download file 10.6 kB
-
https://huggingface.co/spaces/ShoaibAI/Jarvis/resolve/main/app.py
- Command line
-
hf download hf://spaces/ShoaibAI/Jarvis/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ShoaibAI/Jarvis/resolve/main/app.py
10.6 kB
| import gradio as gr | |
| import random | |
| import re | |
| from datetime import datetime | |
| print("=" * 60) | |
| print("ποΈ Jarvis AI Assistant Starting...") | |
| print("=" * 60) | |
| class Jarvis: | |
| def __init__(self): | |
| self.reminders = [] | |
| self.conversation = [] | |
| print("β Jarvis initialized successfully") | |
| def respond(self, message, history): | |
| if not message or not message.strip(): | |
| return "", history | |
| msg = message.lower().strip() | |
| # Store in conversation | |
| self.conversation.append(msg) | |
| if len(self.conversation) > 10: | |
| self.conversation.pop(0) | |
| # Handle reminders | |
| if msg == "list reminders": | |
| if not self.reminders: | |
| response = "π You have no active reminders." | |
| else: | |
| response = "π Your reminders:\n" + "\n".join([f" β’ {r}" for r in self.reminders]) | |
| history.append((message, response)) | |
| return "", history | |
| if msg == "clear reminders": | |
| count = len(self.reminders) | |
| self.reminders = [] | |
| response = f"ποΈ Cleared {count} reminder(s)." | |
| history.append((message, response)) | |
| return "", history | |
| if msg.startswith("remind me to "): | |
| reminder = message.replace("remind me to", "").strip() | |
| self.reminders.append(reminder) | |
| response = f"β Reminder set: '{reminder}'" | |
| history.append((message, response)) | |
| return "", history | |
| # Time | |
| if any(word in msg for word in ["time", "clock"]): | |
| now = datetime.now() | |
| response = f"π The current time is {now.strftime('%I:%M %p')}" | |
| history.append((message, response)) | |
| return "", history | |
| # Date | |
| if any(word in msg for word in ["date", "today"]): | |
| now = datetime.now() | |
| response = f"π Today is {now.strftime('%A, %B %d, %Y')}" | |
| history.append((message, response)) | |
| return "", history | |
| # Jokes | |
| if any(word in msg for word in ["joke", "funny", "laugh"]): | |
| jokes = [ | |
| "Why don't scientists trust atoms? Because they make up everything! π", | |
| "What do you call a fake noodle? An impasta! π", | |
| "Why did the scarecrow win an award? He was outstanding in his field! πΎ", | |
| "What do you call a bear with no teeth? A gummy bear! π»", | |
| "Why don't eggs tell jokes? They'd crack each other up! π₯", | |
| "What do you call a fish wearing a bowtie? Sofishticated! π ", | |
| "Why did the coffee file a police report? It got mugged! β" | |
| ] | |
| response = f"π {random.choice(jokes)}" | |
| history.append((message, response)) | |
| return "", history | |
| # Quotes/Motivation | |
| if any(word in msg for word in ["quote", "motivate", "inspire"]): | |
| quotes = [ | |
| "The only limit is your mind. Keep pushing forward! πͺ", | |
| "Every expert was once a beginner. You've got this! π", | |
| "Success is not final, failure is not fatal. It's the courage to continue that counts.", | |
| "Your potential is endless. Keep learning and growing! π", | |
| "Believe you can and you're halfway there. - Theodore Roosevelt", | |
| "It does not matter how slowly you go as long as you do not stop. - Confucius" | |
| ] | |
| response = f"π« {random.choice(quotes)}" | |
| history.append((message, response)) | |
| return "", history | |
| # Math calculations | |
| if any(word in msg for word in ["calculate", "what is", "=", "plus", "minus", "times", "divided"]): | |
| # Extract numbers | |
| numbers = re.findall(r'\d+', msg) | |
| if len(numbers) >= 2: | |
| try: | |
| if "plus" in msg or "+" in msg: | |
| result = int(numbers[0]) + int(numbers[1]) | |
| response = f"π’ {numbers[0]} + {numbers[1]} = {result}" | |
| elif "minus" in msg or "-" in msg: | |
| result = int(numbers[0]) - int(numbers[1]) | |
| response = f"π’ {numbers[0]} - {numbers[1]} = {result}" | |
| elif "times" in msg or "*" in msg or "x" in msg: | |
| result = int(numbers[0]) * int(numbers[1]) | |
| response = f"π’ {numbers[0]} Γ {numbers[1]} = {result}" | |
| elif "divided" in msg or "/" in msg: | |
| if int(numbers[1]) != 0: | |
| result = int(numbers[0]) / int(numbers[1]) | |
| response = f"π’ {numbers[0]} Γ· {numbers[1]} = {result}" | |
| else: | |
| response = "β Cannot divide by zero!" | |
| else: | |
| response = "Try: 'calculate 5 + 3' or '10 divided by 2'" | |
| history.append((message, response)) | |
| return "", history | |
| except: | |
| pass | |
| # Greetings | |
| if any(word in msg for word in ["hello", "hi", "hey", "greetings"]): | |
| greetings = [ | |
| "Hello! How can I assist you today? ποΈ", | |
| "Hi there! Ready to help! π«", | |
| "Greetings! What can I do for you? β¨", | |
| "Hey! Jarvis at your service! π" | |
| ] | |
| response = random.choice(greetings) | |
| history.append((message, response)) | |
| return "", history | |
| # Farewell | |
| if any(word in msg for word in ["bye", "goodbye", "see you"]): | |
| farewells = [ | |
| "Goodbye! I'm always here when you need me. π", | |
| "See you later! Have a great day! π", | |
| "Take care! Come back anytime! π" | |
| ] | |
| response = random.choice(farewells) | |
| history.append((message, response)) | |
| return "", history | |
| # About Jarvis | |
| if any(word in msg for word in ["who are you", "your name"]): | |
| response = "I'm Jarvis, your personal AI assistant! I can help with time, date, jokes, math, reminders, and more! π€" | |
| history.append((message, response)) | |
| return "", history | |
| # How are you | |
| if "how are you" in msg: | |
| response = "I'm doing great! Thanks for asking! How can I help you today? π" | |
| history.append((message, response)) | |
| return "", history | |
| # Thanks | |
| if any(word in msg for word in ["thank", "thanks"]): | |
| response = "You're welcome! Always happy to help! π" | |
| history.append((message, response)) | |
| return "", history | |
| # Help | |
| if msg == "help" or msg == "what can you do": | |
| response = """π― **Jarvis Commands** | |
| β° **Time & Date** | |
| β’ "What time is it?" | |
| β’ "What's today's date?" | |
| π **Fun** | |
| β’ "Tell me a joke" | |
| β’ "Give me a quote" | |
| π’ **Math** | |
| β’ "Calculate 5 + 3" | |
| β’ "What is 10 minus 4?" | |
| β’ "5 times 6" | |
| β’ "20 divided by 4" | |
| π **Reminders** | |
| β’ "Remind me to call mom" | |
| β’ "List reminders" | |
| β’ "Clear reminders" | |
| π¬ **Chat** | |
| β’ "Hello" | |
| β’ "How are you?" | |
| β’ "Who are you?" | |
| β’ "Thanks" | |
| β’ "Goodbye" | |
| **Just type naturally!** π""" | |
| history.append((message, response)) | |
| return "", history | |
| # Default response | |
| response = f"I heard: '{message}'. Try saying 'help' to see what I can do! π―" | |
| history.append((message, response)) | |
| return "", history | |
| # Create Jarvis instance | |
| jarvis = Jarvis() | |
| # Create Gradio interface | |
| with gr.Blocks(title="Jarvis AI Assistant", theme=gr.themes.Soft()) as demo: | |
| gr.Markdown(""" | |
| # ποΈ **Jarvis AI - Your Personal Assistant** | |
| ### *Smart, Fast, and Private - No Cloud Required* | |
| --- | |
| """) | |
| chatbot = gr.Chatbot( | |
| height=500, | |
| bubble_full_width=False, | |
| avatar_images=(None, "π€"), | |
| label="Conversation" | |
| ) | |
| with gr.Row(): | |
| msg = gr.Textbox( | |
| label="Your message", | |
| placeholder="Type your command here... (Press Enter to send)", | |
| scale=4, | |
| lines=2 | |
| ) | |
| send = gr.Button("Send", variant="primary", scale=1) | |
| with gr.Row(): | |
| clear = gr.Button("Clear Chat", variant="secondary", size="sm") | |
| gr.Markdown("---") | |
| gr.Markdown("### π **Quick Commands**") | |
| # Quick command buttons | |
| quick_commands = [ | |
| "Hello", "What time is it?", "Tell me a joke", | |
| "Calculate 25 * 4", "Remind me to buy milk", "Help" | |
| ] | |
| # Create rows of buttons | |
| for i in range(0, len(quick_commands), 3): | |
| with gr.Row(): | |
| for cmd in quick_commands[i:i+3]: | |
| btn = gr.Button(cmd, size="sm", variant="secondary") | |
| btn.click(fn=lambda x=cmd: x, outputs=[msg]) | |
| gr.Markdown(""" | |
| --- | |
| ### β¨ **Features** | |
| | Category | Examples | | |
| |----------|----------| | |
| | π Time | "What time is it?" | | |
| | π Date | "What's today's date?" | | |
| | π Jokes | "Tell me a joke" | | |
| | πͺ Motivation | "Give me a quote" | | |
| | π’ Math | "Calculate 5 + 3" | | |
| | π Reminders | "Remind me to..." | | |
| | π¬ Chat | "Hello", "How are you?" | | |
| ### π **Privacy Guarantee** | |
| - β 100% local processing | |
| - β No data stored permanently | |
| - β No external APIs | |
| - β Your conversations stay private | |
| ### π‘ **Pro Tips** | |
| - Try different phrasings - I understand natural language! | |
| - Use "Help" anytime to see all commands | |
| - Reminders stay until you clear them or the session ends | |
| **Start by typing "Hello"!** π | |
| """) | |
| # Connect the respond function | |
| def respond(message, history): | |
| return jarvis.respond(message, history) | |
| send.click(respond, [msg, chatbot], [msg, chatbot]) | |
| msg.submit(respond, [msg, chatbot], [msg, chatbot]) | |
| clear.click(lambda: [], None, chatbot) | |
| if __name__ == "__main__": | |
| print("=" * 60) | |
| print("π Jarvis AI Assistant is running!") | |
| print("β Status: Online") | |
| print("β Features: Time, Date, Jokes, Quotes, Math, Reminders, Chat") | |
| print("β Interface: Gradio " + gr.__version__) | |
| print("=" * 60) | |
| demo.launch(share=False) |