Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| # ------------------------- | |
| # Offline Weather Database | |
| # ------------------------- | |
| OFFLINE_WEATHER = { | |
| "bengaluru": "Sunny with mild breeze.", | |
| "bangalore": "Sunny with mild breeze.", | |
| "chitradurga": "Cloudy with light breeze.", | |
| "madurai": "Hot and dry.", | |
| "chennai": "Humid and warm.", | |
| "mumbai": "Humid with chance of rain.", | |
| "mysuru": "Cool and pleasant.", | |
| "delhi": "Hot and hazy.", | |
| } | |
| # ------------------------- | |
| # Simple Task Storage | |
| # ------------------------- | |
| TASKS = [] | |
| def add_task(text): | |
| text = text.strip() | |
| TASKS.append(text) | |
| return f"Task added: {text}" | |
| def list_tasks(): | |
| if not TASKS: | |
| return "No tasks added yet." | |
| out = "Your Tasks:\n" | |
| for i, t in enumerate(TASKS, 1): | |
| out += f"{i}. {t}\n" | |
| return out | |
| # ------------------------- | |
| # Event Suggestions | |
| # ------------------------- | |
| def suggest_events(weather): | |
| if "rain" in weather.lower(): | |
| return "Suggested: Indoor games, reading, or a cozy movie." | |
| if "sunny" in weather.lower(): | |
| return "Suggested: Morning walk, coffee meetup, light outdoor activity." | |
| if "cloudy" in weather.lower(): | |
| return "Suggested: Visit a café, do a short workout, or relax outside." | |
| return "Suggested: Normal day activities." | |
| # ------------------------- | |
| # Day Planner | |
| # ------------------------- | |
| def plan_day(city): | |
| city_key = city.lower() | |
| weather = OFFLINE_WEATHER.get(city_key, "Weather not found.") | |
| plan = "📅 Your Day Plan\n" | |
| plan += f"• Weather in {city.title()}: {weather}\n\n" | |
| if TASKS: | |
| plan += "📝 Your Tasks:\n" | |
| for i, t in enumerate(TASKS, 1): | |
| plan += f"{i}. {t}\n" | |
| else: | |
| plan += "No tasks added yet.\n" | |
| plan += "\n🎯 Suggestions: " + suggest_events(weather) | |
| return plan | |
| # ------------------------- | |
| # Main Chat Logic | |
| # ------------------------- | |
| def smartlife_agent(message): | |
| msg = message.lower().strip() | |
| # ADD TASK | |
| if msg.startswith("add task"): | |
| text = message.split(":", 1)[-1].strip() if ":" in message else message.replace("add task", "").strip() | |
| return add_task(text) | |
| # LIST TASKS | |
| if "list tasks" in msg: | |
| return list_tasks() | |
| # WEATHER | |
| if "weather" in msg: | |
| for city in OFFLINE_WEATHER.keys(): | |
| if city in msg: | |
| return f"Weather in {city.title()}: {OFFLINE_WEATHER[city]}" | |
| return "City not found in offline weather database." | |
| # PLAN MY DAY | |
| if "plan my day" in msg or "day plan" in msg: | |
| for city in OFFLINE_WEATHER.keys(): | |
| if city in msg: | |
| return plan_day(city) | |
| return "Tell me your city for planning your day." | |
| # SUGGEST ACTIVITIES | |
| if "suggest" in msg and "weather" in msg: | |
| w = msg.replace("suggest activities for", "").strip() | |
| return suggest_events(w) | |
| 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" | |
| # ------------------------- | |
| # Gradio UI | |
| # ------------------------- | |
| iface = gr.Interface( | |
| fn=smartlife_agent, | |
| inputs=gr.Textbox(label="SmartLife Assistant Agent"), | |
| outputs=gr.Textbox(label="Response"), | |
| title="🧠 SmartLife Assistant (Offline)", | |
| description="Your personal offline day planner — weather, tasks, schedule & suggestions without any external API." | |
| ) | |
| iface.launch() | |