Update app.py
Browse files
app.py
CHANGED
|
@@ -10,39 +10,56 @@ print("=" * 60)
|
|
| 10 |
class Jarvis:
|
| 11 |
def __init__(self):
|
| 12 |
self.reminders = []
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
def respond(self, message):
|
| 16 |
if not message or not message.strip():
|
| 17 |
-
return "
|
| 18 |
|
| 19 |
msg = message.lower().strip()
|
| 20 |
|
| 21 |
-
# Store
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
if msg == "list reminders":
|
| 23 |
if not self.reminders:
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
if msg == "clear reminders":
|
| 28 |
count = len(self.reminders)
|
| 29 |
self.reminders = []
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
|
| 32 |
if msg.startswith("remind me to "):
|
| 33 |
reminder = message.replace("remind me to", "").strip()
|
| 34 |
self.reminders.append(reminder)
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# Time
|
| 38 |
if any(word in msg for word in ["time", "clock"]):
|
| 39 |
now = datetime.now()
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# Date
|
| 43 |
if any(word in msg for word in ["date", "today"]):
|
| 44 |
now = datetime.now()
|
| 45 |
-
|
|
|
|
|
|
|
| 46 |
|
| 47 |
# Jokes
|
| 48 |
if any(word in msg for word in ["joke", "funny", "laugh"]):
|
|
@@ -51,81 +68,100 @@ class Jarvis:
|
|
| 51 |
"What do you call a fake noodle? An impasta! π",
|
| 52 |
"Why did the scarecrow win an award? He was outstanding in his field! πΎ",
|
| 53 |
"What do you call a bear with no teeth? A gummy bear! π»",
|
| 54 |
-
"Why don't eggs tell jokes? They'd crack each other up! π₯"
|
|
|
|
|
|
|
| 55 |
]
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
# Quotes
|
| 59 |
if any(word in msg for word in ["quote", "motivate", "inspire"]):
|
| 60 |
quotes = [
|
| 61 |
"The only limit is your mind. Keep pushing forward! πͺ",
|
| 62 |
"Every expert was once a beginner. You've got this! π",
|
| 63 |
"Success is not final, failure is not fatal. It's the courage to continue that counts.",
|
| 64 |
-
"Your potential is endless. Keep learning and growing! π"
|
|
|
|
|
|
|
| 65 |
]
|
| 66 |
-
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
# Math
|
| 69 |
-
if any(word in msg for word in ["calculate", "what is", "="]):
|
| 70 |
-
# Extract numbers
|
| 71 |
numbers = re.findall(r'\d+', msg)
|
| 72 |
if len(numbers) >= 2:
|
| 73 |
try:
|
| 74 |
if "plus" in msg or "+" in msg:
|
| 75 |
result = int(numbers[0]) + int(numbers[1])
|
| 76 |
-
|
| 77 |
elif "minus" in msg or "-" in msg:
|
| 78 |
result = int(numbers[0]) - int(numbers[1])
|
| 79 |
-
|
| 80 |
elif "times" in msg or "*" in msg or "x" in msg:
|
| 81 |
result = int(numbers[0]) * int(numbers[1])
|
| 82 |
-
|
| 83 |
elif "divided" in msg or "/" in msg:
|
| 84 |
if int(numbers[1]) != 0:
|
| 85 |
result = int(numbers[0]) / int(numbers[1])
|
| 86 |
-
|
| 87 |
else:
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
except:
|
| 90 |
pass
|
| 91 |
|
| 92 |
# Greetings
|
| 93 |
if any(word in msg for word in ["hello", "hi", "hey", "greetings"]):
|
| 94 |
-
|
| 95 |
"Hello! How can I assist you today? ποΈ",
|
| 96 |
"Hi there! Ready to help! π«",
|
| 97 |
-
"Greetings! What can I do for you? β¨"
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
# Farewell
|
| 101 |
if any(word in msg for word in ["bye", "goodbye", "see you"]):
|
| 102 |
-
|
| 103 |
"Goodbye! I'm always here when you need me. π",
|
| 104 |
"See you later! Have a great day! π",
|
| 105 |
"Take care! Come back anytime! π"
|
| 106 |
-
]
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
# About
|
| 109 |
if any(word in msg for word in ["who are you", "your name"]):
|
| 110 |
-
|
|
|
|
|
|
|
| 111 |
|
| 112 |
# How are you
|
| 113 |
if "how are you" in msg:
|
| 114 |
-
|
|
|
|
|
|
|
| 115 |
|
| 116 |
# Thanks
|
| 117 |
if any(word in msg for word in ["thank", "thanks"]):
|
| 118 |
-
|
|
|
|
|
|
|
| 119 |
|
| 120 |
# Help
|
| 121 |
if msg == "help" or msg == "what can you do":
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
# Default
|
| 125 |
-
return f"I heard: '{message}'. Try saying 'help' to see what I can do! π―"
|
| 126 |
-
|
| 127 |
-
def get_help(self):
|
| 128 |
-
return """π― **Jarvis Commands**
|
| 129 |
|
| 130 |
β° **Time & Date**
|
| 131 |
β’ "What time is it?"
|
|
@@ -153,25 +189,35 @@ class Jarvis:
|
|
| 153 |
β’ "Thanks"
|
| 154 |
β’ "Goodbye"
|
| 155 |
|
| 156 |
-
β **Help**
|
| 157 |
-
β’ "Help" or "What can you do?"
|
| 158 |
-
|
| 159 |
**Just type naturally!** π"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
# Create Jarvis instance
|
| 162 |
jarvis = Jarvis()
|
| 163 |
|
| 164 |
-
# Gradio interface
|
| 165 |
with gr.Blocks(title="Jarvis AI Assistant", theme=gr.themes.Soft()) as demo:
|
|
|
|
| 166 |
gr.Markdown("""
|
| 167 |
# ποΈ **Jarvis AI - Your Personal Assistant**
|
| 168 |
|
| 169 |
-
### *Smart, Fast, and Private*
|
| 170 |
|
| 171 |
---
|
| 172 |
""")
|
| 173 |
|
| 174 |
-
chatbot = gr.Chatbot(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
with gr.Row():
|
| 177 |
msg = gr.Textbox(
|
|
@@ -183,18 +229,18 @@ with gr.Blocks(title="Jarvis AI Assistant", theme=gr.themes.Soft()) as demo:
|
|
| 183 |
send = gr.Button("Send", variant="primary", scale=1)
|
| 184 |
|
| 185 |
with gr.Row():
|
| 186 |
-
clear = gr.Button("Clear Chat", variant="secondary")
|
| 187 |
|
| 188 |
gr.Markdown("---")
|
| 189 |
gr.Markdown("### π **Quick Commands**")
|
| 190 |
|
| 191 |
-
# Quick command buttons
|
| 192 |
quick_commands = [
|
| 193 |
"Hello", "What time is it?", "Tell me a joke",
|
| 194 |
"Calculate 25 * 4", "Remind me to buy milk", "Help"
|
| 195 |
]
|
| 196 |
|
| 197 |
-
# Create
|
| 198 |
for i in range(0, len(quick_commands), 3):
|
| 199 |
with gr.Row():
|
| 200 |
for cmd in quick_commands[i:i+3]:
|
|
@@ -205,10 +251,10 @@ with gr.Blocks(title="Jarvis AI Assistant", theme=gr.themes.Soft()) as demo:
|
|
| 205 |
---
|
| 206 |
### β¨ **Features**
|
| 207 |
|
| 208 |
-
|
|
| 209 |
-
|---------|---------|
|
| 210 |
| π Time | "What time is it?" |
|
| 211 |
-
| π
Date | "
|
| 212 |
| π Jokes | "Tell me a joke" |
|
| 213 |
| πͺ Motivation | "Give me a quote" |
|
| 214 |
| π’ Math | "Calculate 5 + 3" |
|
|
@@ -217,25 +263,21 @@ with gr.Blocks(title="Jarvis AI Assistant", theme=gr.themes.Soft()) as demo:
|
|
| 217 |
|
| 218 |
### π **Privacy Guarantee**
|
| 219 |
- β
100% local processing
|
| 220 |
-
- β
No data stored
|
| 221 |
- β
No external APIs
|
| 222 |
- β
Your conversations stay private
|
| 223 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
**Start by typing "Hello"!** π
|
| 225 |
""")
|
| 226 |
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
response = jarvis.respond(message)
|
| 232 |
-
chat_history.append((message, response))
|
| 233 |
-
|
| 234 |
-
# Keep only last 20 exchanges
|
| 235 |
-
if len(chat_history) > 20:
|
| 236 |
-
chat_history = chat_history[-20:]
|
| 237 |
-
|
| 238 |
-
return "", chat_history
|
| 239 |
|
| 240 |
send.click(respond, [msg, chatbot], [msg, chatbot])
|
| 241 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
|
@@ -243,8 +285,9 @@ with gr.Blocks(title="Jarvis AI Assistant", theme=gr.themes.Soft()) as demo:
|
|
| 243 |
|
| 244 |
if __name__ == "__main__":
|
| 245 |
print("=" * 60)
|
| 246 |
-
print("π Jarvis is running!")
|
| 247 |
-
print("β
|
| 248 |
-
print("β
Features: Time, Date, Jokes, Math, Reminders, Chat")
|
|
|
|
| 249 |
print("=" * 60)
|
| 250 |
demo.launch(share=False)
|
|
|
|
| 10 |
class Jarvis:
|
| 11 |
def __init__(self):
|
| 12 |
self.reminders = []
|
| 13 |
+
self.conversation = []
|
| 14 |
+
print("β
Jarvis initialized successfully")
|
| 15 |
|
| 16 |
+
def respond(self, message, history):
|
| 17 |
if not message or not message.strip():
|
| 18 |
+
return "", history
|
| 19 |
|
| 20 |
msg = message.lower().strip()
|
| 21 |
|
| 22 |
+
# Store in conversation
|
| 23 |
+
self.conversation.append(msg)
|
| 24 |
+
if len(self.conversation) > 10:
|
| 25 |
+
self.conversation.pop(0)
|
| 26 |
+
|
| 27 |
+
# Handle reminders
|
| 28 |
if msg == "list reminders":
|
| 29 |
if not self.reminders:
|
| 30 |
+
response = "π You have no active reminders."
|
| 31 |
+
else:
|
| 32 |
+
response = "π Your reminders:\n" + "\n".join([f" β’ {r}" for r in self.reminders])
|
| 33 |
+
history.append((message, response))
|
| 34 |
+
return "", history
|
| 35 |
|
| 36 |
if msg == "clear reminders":
|
| 37 |
count = len(self.reminders)
|
| 38 |
self.reminders = []
|
| 39 |
+
response = f"ποΈ Cleared {count} reminder(s)."
|
| 40 |
+
history.append((message, response))
|
| 41 |
+
return "", history
|
| 42 |
|
| 43 |
if msg.startswith("remind me to "):
|
| 44 |
reminder = message.replace("remind me to", "").strip()
|
| 45 |
self.reminders.append(reminder)
|
| 46 |
+
response = f"β
Reminder set: '{reminder}'"
|
| 47 |
+
history.append((message, response))
|
| 48 |
+
return "", history
|
| 49 |
|
| 50 |
# Time
|
| 51 |
if any(word in msg for word in ["time", "clock"]):
|
| 52 |
now = datetime.now()
|
| 53 |
+
response = f"π The current time is {now.strftime('%I:%M %p')}"
|
| 54 |
+
history.append((message, response))
|
| 55 |
+
return "", history
|
| 56 |
|
| 57 |
# Date
|
| 58 |
if any(word in msg for word in ["date", "today"]):
|
| 59 |
now = datetime.now()
|
| 60 |
+
response = f"π
Today is {now.strftime('%A, %B %d, %Y')}"
|
| 61 |
+
history.append((message, response))
|
| 62 |
+
return "", history
|
| 63 |
|
| 64 |
# Jokes
|
| 65 |
if any(word in msg for word in ["joke", "funny", "laugh"]):
|
|
|
|
| 68 |
"What do you call a fake noodle? An impasta! π",
|
| 69 |
"Why did the scarecrow win an award? He was outstanding in his field! πΎ",
|
| 70 |
"What do you call a bear with no teeth? A gummy bear! π»",
|
| 71 |
+
"Why don't eggs tell jokes? They'd crack each other up! π₯",
|
| 72 |
+
"What do you call a fish wearing a bowtie? Sofishticated! π ",
|
| 73 |
+
"Why did the coffee file a police report? It got mugged! β"
|
| 74 |
]
|
| 75 |
+
response = f"π {random.choice(jokes)}"
|
| 76 |
+
history.append((message, response))
|
| 77 |
+
return "", history
|
| 78 |
|
| 79 |
+
# Quotes/Motivation
|
| 80 |
if any(word in msg for word in ["quote", "motivate", "inspire"]):
|
| 81 |
quotes = [
|
| 82 |
"The only limit is your mind. Keep pushing forward! πͺ",
|
| 83 |
"Every expert was once a beginner. You've got this! π",
|
| 84 |
"Success is not final, failure is not fatal. It's the courage to continue that counts.",
|
| 85 |
+
"Your potential is endless. Keep learning and growing! π",
|
| 86 |
+
"Believe you can and you're halfway there. - Theodore Roosevelt",
|
| 87 |
+
"It does not matter how slowly you go as long as you do not stop. - Confucius"
|
| 88 |
]
|
| 89 |
+
response = f"π« {random.choice(quotes)}"
|
| 90 |
+
history.append((message, response))
|
| 91 |
+
return "", history
|
| 92 |
|
| 93 |
+
# Math calculations
|
| 94 |
+
if any(word in msg for word in ["calculate", "what is", "=", "plus", "minus", "times", "divided"]):
|
| 95 |
+
# Extract numbers
|
| 96 |
numbers = re.findall(r'\d+', msg)
|
| 97 |
if len(numbers) >= 2:
|
| 98 |
try:
|
| 99 |
if "plus" in msg or "+" in msg:
|
| 100 |
result = int(numbers[0]) + int(numbers[1])
|
| 101 |
+
response = f"π’ {numbers[0]} + {numbers[1]} = {result}"
|
| 102 |
elif "minus" in msg or "-" in msg:
|
| 103 |
result = int(numbers[0]) - int(numbers[1])
|
| 104 |
+
response = f"π’ {numbers[0]} - {numbers[1]} = {result}"
|
| 105 |
elif "times" in msg or "*" in msg or "x" in msg:
|
| 106 |
result = int(numbers[0]) * int(numbers[1])
|
| 107 |
+
response = f"π’ {numbers[0]} Γ {numbers[1]} = {result}"
|
| 108 |
elif "divided" in msg or "/" in msg:
|
| 109 |
if int(numbers[1]) != 0:
|
| 110 |
result = int(numbers[0]) / int(numbers[1])
|
| 111 |
+
response = f"π’ {numbers[0]} Γ· {numbers[1]} = {result}"
|
| 112 |
else:
|
| 113 |
+
response = "β Cannot divide by zero!"
|
| 114 |
+
else:
|
| 115 |
+
response = "Try: 'calculate 5 + 3' or '10 divided by 2'"
|
| 116 |
+
history.append((message, response))
|
| 117 |
+
return "", history
|
| 118 |
except:
|
| 119 |
pass
|
| 120 |
|
| 121 |
# Greetings
|
| 122 |
if any(word in msg for word in ["hello", "hi", "hey", "greetings"]):
|
| 123 |
+
greetings = [
|
| 124 |
"Hello! How can I assist you today? ποΈ",
|
| 125 |
"Hi there! Ready to help! π«",
|
| 126 |
+
"Greetings! What can I do for you? β¨",
|
| 127 |
+
"Hey! Jarvis at your service! π"
|
| 128 |
+
]
|
| 129 |
+
response = random.choice(greetings)
|
| 130 |
+
history.append((message, response))
|
| 131 |
+
return "", history
|
| 132 |
|
| 133 |
# Farewell
|
| 134 |
if any(word in msg for word in ["bye", "goodbye", "see you"]):
|
| 135 |
+
farewells = [
|
| 136 |
"Goodbye! I'm always here when you need me. π",
|
| 137 |
"See you later! Have a great day! π",
|
| 138 |
"Take care! Come back anytime! π"
|
| 139 |
+
]
|
| 140 |
+
response = random.choice(farewells)
|
| 141 |
+
history.append((message, response))
|
| 142 |
+
return "", history
|
| 143 |
|
| 144 |
+
# About Jarvis
|
| 145 |
if any(word in msg for word in ["who are you", "your name"]):
|
| 146 |
+
response = "I'm Jarvis, your personal AI assistant! I can help with time, date, jokes, math, reminders, and more! π€"
|
| 147 |
+
history.append((message, response))
|
| 148 |
+
return "", history
|
| 149 |
|
| 150 |
# How are you
|
| 151 |
if "how are you" in msg:
|
| 152 |
+
response = "I'm doing great! Thanks for asking! How can I help you today? π"
|
| 153 |
+
history.append((message, response))
|
| 154 |
+
return "", history
|
| 155 |
|
| 156 |
# Thanks
|
| 157 |
if any(word in msg for word in ["thank", "thanks"]):
|
| 158 |
+
response = "You're welcome! Always happy to help! π"
|
| 159 |
+
history.append((message, response))
|
| 160 |
+
return "", history
|
| 161 |
|
| 162 |
# Help
|
| 163 |
if msg == "help" or msg == "what can you do":
|
| 164 |
+
response = """π― **Jarvis Commands**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
β° **Time & Date**
|
| 167 |
β’ "What time is it?"
|
|
|
|
| 189 |
β’ "Thanks"
|
| 190 |
β’ "Goodbye"
|
| 191 |
|
|
|
|
|
|
|
|
|
|
| 192 |
**Just type naturally!** π"""
|
| 193 |
+
history.append((message, response))
|
| 194 |
+
return "", history
|
| 195 |
+
|
| 196 |
+
# Default response
|
| 197 |
+
response = f"I heard: '{message}'. Try saying 'help' to see what I can do! π―"
|
| 198 |
+
history.append((message, response))
|
| 199 |
+
return "", history
|
| 200 |
|
| 201 |
# Create Jarvis instance
|
| 202 |
jarvis = Jarvis()
|
| 203 |
|
| 204 |
+
# Create Gradio interface
|
| 205 |
with gr.Blocks(title="Jarvis AI Assistant", theme=gr.themes.Soft()) as demo:
|
| 206 |
+
|
| 207 |
gr.Markdown("""
|
| 208 |
# ποΈ **Jarvis AI - Your Personal Assistant**
|
| 209 |
|
| 210 |
+
### *Smart, Fast, and Private - No Cloud Required*
|
| 211 |
|
| 212 |
---
|
| 213 |
""")
|
| 214 |
|
| 215 |
+
chatbot = gr.Chatbot(
|
| 216 |
+
height=500,
|
| 217 |
+
bubble_full_width=False,
|
| 218 |
+
avatar_images=(None, "π€"),
|
| 219 |
+
label="Conversation"
|
| 220 |
+
)
|
| 221 |
|
| 222 |
with gr.Row():
|
| 223 |
msg = gr.Textbox(
|
|
|
|
| 229 |
send = gr.Button("Send", variant="primary", scale=1)
|
| 230 |
|
| 231 |
with gr.Row():
|
| 232 |
+
clear = gr.Button("Clear Chat", variant="secondary", size="sm")
|
| 233 |
|
| 234 |
gr.Markdown("---")
|
| 235 |
gr.Markdown("### π **Quick Commands**")
|
| 236 |
|
| 237 |
+
# Quick command buttons
|
| 238 |
quick_commands = [
|
| 239 |
"Hello", "What time is it?", "Tell me a joke",
|
| 240 |
"Calculate 25 * 4", "Remind me to buy milk", "Help"
|
| 241 |
]
|
| 242 |
|
| 243 |
+
# Create rows of buttons
|
| 244 |
for i in range(0, len(quick_commands), 3):
|
| 245 |
with gr.Row():
|
| 246 |
for cmd in quick_commands[i:i+3]:
|
|
|
|
| 251 |
---
|
| 252 |
### β¨ **Features**
|
| 253 |
|
| 254 |
+
| Category | Examples |
|
| 255 |
+
|----------|----------|
|
| 256 |
| π Time | "What time is it?" |
|
| 257 |
+
| π
Date | "What's today's date?" |
|
| 258 |
| π Jokes | "Tell me a joke" |
|
| 259 |
| πͺ Motivation | "Give me a quote" |
|
| 260 |
| π’ Math | "Calculate 5 + 3" |
|
|
|
|
| 263 |
|
| 264 |
### π **Privacy Guarantee**
|
| 265 |
- β
100% local processing
|
| 266 |
+
- β
No data stored permanently
|
| 267 |
- β
No external APIs
|
| 268 |
- β
Your conversations stay private
|
| 269 |
|
| 270 |
+
### π‘ **Pro Tips**
|
| 271 |
+
- Try different phrasings - I understand natural language!
|
| 272 |
+
- Use "Help" anytime to see all commands
|
| 273 |
+
- Reminders stay until you clear them or the session ends
|
| 274 |
+
|
| 275 |
**Start by typing "Hello"!** π
|
| 276 |
""")
|
| 277 |
|
| 278 |
+
# Connect the respond function
|
| 279 |
+
def respond(message, history):
|
| 280 |
+
return jarvis.respond(message, history)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
|
| 282 |
send.click(respond, [msg, chatbot], [msg, chatbot])
|
| 283 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
|
|
|
| 285 |
|
| 286 |
if __name__ == "__main__":
|
| 287 |
print("=" * 60)
|
| 288 |
+
print("π Jarvis AI Assistant is running!")
|
| 289 |
+
print("β
Status: Online")
|
| 290 |
+
print("β
Features: Time, Date, Jokes, Quotes, Math, Reminders, Chat")
|
| 291 |
+
print("β
Interface: Gradio " + gr.__version__)
|
| 292 |
print("=" * 60)
|
| 293 |
demo.launch(share=False)
|