Spaces:
Running
Running
File size: 5,869 Bytes
a31f556 | 1 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 82 83 84 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | """
modules/context_display.py — FRIDAY Contextual Display
Asks what's around and shows info in AR or normal mode.
"""
import os
import json
import time
from typing import Optional
from config import DATA_DIR
CONTEXT_FILE = os.path.join(DATA_DIR, "context_display.json")
def _load() -> dict:
"""Load context data."""
if not os.path.exists(CONTEXT_FILE):
return {"presets": {}, "current_view": "normal", "ar_enabled": False}
try:
with open(CONTEXT_FILE, "r", encoding="utf-8") as f:
return json.load(f)
except Exception:
return {"presets": {}, "current_view": "normal", "ar_enabled": False}
def _save(data: dict) -> None:
"""Save context data."""
try:
os.makedirs(DATA_DIR, exist_ok=True)
with open(CONTEXT_FILE, "w", encoding="utf-8") as f:
json.dump(data, f)
except Exception:
pass
# === View Presets ===
PRESETS = {
"system": ["CPU", "RAM", "GPU", "Temps", "Battery"],
"network": ["WiFi", "LAN", "VPN", "Ping"],
"health": ["Focus", "Breaks", "Eye strain", "Posture"],
"weather": ["Temperature", "Humidity", "Forecast"],
"calendar": ["Next meeting", "Events", "Free time"],
"news": ["Headlines", "Tech news", "Weather alert"],
"stocks": ["Portfolio", "Market", "Alerts"],
"security": ["Firewall", "Network", "Logins"],
}
def set_view(view: str, mode: str = "normal") -> str:
"""
Set what to display.
Args:
view: View name (system, network, health, etc)
mode: Display mode (ar or normal)
Returns:
Confirmation
"""
data = _load()
data["current_view"] = view
data["display_mode"] = mode
data["ar_enabled"] = mode == "ar"
_save(data)
mode_text = "AR" if mode == "ar" else "normal"
return f"Showing {view} in {mode_text} mode."
def get_view() -> dict:
"""Get current view settings."""
return _load()
def enable_ar() -> str:
"""Enable AR display mode."""
return set_view("system", "ar")
def disable_ar() -> str:
"""Disable AR display mode."""
data = _load()
data["ar_enabled"] = False
data["display_mode"] = "normal"
_save(data)
return "AR disabled. Back to normal display."
def ask_what_to_show() -> str:
"""FRIDAY asks what user wants to see."""
import random
questions = [
"What would you like to see, boss?",
"What info do you need?",
"Show you the system stats?",
"Want to see network status?",
"Check your health metrics?",
"See weather?",
"Display anything specific?",
]
return random.choice(questions)
def suggest_views() -> list[str]:
"""Suggest available views."""
return list(PRESETS.keys())
# === Build Display Content ===
def get_system_display() -> str:
"""Get system info for display."""
import psutil
import platform
cpu = psutil.cpu_percent()
ram = psutil.virtual_memory()
battery = ""
try:
b = psutil.sensors_battery()
if b:
battery = f"{b.percent}%"
except Exception:
pass
return f"CPU: {cpu}% | RAM: {ram.percent}% | {battery}"
def get_network_display() -> str:
"""Get network info for display."""
import psutil
net = psutil.net_io_counters()
return f"Download: {net.bytes_recv/1024/1024:.1f}MB | Upload: {net.bytes_sent/1024/1024:.1f}MB"
def get_health_display() -> str:
"""Get health info for display."""
try:
from modules.health_monitor import get_health_summary
summary = get_health_summary()
focus = summary.get("total_focus_hours", 0)
breaks = summary.get("breaks_taken", 0)
return f"Focus: {focus:.1f}h | Breaks: {breaks}"
except Exception:
return "No health data"
def get_weather_display() -> str:
"""Get weather info for display."""
try:
from modules.weather import get_weather
w = get_weather()
return w[:100] if w else "Weather unavailable"
except Exception:
return "Weather unavailable"
def build_display_content() -> str:
"""Build content based on current view."""
data = _load()
view = data.get("current_view", "system")
if view == "system":
return get_system_display()
elif view == "network":
return get_network_display()
elif view == "health":
return get_health_display()
elif view == "weather":
return get_weather_display()
else:
return f"View: {view}"
# === Voice Commands ===
def handle_command(command: str, speak) -> bool:
"""Handle context display commands."""
c = command.lower()
# Ask what to show
if "what do you see" in c or "what's around" in c or "what's happening" in c:
result = ask_what_to_show()
speak(result)
return True
# Enable AR
if _has(c, ["show in ar", "ar display", "enable ar view"]):
try:
enable_ar()
speak("AR enabled. What do you want to see?")
except Exception as e:
speak(f"AR error: {e}")
return True
# Disable AR
if "disable ar" in c or "normal mode" in c:
result = disable_ar()
speak(result)
return True
# Show specific view
for view in PRESETS.keys():
if f"show {view}" in c or f"display {view}" in c:
mode = "ar" if "ar" in c else "normal"
result = set_view(view, mode)
speak(result)
return True
return False
def _has(text: str, words: list) -> bool:
"""Check if any word in text."""
return any(w in text for w in words) |