Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,126 +1,116 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
#
|
| 69 |
-
#
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
return
|
| 82 |
-
|
| 83 |
-
#
|
| 84 |
-
if "
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
# ---------------------
|
| 118 |
-
def chat_fn(message, history):
|
| 119 |
-
response = agent_response(message)
|
| 120 |
-
return response
|
| 121 |
-
|
| 122 |
-
with gr.Blocks() as demo:
|
| 123 |
-
gr.Markdown("# 🧠 SmartLife Assistant Agent\n### Runs fully offline inside HuggingFace Space")
|
| 124 |
-
chatbot = gr.ChatInterface(fn=chat_fn)
|
| 125 |
-
|
| 126 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
|
| 3 |
+
# -------------------------
|
| 4 |
+
# Offline Weather Database
|
| 5 |
+
# -------------------------
|
| 6 |
+
OFFLINE_WEATHER = {
|
| 7 |
+
"bengaluru": "Sunny with mild breeze.",
|
| 8 |
+
"bangalore": "Sunny with mild breeze.",
|
| 9 |
+
"chitradurga": "Cloudy with light breeze.",
|
| 10 |
+
"madurai": "Hot and dry.",
|
| 11 |
+
"chennai": "Humid and warm.",
|
| 12 |
+
"mumbai": "Humid with chance of rain.",
|
| 13 |
+
"mysuru": "Cool and pleasant.",
|
| 14 |
+
"delhi": "Hot and hazy.",
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
# -------------------------
|
| 18 |
+
# Simple Task Storage
|
| 19 |
+
# -------------------------
|
| 20 |
+
TASKS = []
|
| 21 |
+
|
| 22 |
+
def add_task(text):
|
| 23 |
+
text = text.strip()
|
| 24 |
+
TASKS.append(text)
|
| 25 |
+
return f"Task added: {text}"
|
| 26 |
+
|
| 27 |
+
def list_tasks():
|
| 28 |
+
if not TASKS:
|
| 29 |
+
return "No tasks added yet."
|
| 30 |
+
out = "Your Tasks:\n"
|
| 31 |
+
for i, t in enumerate(TASKS, 1):
|
| 32 |
+
out += f"{i}. {t}\n"
|
| 33 |
+
return out
|
| 34 |
+
|
| 35 |
+
# -------------------------
|
| 36 |
+
# Event Suggestions
|
| 37 |
+
# -------------------------
|
| 38 |
+
def suggest_events(weather):
|
| 39 |
+
if "rain" in weather.lower():
|
| 40 |
+
return "Suggested: Indoor games, reading, or a cozy movie."
|
| 41 |
+
if "sunny" in weather.lower():
|
| 42 |
+
return "Suggested: Morning walk, coffee meetup, light outdoor activity."
|
| 43 |
+
if "cloudy" in weather.lower():
|
| 44 |
+
return "Suggested: Visit a café, do a short workout, or relax outside."
|
| 45 |
+
return "Suggested: Normal day activities."
|
| 46 |
+
|
| 47 |
+
# -------------------------
|
| 48 |
+
# Day Planner
|
| 49 |
+
# -------------------------
|
| 50 |
+
def plan_day(city):
|
| 51 |
+
city_key = city.lower()
|
| 52 |
+
weather = OFFLINE_WEATHER.get(city_key, "Weather not found.")
|
| 53 |
+
|
| 54 |
+
plan = "📅 Your Day Plan\n"
|
| 55 |
+
plan += f"• Weather in {city.title()}: {weather}\n\n"
|
| 56 |
+
|
| 57 |
+
if TASKS:
|
| 58 |
+
plan += "📝 Your Tasks:\n"
|
| 59 |
+
for i, t in enumerate(TASKS, 1):
|
| 60 |
+
plan += f"{i}. {t}\n"
|
| 61 |
+
else:
|
| 62 |
+
plan += "No tasks added yet.\n"
|
| 63 |
+
|
| 64 |
+
plan += "\n🎯 Suggestions: " + suggest_events(weather)
|
| 65 |
+
|
| 66 |
+
return plan
|
| 67 |
+
|
| 68 |
+
# -------------------------
|
| 69 |
+
# Main Chat Logic
|
| 70 |
+
# -------------------------
|
| 71 |
+
def smartlife_agent(message):
|
| 72 |
+
msg = message.lower().strip()
|
| 73 |
+
|
| 74 |
+
# ADD TASK
|
| 75 |
+
if msg.startswith("add task"):
|
| 76 |
+
text = message.split(":", 1)[-1].strip() if ":" in message else message.replace("add task", "").strip()
|
| 77 |
+
return add_task(text)
|
| 78 |
+
|
| 79 |
+
# LIST TASKS
|
| 80 |
+
if "list tasks" in msg:
|
| 81 |
+
return list_tasks()
|
| 82 |
+
|
| 83 |
+
# WEATHER
|
| 84 |
+
if "weather" in msg:
|
| 85 |
+
for city in OFFLINE_WEATHER.keys():
|
| 86 |
+
if city in msg:
|
| 87 |
+
return f"Weather in {city.title()}: {OFFLINE_WEATHER[city]}"
|
| 88 |
+
return "City not found in offline weather database."
|
| 89 |
+
|
| 90 |
+
# PLAN MY DAY
|
| 91 |
+
if "plan my day" in msg or "day plan" in msg:
|
| 92 |
+
for city in OFFLINE_WEATHER.keys():
|
| 93 |
+
if city in msg:
|
| 94 |
+
return plan_day(city)
|
| 95 |
+
return "Tell me your city for planning your day."
|
| 96 |
+
|
| 97 |
+
# SUGGEST ACTIVITIES
|
| 98 |
+
if "suggest" in msg and "weather" in msg:
|
| 99 |
+
w = msg.replace("suggest activities for", "").strip()
|
| 100 |
+
return suggest_events(w)
|
| 101 |
+
|
| 102 |
+
return "I understood your message — but try commands like:\n• Add task: <task>\n• List tasks\n• Weather in Bengaluru\n• Plan my day in Bengaluru"
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
# -------------------------
|
| 106 |
+
# Gradio UI
|
| 107 |
+
# -------------------------
|
| 108 |
+
iface = gr.Interface(
|
| 109 |
+
fn=smartlife_agent,
|
| 110 |
+
inputs=gr.Textbox(label="SmartLife Assistant Agent"),
|
| 111 |
+
outputs=gr.Textbox(label="Response"),
|
| 112 |
+
title="🧠 SmartLife Assistant (Offline)",
|
| 113 |
+
description="Your personal offline day planner — weather, tasks, schedule & suggestions without any external API."
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|