lvvignesh2122 commited on
Commit
17de158
·
verified ·
1 Parent(s): 1f2154d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -125
app.py CHANGED
@@ -1,126 +1,116 @@
1
  import gradio as gr
2
- import datetime
3
- import time
4
- import random
5
-
6
- # ---------------------
7
- # GLOBAL MEMORY
8
- # ---------------------
9
- planner_tasks = []
10
- daily_tasks = []
11
- reminders = []
12
- timers = []
13
-
14
- # ---------------------
15
- # WEATHER TOOL (dummy)
16
- # ---------------------
17
- def get_weather(city):
18
- sample_weather = [
19
- "Sunny and warm",
20
- "Cloudy with light breeze",
21
- "Heavy rain expected",
22
- "Thunderstorms likely",
23
- "Clear skies and humid",
24
- "Mild and windy"
25
- ]
26
- condition = random.choice(sample_weather)
27
- return f"Weather in {city}: {condition}."
28
-
29
- # ---------------------
30
- # PLANNER TOOL
31
- # ---------------------
32
- def add_planner_task(task):
33
- planner_tasks.append(task)
34
- return f"Added to planner: {task}"
35
-
36
- def get_planner_tasks():
37
- if not planner_tasks:
38
- return "No planner tasks yet."
39
- return "\n".join(f"- {t}" for t in planner_tasks)
40
-
41
- # ---------------------
42
- # DAILY TASK TOOL
43
- # ---------------------
44
- def add_daily_task(task):
45
- daily_tasks.append(task)
46
- return f"Added daily task: {task}"
47
-
48
- def get_daily_tasks():
49
- if not daily_tasks:
50
- return "No daily tasks yet."
51
- return "\n".join(f"- {t}" for t in daily_tasks)
52
-
53
- # ---------------------
54
- # REMINDER TOOL
55
- # ---------------------
56
- def add_reminder(text, time_str):
57
- reminders.append((text, time_str))
58
- return f"Reminder set: {text} at {time_str}"
59
-
60
- # ---------------------
61
- # TIMER TOOL
62
- # ---------------------
63
- def start_timer(seconds):
64
- timers.append(seconds)
65
- return f"Timer started for {seconds} seconds."
66
-
67
- # ---------------------
68
- # MAIN AGENT LOGIC
69
- # ---------------------
70
- def agent_response(message):
71
- message_lower = message.lower()
72
-
73
- # weather
74
- if "weather" in message_lower:
75
- city = message.split("in")[-1].strip() if "in" in message_lower else "your city"
76
- return get_weather(city)
77
-
78
- # planner add
79
- if message_lower.startswith("add plan"):
80
- task = message.replace("add plan", "").strip()
81
- return add_planner_task(task)
82
-
83
- # planner view
84
- if "show plan" in message_lower or "planner" in message_lower:
85
- return get_planner_tasks()
86
-
87
- # daily tasks add
88
- if message_lower.startswith("add daily"):
89
- task = message.replace("add daily", "").strip()
90
- return add_daily_task(task)
91
-
92
- # daily tasks view
93
- if "show daily" in message_lower:
94
- return get_daily_tasks()
95
-
96
- # reminder
97
- if "remind me" in message_lower:
98
- parts = message.split(" at ")
99
- if len(parts) == 2:
100
- text = parts[0].replace("remind me", "").strip()
101
- time_str = parts[1].strip()
102
- return add_reminder(text, time_str)
103
- return "Format: remind me <task> at <time>"
104
-
105
- # timer
106
- if "timer" in message_lower:
107
- try:
108
- seconds = int("".join(filter(str.isdigit, message)))
109
- return start_timer(seconds)
110
- except:
111
- return "Please specify timer in seconds."
112
-
113
- return f"SmartLife Assistant: I understood your message '{message}'. How can I help next?"
114
-
115
- # ---------------------
116
- # GRADIO UI
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 plannerweather, tasks, schedule & suggestions without any external API."
114
+ )
115
+
116
+ iface.launch()