Jarvis / app.py
ShoaibAI's picture
Update app.py
c3e4137 verified
Raw History Blame Contribute Delete
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)