Spaces:
Sleeping
Sleeping
Download smart-farm-ui.html from SheSync/meta-hackathon-project: direct link, hf CLI and curl.
- Browser
- Download file 34.4 kB
-
https://huggingface.co/spaces/SheSync/meta-hackathon-project/resolve/main/smart-farm-ui.html
- Command line
-
hf download hf://spaces/SheSync/meta-hackathon-project/smart-farm-ui.html
-
curl -L -o smart-farm-ui.html https://huggingface.co/spaces/SheSync/meta-hackathon-project/resolve/main/smart-farm-ui.html
34.4 kB
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Smart Farm Irrigation AI</title> | |
| <link href="https://fonts.googleapis.com/css2?family=Playfair+Display:wght@700;900&family=DM+Sans:wght@300;400;500;600&display=swap" rel="stylesheet"> | |
| <style> | |
| :root { | |
| --green-dark: #1a3d1f; --green-mid: #2d6a35; --green-light: #4caf50; | |
| --green-pale: #e8f5e9; --cream: #fdf6ec; --white: #ffffff; | |
| --text: #1a2e1c; --text-light: #5a7a5c; --card-shadow: 0 4px 20px rgba(26,61,31,0.10); | |
| } | |
| * { margin:0; padding:0; box-sizing:border-box; } | |
| body { font-family:'DM Sans',sans-serif; background:var(--cream); color:var(--text); min-height:100vh; } | |
| header { | |
| background:linear-gradient(135deg,var(--green-dark) 0%,var(--green-mid) 60%,#3d8b45 100%); | |
| color:white; padding:28px 20px 36px; text-align:center; position:relative; overflow:hidden; | |
| } | |
| header::before { | |
| content:''; position:absolute; bottom:-2px; left:0; right:0; | |
| height:40px; background:var(--cream); border-radius:50% 50% 0 0/100% 100% 0 0; | |
| } | |
| .header-bg-dots { | |
| position:absolute; top:0; left:0; right:0; bottom:0; | |
| background-image:radial-gradient(circle,rgba(255,255,255,0.07) 1px,transparent 1px); | |
| background-size:24px 24px; pointer-events:none; | |
| } | |
| header h1 { font-family:'Playfair Display',serif; font-size:clamp(1.5rem,5vw,2.4rem); font-weight:900; letter-spacing:-0.5px; position:relative; z-index:1; } | |
| header h1 span { color:#a5d6a7; } | |
| .badges { display:flex; justify-content:center; gap:8px; margin-top:14px; flex-wrap:wrap; position:relative; z-index:1; } | |
| .badge { background:rgba(255,255,255,0.15); border:1px solid rgba(255,255,255,0.25); color:white; font-size:0.72rem; padding:4px 12px; border-radius:20px; font-weight:500; } | |
| .container { max-width:480px; margin:0 auto; padding:20px 16px 40px; } | |
| .card { background:white; border-radius:18px; padding:18px; box-shadow:var(--card-shadow); margin-bottom:16px; border:1.5px solid #e0f0e1; } | |
| .card-label { font-size:0.78rem; font-weight:600; color:var(--text-light); letter-spacing:0.8px; text-transform:uppercase; margin-bottom:12px; } | |
| .city-row { display:flex; gap:10px; } | |
| .city-input { flex:1; border:1.5px solid #d0e8d2; border-radius:12px; padding:11px 14px; font-family:'DM Sans',sans-serif; font-size:0.95rem; color:var(--text); background:var(--green-pale); outline:none; transition:border 0.2s; } | |
| .city-input:focus { border-color:var(--green-light); } | |
| .start-btn { background:linear-gradient(135deg,var(--green-light),var(--green-mid)); color:white; border:none; border-radius:12px; padding:11px 20px; font-family:'DM Sans',sans-serif; font-size:0.9rem; font-weight:600; cursor:pointer; transition:transform 0.15s,box-shadow 0.15s; box-shadow:0 3px 12px rgba(76,175,80,0.35); white-space:nowrap; } | |
| .start-btn:hover { transform:translateY(-1px); } | |
| .start-btn:disabled { opacity:0.6; cursor:not-allowed; transform:none; } | |
| .task-selector { display:grid; grid-template-columns:repeat(3,1fr); gap:10px; } | |
| .task-btn { border-radius:14px; padding:14px 8px; text-align:center; cursor:pointer; border:2px solid #e0f0e1; background:var(--green-pale); transition:all 0.2s; font-family:'DM Sans',sans-serif; } | |
| .task-btn:hover { transform:translateY(-2px); box-shadow:0 4px 14px rgba(76,175,80,0.2); } | |
| .task-btn.selected { border-color:var(--green-mid); background:var(--green-mid); color:white; transform:translateY(-2px); box-shadow:0 4px 14px rgba(45,106,53,0.3); } | |
| .task-btn.selected .task-btn-diff, .task-btn.selected .task-btn-days { color:rgba(255,255,255,0.75); } | |
| .task-btn-icon { font-size:1.6rem; margin-bottom:6px; } | |
| .task-btn-name { font-size:0.82rem; font-weight:600; display:block; } | |
| .task-btn-diff { font-size:0.68rem; color:var(--text-light); display:block; margin-top:2px; } | |
| .task-btn-days { font-size:0.65rem; color:var(--text-light); display:block; margin-top:2px; } | |
| .run-btn { width:100%; background:linear-gradient(135deg,var(--green-light),var(--green-mid)); color:white; border:none; border-radius:14px; padding:15px; font-family:'DM Sans',sans-serif; font-size:1rem; font-weight:700; cursor:pointer; box-shadow:0 4px 16px rgba(76,175,80,0.35); transition:all 0.2s; letter-spacing:0.3px; margin-top:14px; } | |
| .run-btn:hover { transform:translateY(-1px); box-shadow:0 6px 20px rgba(76,175,80,0.45); } | |
| .run-btn:disabled { opacity:0.6; cursor:not-allowed; transform:none; } | |
| .stats-grid { display:grid; grid-template-columns:1fr 1fr; gap:12px; margin-bottom:16px; } | |
| .stat-card { background:white; border-radius:16px; padding:16px; box-shadow:var(--card-shadow); border:1.5px solid #e8f5e9; position:relative; overflow:hidden; } | |
| .stat-card::before { content:''; position:absolute; top:0; left:0; right:0; height:3px; border-radius:16px 16px 0 0; } | |
| .stat-card.temp::before { background:linear-gradient(90deg,#ff8a65,#ffa726); } | |
| .stat-card.rain::before { background:linear-gradient(90deg,#42a5f5,#26c6da); } | |
| .stat-card.moisture::before { background:linear-gradient(90deg,#26a69a,#66bb6a); } | |
| .stat-card.health::before { background:linear-gradient(90deg,#ab47bc,#7e57c2); } | |
| .stat-icon { font-size:1.5rem; margin-bottom:4px; } | |
| .stat-label { font-size:0.72rem; font-weight:600; color:var(--text-light); letter-spacing:0.5px; text-transform:uppercase; } | |
| .stat-value { font-family:'Playfair Display',serif; font-size:1.55rem; font-weight:700; color:var(--text); line-height:1.1; margin-top:2px; } | |
| .stat-unit { font-size:0.8rem; font-weight:400; color:var(--text-light); } | |
| .health-badge { display:inline-block; margin-top:6px; font-size:0.75rem; font-weight:600; padding:3px 10px; border-radius:20px; background:var(--green-pale); color:var(--green-mid); border:1px solid #c8e6c9; text-transform:capitalize; } | |
| .moisture-bar-wrap { margin-top:6px; height:6px; background:#e8f5e9; border-radius:10px; overflow:hidden; } | |
| .moisture-bar-fill { height:100%; border-radius:10px; background:linear-gradient(90deg,#42a5f5,#26a69a); width:0%; transition:width 0.6s cubic-bezier(.4,0,.2,1); } | |
| .day-header { display:flex; justify-content:space-between; align-items:center; margin-bottom:12px; } | |
| .day-number { font-family:'Playfair Display',serif; font-size:1.1rem; font-weight:700; } | |
| .stage-pill { background:var(--green-pale); color:var(--green-mid); font-size:0.78rem; font-weight:600; padding:4px 12px; border-radius:20px; border:1px solid #c8e6c9; } | |
| .day-progress-bar { height:10px; background:#e8f5e9; border-radius:10px; overflow:hidden; } | |
| .day-progress-fill { height:100%; border-radius:10px; background:linear-gradient(90deg,var(--green-light),var(--green-mid)); width:0%; transition:width 0.6s; } | |
| .day-markers { display:flex; justify-content:space-between; margin-top:6px; } | |
| .day-marker { font-size:0.68rem; color:var(--text-light); } | |
| .stage-markers { display:flex; gap:6px; margin-top:10px; } | |
| .stage-marker { flex:1; text-align:center; font-size:0.68rem; padding:4px 0; border-radius:8px; font-weight:500; } | |
| .stage-marker.seedling { background:#e8f5e9; color:#388e3c; } | |
| .stage-marker.growing { background:#e3f2fd; color:#1565c0; } | |
| .stage-marker.mature { background:#fff3e0; color:#e65100; } | |
| .stage-marker.active { font-weight:700; box-shadow:0 2px 8px rgba(0,0,0,0.10); } | |
| .ai-thinking-row { display:flex; align-items:center; gap:10px; margin-bottom:12px; } | |
| .ai-pulse { width:10px; height:10px; border-radius:50%; background:var(--green-light); flex-shrink:0; } | |
| .ai-pulse.active { animation:pulse 1s ease-in-out infinite; } | |
| @keyframes pulse { 0%,100%{opacity:1;transform:scale(1)} 50%{opacity:0.4;transform:scale(0.8)} } | |
| .ai-thinking-text { font-size:0.85rem; color:var(--text-light); font-weight:500; } | |
| .ai-action-display { display:grid; grid-template-columns:repeat(4,1fr); gap:8px; margin-bottom:12px; } | |
| .ai-action-btn { border-radius:14px; padding:12px 4px; text-align:center; border:2px solid #e0f0e1; background:var(--green-pale); font-family:'DM Sans',sans-serif; transition:all 0.3s; } | |
| .ai-action-btn .btn-icon { font-size:1.4rem; display:block; margin-bottom:4px; } | |
| .ai-action-btn .btn-label { font-size:0.72rem; font-weight:600; display:block; } | |
| .ai-action-btn .btn-mm { font-size:0.65rem; color:var(--text-light); display:block; margin-top:1px; } | |
| .ai-action-btn.chosen { border-color:var(--green-mid); background:var(--green-mid); color:white; transform:translateY(-3px); box-shadow:0 6px 16px rgba(45,106,53,0.35); } | |
| .ai-action-btn.chosen .btn-mm { color:rgba(255,255,255,0.7); } | |
| .ai-reason-box { background:var(--green-pale); border-radius:10px; padding:10px 14px; font-size:0.82rem; color:var(--text); line-height:1.5; border-left:3px solid var(--green-light); min-height:40px; } | |
| .result-score-row { display:flex; align-items:center; gap:14px; margin-bottom:12px; } | |
| .score-circle { width:64px; height:64px; border-radius:50%; background:linear-gradient(135deg,var(--green-light),var(--green-mid)); display:flex; align-items:center; justify-content:center; flex-shrink:0; box-shadow:0 4px 14px rgba(76,175,80,0.35); transition:background 0.4s; } | |
| .score-number { font-family:'Playfair Display',serif; font-size:1.35rem; font-weight:900; color:white; } | |
| .score-info { flex:1; } | |
| .score-stars { font-size:1.1rem; margin-bottom:4px; } | |
| .score-message { font-size:0.88rem; color:var(--text); font-weight:500; line-height:1.4; } | |
| .result-detail { background:var(--green-pale); border-radius:10px; padding:10px 14px; font-size:0.8rem; color:var(--text-light); line-height:1.5; } | |
| .log-header-row { display:grid; grid-template-columns:50px 1fr 65px 55px; font-size:0.7rem; font-weight:600; color:var(--text-light); padding:0 6px 6px; gap:4px; } | |
| .log-scroll { max-height:180px; overflow-y:auto; display:flex; flex-direction:column; gap:4px; } | |
| .log-row { display:grid; grid-template-columns:50px 1fr 65px 55px; font-size:0.75rem; padding:5px 6px; border-radius:8px; background:var(--green-pale); align-items:center; gap:4px; animation:slideIn 0.3s ease both; } | |
| @keyframes slideIn { from{opacity:0;transform:translateX(-6px)} to{opacity:1;transform:translateX(0)} } | |
| .log-day { color:var(--text-light); font-weight:600; } | |
| .log-action { font-weight:700; text-align:center; padding:2px 6px; border-radius:6px; font-size:0.7rem; } | |
| .log-action.none { background:#f5f5f5; color:#666; } | |
| .log-action.low { background:#e3f2fd; color:#1565c0; } | |
| .log-action.medium { background:#e8f5e9; color:#2e7d32; } | |
| .log-action.high { background:#fce4ec; color:#c62828; } | |
| .log-moisture { color:var(--text); text-align:center; } | |
| .log-reward { font-weight:700; text-align:right; } | |
| .log-reward.good { color:#2e7d32; } | |
| .log-reward.ok { color:#e65100; } | |
| .log-reward.bad { color:#c62828; } | |
| .scores-header { display:flex; justify-content:space-between; align-items:center; margin-bottom:16px; } | |
| .task-row { margin-bottom:14px; } | |
| .task-row:last-child { margin-bottom:0; } | |
| .task-row-header { display:flex; justify-content:space-between; align-items:center; margin-bottom:6px; } | |
| .task-name { font-size:0.82rem; font-weight:500; } | |
| .task-diff { font-size:0.68rem; padding:2px 8px; border-radius:10px; font-weight:600; } | |
| .task-diff.easy { background:#e8f5e9; color:#388e3c; } | |
| .task-diff.medium { background:#fff3e0; color:#e65100; } | |
| .task-diff.hard { background:#fce4ec; color:#c62828; } | |
| .task-score-bar { height:8px; background:#e8f5e9; border-radius:10px; overflow:hidden; } | |
| .task-score-fill { height:100%; border-radius:10px; transition:width 0.8s cubic-bezier(.4,0,.2,1); width:0%; } | |
| .task-score-fill.t1 { background:linear-gradient(90deg,#66bb6a,#43a047); } | |
| .task-score-fill.t2 { background:linear-gradient(90deg,#ffa726,#fb8c00); } | |
| .task-score-fill.t3 { background:linear-gradient(90deg,#ef5350,#e53935); } | |
| .task-score-num { font-size:0.72rem; color:var(--text-light); text-align:right; margin-top:3px; font-weight:600; } | |
| .error-banner { display:none; background:#fce4ec; color:#c62828; border:1.5px solid #ef9a9a; border-radius:12px; padding:12px 16px; margin-bottom:14px; font-size:0.85rem; font-weight:500; text-align:center; } | |
| footer { text-align:center; padding:24px 16px; font-size:0.75rem; color:var(--text-light); border-top:1px solid #e0f0e1; } | |
| footer strong { color:var(--green-mid); } | |
| @keyframes fadeUp { from{opacity:0;transform:translateY(16px)} to{opacity:1;transform:translateY(0)} } | |
| .card { animation:fadeUp 0.4s ease both; } | |
| .stats-grid { animation:fadeUp 0.4s 0.08s ease both; } | |
| .day-card { animation:fadeUp 0.4s 0.16s ease both; } | |
| .ai-card { animation:fadeUp 0.4s 0.24s ease both; } | |
| .result-card { animation:fadeUp 0.4s 0.32s ease both; } | |
| .log-card { animation:fadeUp 0.4s 0.36s ease both; } | |
| .scores-card { animation:fadeUp 0.4s 0.40s ease both; } | |
| @media (min-width:600px) { | |
| .container { padding:28px 24px 60px; } | |
| .stats-grid { grid-template-columns:repeat(4,1fr); } | |
| header { padding:40px 20px 50px; } | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <header> | |
| <div class="header-bg-dots"></div> | |
| <h1>πΎ Smart Farm <span>Irrigation AI</span></h1> | |
| <div class="badges"> | |
| <span class="badge">π€ AI Agent</span> | |
| <span class="badge">π Live Weather</span> | |
| </div> | |
| </header> | |
| <div class="container"> | |
| <div class="error-banner" id="errorBanner"></div> | |
| <!-- CITY --> | |
| <div class="card"> | |
| <div class="card-label">π Your Farm Location</div> | |
| <div class="city-row"> | |
| <input class="city-input" type="text" placeholder="Enter city (e.g. Indore, Mumbai...)" value="Indore" id="cityInput"/> | |
| <button class="start-btn" id="startBtn" onclick="initFarm()">Load Weather π€οΈ</button> | |
| </div> | |
| </div> | |
| <!-- STATS --> | |
| <div class="stats-grid"> | |
| <div class="stat-card temp"> | |
| <div class="stat-icon">π‘οΈ</div> | |
| <div class="stat-label">Temperature</div> | |
| <div class="stat-value" id="tempVal">--<span class="stat-unit">Β°C</span></div> | |
| </div> | |
| <div class="stat-card rain"> | |
| <div class="stat-icon">π§οΈ</div> | |
| <div class="stat-label">Rainfall</div> | |
| <div class="stat-value" id="rainVal">--<span class="stat-unit">mm</span></div> | |
| </div> | |
| <div class="stat-card moisture"> | |
| <div class="stat-icon">π§</div> | |
| <div class="stat-label">Soil Moisture</div> | |
| <div class="stat-value" id="moistureVal">--<span class="stat-unit">%</span></div> | |
| <div class="moisture-bar-wrap"><div class="moisture-bar-fill" id="moistureBar"></div></div> | |
| </div> | |
| <div class="stat-card health"> | |
| <div class="stat-icon">π±</div> | |
| <div class="stat-label">Crop Health</div> | |
| <div class="stat-value" id="healthVal" style="font-size:1.1rem;margin-top:4px">--</div> | |
| <span class="health-badge" id="healthBadge">--</span> | |
| </div> | |
| </div> | |
| <!-- TASK SELECTOR --> | |
| <div class="card"> | |
| <div class="card-label">π― Select Task β AI Agent will run it automatically</div> | |
| <div class="task-selector"> | |
| <div class="task-btn selected" id="tbtn1" onclick="selectTask(1)"> | |
| <div class="task-btn-icon">π±</div> | |
| <span class="task-btn-name">Easy</span> | |
| <span class="task-btn-diff">Basic Moisture</span> | |
| <span class="task-btn-days">7 days</span> | |
| </div> | |
| <div class="task-btn" id="tbtn2" onclick="selectTask(2)"> | |
| <div class="task-btn-icon">πΏ</div> | |
| <span class="task-btn-name">Medium</span> | |
| <span class="task-btn-diff">Weather Adaptive</span> | |
| <span class="task-btn-days">14 days</span> | |
| </div> | |
| <div class="task-btn" id="tbtn3" onclick="selectTask(3)"> | |
| <div class="task-btn-icon">πΎ</div> | |
| <span class="task-btn-name">Hard</span> | |
| <span class="task-btn-diff">Full Season</span> | |
| <span class="task-btn-days">30 days</span> | |
| </div> | |
| </div> | |
| <button class="run-btn" id="runBtn" onclick="runAIAgent()">π€ Run AI Agent</button> | |
| </div> | |
| <!-- DAY PROGRESS --> | |
| <div class="card day-card"> | |
| <div class="day-header"> | |
| <div class="day-number">π Day <span id="dayNum">0</span> / <span id="dayMax">7</span></div> | |
| <div class="stage-pill" id="stagePill">π± Seedling</div> | |
| </div> | |
| <div class="day-progress-bar"><div class="day-progress-fill" id="dayBar"></div></div> | |
| <div class="day-markers"> | |
| <span class="day-marker">Day 1</span> | |
| <span class="day-marker" id="midMarker">Day 4</span> | |
| <span class="day-marker" id="endMarker">Day 7</span> | |
| </div> | |
| <div class="stage-markers"> | |
| <div class="stage-marker seedling active" id="sm1">π± Seedling<br><span style="font-size:0.6rem">Day 1-10</span></div> | |
| <div class="stage-marker growing" id="sm2">πΏ Growing<br><span style="font-size:0.6rem">Day 11-20</span></div> | |
| <div class="stage-marker mature" id="sm3">πΎ Mature<br><span style="font-size:0.6rem">Day 21-30</span></div> | |
| </div> | |
| </div> | |
| <!-- AI DECISION --> | |
| <div class="card ai-card"> | |
| <div class="card-label">π€ AI Agent Decision</div> | |
| <div class="ai-thinking-row"> | |
| <div class="ai-pulse" id="aiPulse"></div> | |
| <div class="ai-thinking-text" id="aiThinkingText">Waiting to start β select a task and click Run AI Agent.</div> | |
| </div> | |
| <div class="ai-action-display"> | |
| <div class="ai-action-btn" id="abtn-none"> | |
| <span class="btn-icon">π«</span> | |
| <span class="btn-label">None</span> | |
| <span class="btn-mm">0mm</span> | |
| </div> | |
| <div class="ai-action-btn" id="abtn-low"> | |
| <span class="btn-icon">πΏ</span> | |
| <span class="btn-label">Low</span> | |
| <span class="btn-mm">10mm</span> | |
| </div> | |
| <div class="ai-action-btn" id="abtn-medium"> | |
| <span class="btn-icon">π</span> | |
| <span class="btn-label">Medium</span> | |
| <span class="btn-mm">25mm</span> | |
| </div> | |
| <div class="ai-action-btn" id="abtn-high"> | |
| <span class="btn-icon">β²</span> | |
| <span class="btn-label">High</span> | |
| <span class="btn-mm">50mm</span> | |
| </div> | |
| </div> | |
| <div class="ai-reason-box" id="aiReasonBox">The AI agent will analyse soil moisture, temperature and rainfall every day and decide the best watering action automatically.</div> | |
| </div> | |
| <!-- TODAY'S RESULT --> | |
| <div class="card result-card"> | |
| <div class="card-label">π Today's Result</div> | |
| <div class="result-score-row"> | |
| <div class="score-circle" id="scoreCircle"> | |
| <span class="score-number" id="rewardNum">β</span> | |
| </div> | |
| <div class="score-info"> | |
| <div class="score-stars" id="rewardStars">β Ready</div> | |
| <div class="score-message" id="rewardMsg">Run the AI agent to see live results.</div> | |
| </div> | |
| </div> | |
| <div class="result-detail" id="resultDetail">Each day the AI observes the farm state and decides the optimal water amount. Watch it go! πΎ</div> | |
| </div> | |
| <!-- LIVE LOG --> | |
| <div class="card log-card"> | |
| <div class="card-label">π Live Decision Log</div> | |
| <div class="log-header-row"> | |
| <span>Day</span><span>AI Decision</span><span style="text-align:center">Moisture</span><span style="text-align:right">Reward</span> | |
| </div> | |
| <div class="log-scroll" id="logScroll"></div> | |
| </div> | |
| <!-- TASK SCORES --> | |
| <div class="card scores-card"> | |
| <div class="scores-header"> | |
| <div class="card-label" style="margin:0">π Task Scores</div> | |
| </div> | |
| <div class="task-row"> | |
| <div class="task-row-header"> | |
| <span class="task-name">Task 1 β Basic Moisture Control</span> | |
| <span class="task-diff easy">Easy</span> | |
| </div> | |
| <div class="task-score-bar"><div class="task-score-fill t1" id="fill1"></div></div> | |
| <div class="task-score-num" id="score1">β / 1.0</div> | |
| </div> | |
| <div class="task-row"> | |
| <div class="task-row-header"> | |
| <span class="task-name">Task 2 β Weather Adaptive</span> | |
| <span class="task-diff medium">Medium</span> | |
| </div> | |
| <div class="task-score-bar"><div class="task-score-fill t2" id="fill2"></div></div> | |
| <div class="task-score-num" id="score2">β / 1.0</div> | |
| </div> | |
| <div class="task-row"> | |
| <div class="task-row-header"> | |
| <span class="task-name">Task 3 β Full Season (30 Days)</span> | |
| <span class="task-diff hard">Hard</span> | |
| </div> | |
| <div class="task-score-bar"><div class="task-score-fill t3" id="fill3"></div></div> | |
| <div class="task-score-num" id="score3">β / 1.0</div> | |
| </div> | |
| </div>const | |
| </div> | |
| <footer> | |
| Built with β€οΈ for <strong>Meta Γ Hugging Face Γ Scaler SST</strong> OpenEnv Hackathon 2026 | |
| </footer> | |
| <script> | |
| BACKEND_URL = window.location.hostname; | |
| let selectedTask = 1; | |
| let currentDay = 0; | |
| let moisture = 0; | |
| let episodeHistory = []; | |
| let isRunning = false; | |
| const TASK_MAX = { 1:7, 2:14, 3:30 }; | |
| // ββ AI agent logic β mirrors baseline.py exactly ββ | |
| // Decides water amount based on REAL soil_moisture and rainfall from backend | |
| function aiDecide(obs) { | |
| const m = parseFloat(obs.soil_moisture ?? 50); | |
| const r = parseFloat(obs.rainfall ?? 0); | |
| if (r > 15) return 'none'; // heavy rain β no need to water | |
| if (m < 40) return 'high'; // critically dry | |
| if (m < 55) return 'medium'; // moderately dry | |
| if (m < 65) return 'low'; // slightly dry | |
| return 'none'; // moisture is fine | |
| } | |
| function aiReason(action, obs) { | |
| const m = parseFloat(obs.soil_moisture ?? 0).toFixed(1); | |
| const r = parseFloat(obs.rainfall ?? 0); | |
| const stage = obs.crop_stage ?? 'seedling'; | |
| if (r > 15) return `It rained ${r}mm today β crops are getting natural water. AI skips irrigation to conserve resources. π§`; | |
| if (action === 'high') return `Moisture is ${m}% β critically low! AI applies high water (50mm) to save the crop. π¨`; | |
| if (action === 'medium') return `Moisture is ${m}% β below ideal for ${stage} stage. AI chooses medium watering (25mm). π`; | |
| if (action === 'low') return `Moisture is ${m}% β slightly dry. AI gives a light top-up of 10mm. πΏ`; | |
| return `Moisture is ${m}% β already in the ideal range for ${stage} stage. AI skips watering today. β `; | |
| } | |
| function showError(msg) { | |
| const b = document.getElementById('errorBanner'); | |
| b.textContent = 'β οΈ ' + msg; | |
| b.style.display = 'block'; | |
| setTimeout(() => { b.style.display = 'none'; }, 7000); | |
| } | |
| function selectTask(id) { | |
| if (isRunning) return; | |
| selectedTask = id; | |
| [1,2,3].forEach(i => document.getElementById('tbtn'+i).classList.toggle('selected', i===id)); | |
| const max = TASK_MAX[id]; | |
| document.getElementById('dayMax').textContent = max; | |
| document.getElementById('endMarker').textContent = 'Day ' + max; | |
| document.getElementById('midMarker').textContent = 'Day ' + Math.round(max/2); | |
| updateDayUI(0, max); | |
| } | |
| async function initFarm() { | |
| const city = document.getElementById('cityInput').value.trim() || 'Indore'; | |
| const btn = document.getElementById('startBtn'); | |
| btn.disabled = true; btn.textContent = 'β³ Loading...'; | |
| try { | |
| const res = await fetch(`${BACKEND_URL}/reset`, { | |
| method:'POST', headers:{'Content-Type':'application/json'}, | |
| body: JSON.stringify({ city, crop_health:'fair' }) | |
| }); | |
| if (!res.ok) throw new Error('Server returned ' + res.status); | |
| const data = await res.json(); | |
| const obs = data.observation ?? data; | |
| moisture = parseFloat(obs.soil_moisture ?? 45); | |
| document.getElementById('tempVal').innerHTML = (obs.temperature ?? '--') + '<span class="stat-unit">Β°C</span>'; | |
| document.getElementById('rainVal').innerHTML = (obs.rainfall ?? '--') + '<span class="stat-unit">mm</span>'; | |
| document.getElementById('healthVal').textContent = obs.crop_health ?? 'fair'; | |
| document.getElementById('healthBadge').textContent = obs.crop_health ?? 'fair'; | |
| updateMoistureUI(moisture); | |
| document.getElementById('aiThinkingText').textContent = `Weather loaded for ${city}. Select a task and run the AI!`; | |
| } catch(err) { | |
| showError('Cannot reach backend. Is the server running at localhost:8000? (' + err.message + ')'); | |
| } finally { | |
| btn.disabled = false; btn.textContent = 'Load Weather π€οΈ'; | |
| } | |
| } | |
| async function runAIAgent() { | |
| if (isRunning) return; | |
| isRunning = true; | |
| episodeHistory = []; | |
| const city = document.getElementById('cityInput').value.trim() || 'Indore'; | |
| const maxDay = TASK_MAX[selectedTask]; | |
| const runBtn = document.getElementById('runBtn'); | |
| runBtn.disabled = true; runBtn.textContent = 'β³ AI is running...'; | |
| document.getElementById('logScroll').innerHTML = ''; | |
| [1,2,3].forEach(i => document.getElementById('tbtn'+i).style.pointerEvents = 'none'); | |
| // ββ FIX 1: declare lastObs before the try block so it's accessible in the loop ββ | |
| let lastObs = null; | |
| // Reset environment | |
| try { | |
| const res = await fetch(`${BACKEND_URL}/reset`, { | |
| method:'POST', headers:{'Content-Type':'application/json'}, | |
| body: JSON.stringify({ city, crop_health:'fair' }) | |
| }); | |
| if (!res.ok) throw new Error('Reset failed: ' + res.status); | |
| const data = await res.json(); | |
| const obs = data.observation ?? data; | |
| moisture = parseFloat(obs.soil_moisture ?? 45); | |
| currentDay = 1; | |
| updateMoistureUI(moisture); | |
| updateDayUI(1, maxDay); | |
| document.getElementById('tempVal').innerHTML = (obs.temperature ?? '--') + '<span class="stat-unit">Β°C</span>'; | |
| document.getElementById('rainVal').innerHTML = (obs.rainfall ?? '--') + '<span class="stat-unit">mm</span>'; | |
| document.getElementById('healthVal').textContent = obs.crop_health ?? 'fair'; | |
| document.getElementById('healthBadge').textContent = obs.crop_health ?? 'fair'; | |
| // ββ FIX 2: save the reset observation as lastObs INSIDE the try block ββ | |
| lastObs = obs; | |
| } catch(err) { | |
| showError('Could not start: ' + err.message); | |
| resetRunState(); return; | |
| } | |
| // Run day by day | |
| for (let day = 1; day <= maxDay; day++) { | |
| currentDay = day; | |
| // Thinking animation | |
| setAIThinking(true, `Day ${day} of ${maxDay} β AI is analysing soil moisture and weather...`); | |
| clearChosenBtn(); | |
| await sleep(700); | |
| // ββ FIX 3: use lastObs directly β real data from backend, no fallback to defaults ββ | |
| const obs = lastObs ?? { soil_moisture: moisture, rainfall: 0, crop_stage: 'seedling' }; | |
| // AI decides based on REAL moisture and rainfall | |
| const action = aiDecide(obs); | |
| const reason = aiReason(action, obs); | |
| setAIThinking(false, `Day ${day} β AI chose: ${action.toUpperCase()}`); | |
| highlightAIChoice(action); | |
| document.getElementById('aiReasonBox').textContent = reason; | |
| await sleep(800); | |
| // Send action to backend | |
| let result; | |
| try { | |
| const res = await fetch(`${BACKEND_URL}/step`, { | |
| method:'POST', headers:{'Content-Type':'application/json'}, | |
| body: JSON.stringify({ action:{ water_amount: action } }) | |
| }); | |
| if (!res.ok) throw new Error('Step failed on day ' + day); | |
| result = await res.json(); | |
| } catch(err) { | |
| showError(err.message); break; | |
| } | |
| const newObs = result.observation ?? result; | |
| // ββ FIX 4: update lastObs with the fresh response so next day uses real data ββ | |
| lastObs = newObs; | |
| const reward = result.reward ?? 0; | |
| const done = result.done ?? false; | |
| // Update all UI cards | |
| moisture = parseFloat(newObs.soil_moisture ?? moisture); | |
| updateMoistureUI(moisture); | |
| updateDayUI(day, maxDay); | |
| document.getElementById('tempVal').innerHTML = (newObs.temperature ?? '--') + '<span class="stat-unit">Β°C</span>'; | |
| document.getElementById('rainVal').innerHTML = (newObs.rainfall ?? '--') + '<span class="stat-unit">mm</span>'; | |
| document.getElementById('healthVal').textContent = newObs.crop_health ?? 'fair'; | |
| document.getElementById('healthBadge').textContent = newObs.crop_health ?? 'fair'; | |
| const actionMap = { none:0, low:1, medium:2, high:3 }; | |
| episodeHistory.push({ | |
| day, | |
| soil_moisture: moisture, | |
| crop_health: newObs.crop_health ?? 'fair', | |
| water_action: actionMap[action] ?? 0, | |
| rainfall: parseFloat(newObs.rainfall ?? 0), | |
| temperature: parseFloat(newObs.temperature ?? 30), | |
| crop_stage: newObs.crop_stage ?? 'seedling', | |
| }); | |
| displayReward(reward, moisture, parseFloat(newObs.rainfall ?? 0)); | |
| addLogRow(day, action, moisture, reward); | |
| if (done) break; | |
| await sleep(600); | |
| } | |
| // Get final score from /grader | |
| setAIThinking(false, 'Season complete! Calculating final score...'); | |
| await sleep(400); | |
| await getGraderScore(); | |
| resetRunState(); | |
| } | |
| async function getGraderScore() { | |
| try { | |
| const res = await fetch(`${BACKEND_URL}/grader`, { | |
| method:'POST', headers:{'Content-Type':'application/json'}, | |
| body: JSON.stringify({ task_id: selectedTask, episode_history: episodeHistory }) | |
| }); | |
| if (!res.ok) throw new Error('Grader error'); | |
| const data = await res.json(); | |
| const score = data.score ?? 0; | |
| document.getElementById('fill'+selectedTask).style.width = (score * 100) + '%'; | |
| document.getElementById('score'+selectedTask).textContent = score.toFixed(2) + ' / 1.0'; | |
| document.getElementById('resultDetail').textContent = | |
| `β Task ${selectedTask} completed! Final score: ${score.toFixed(2)} / 1.0 β AI ran ${episodeHistory.length} days.`; | |
| document.getElementById('aiThinkingText').textContent = | |
| `Done! Task ${selectedTask} final score: ${score.toFixed(2)} / 1.0`; | |
| document.getElementById('aiReasonBox').textContent = | |
| `AI agent completed all ${episodeHistory.length} days. Final score: ${score.toFixed(2)} / 1.0 π`; | |
| } catch(err) { | |
| showError('Could not get final score: ' + err.message); | |
| } | |
| } | |
| function setAIThinking(active, text) { | |
| document.getElementById('aiPulse').classList.toggle('active', active); | |
| document.getElementById('aiThinkingText').textContent = text; | |
| } | |
| function clearChosenBtn() { | |
| ['none','low','medium','high'].forEach(a => document.getElementById('abtn-'+a).classList.remove('chosen')); | |
| } | |
| function highlightAIChoice(action) { | |
| clearChosenBtn(); | |
| document.getElementById('abtn-'+action).classList.add('chosen'); | |
| } | |
| function updateDayUI(day, max) { | |
| document.getElementById('dayNum').textContent = day; | |
| document.getElementById('dayMax').textContent = max; | |
| document.getElementById('dayBar').style.width = (max > 0 ? (day/max*100).toFixed(1) : 0) + '%'; | |
| let stage = 1, pill = 'π± Seedling'; | |
| if (day > 20) { stage=3; pill='πΎ Mature'; } | |
| else if (day > 10) { stage=2; pill='πΏ Growing'; } | |
| document.getElementById('stagePill').textContent = pill; | |
| ['sm1','sm2','sm3'].forEach((id,i) => document.getElementById(id).classList.toggle('active', i+1===stage)); | |
| } | |
| function updateMoistureUI(val) { | |
| document.getElementById('moistureVal').innerHTML = parseFloat(val).toFixed(1) + '<span class="stat-unit">%</span>'; | |
| const bar = document.getElementById('moistureBar'); | |
| bar.style.width = Math.min(100, Math.max(0, val)) + '%'; | |
| if (val < 30) bar.style.background = 'linear-gradient(90deg,#ef5350,#e53935)'; | |
| else if (val > 80) bar.style.background = 'linear-gradient(90deg,#42a5f5,#1565c0)'; | |
| else bar.style.background = 'linear-gradient(90deg,#42a5f5,#26a69a)'; | |
| } | |
| function displayReward(reward, m, rain) { | |
| let stars, msg, detail; | |
| if (reward >= 0.8) { stars='βββββ'; msg='Perfect decision by AI!'; detail=`Moisture ${parseFloat(m).toFixed(1)}% β ideal range. AI nailed it! π±`; } | |
| else if (reward>=0.3) { stars='βββ'; msg='Acceptable, not perfect.'; detail=`Moisture ${parseFloat(m).toFixed(1)}% β close to ideal. AI is adapting. π§`; } | |
| else if (reward<-0.5) { stars='π'; msg='Crop in danger!'; detail=`Moisture ${parseFloat(m).toFixed(1)}% β critically off. AI will correct. π¨`; } | |
| else if (rain > 15) { stars='π§οΈ'; msg='Heavy rain β AI saved water.'; detail=`Rained ${rain}mm β AI correctly skipped watering to avoid waste. π¦`; } | |
| else { stars='β οΈ'; msg='Needs improvement.'; detail=`Reward: ${reward.toFixed(2)}. AI will adjust tomorrow. πΏ`; } | |
| document.getElementById('rewardNum').textContent = reward.toFixed(2); | |
| document.getElementById('rewardStars').textContent = stars; | |
| document.getElementById('rewardMsg').textContent = msg; | |
| document.getElementById('resultDetail').textContent = detail; | |
| const c = document.getElementById('scoreCircle'); | |
| if (reward >= 0.8) c.style.background = 'linear-gradient(135deg,#66bb6a,#2e7d32)'; | |
| else if (reward >= 0.3) c.style.background = 'linear-gradient(135deg,#ffa726,#e65100)'; | |
| else c.style.background = 'linear-gradient(135deg,#ef5350,#b71c1c)'; | |
| } | |
| function addLogRow(day, action, moist, reward) { | |
| const row = document.createElement('div'); | |
| row.className = 'log-row'; | |
| const rClass = reward >= 0.8 ? 'good' : reward >= 0.3 ? 'ok' : 'bad'; | |
| row.innerHTML = ` | |
| <span class="log-day">Day ${day}</span> | |
| <span class="log-action ${action}">${action.toUpperCase()}</span> | |
| <span class="log-moisture">${parseFloat(moist).toFixed(1)}%</span> | |
| <span class="log-reward ${rClass}">${reward.toFixed(2)}</span>`; | |
| const scroll = document.getElementById('logScroll'); | |
| scroll.appendChild(row); | |
| scroll.scrollTop = scroll.scrollHeight; | |
| } | |
| function resetRunState() { | |
| isRunning = false; | |
| const btn = document.getElementById('runBtn'); | |
| btn.disabled = false; btn.textContent = 'π€ Run AI Agent'; | |
| [1,2,3].forEach(i => document.getElementById('tbtn'+i).style.pointerEvents = 'auto'); | |
| } | |
| function sleep(ms) { return new Promise(r => setTimeout(r, ms)); } | |
| </script> | |
| </body> | |
| </html> | |