Skip to content

Rishab-Mal/SwampSaver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

39 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

SwampSaver ๐ŸŠ๐ŸŒฟ

A sustainability-focused application that monitors your digital carbon footprint across browser and system activities. Track your energy consumption, earn rewards for eco-friendly behavior, and get AI-powered tips to reduce your environmental impact.

Features

  • โœ… Real-time Energy Monitoring - Track CPU, memory, and power consumption (in watts/milliwatts)
  • โœ… AI-Powered Tips - Get personalized sustainability recommendations via OpenAI integration
  • โœ… AI Chatbot - Interactive chat with context-aware responses about your system usage
  • โœ… Gamification System - Earn "leaves" (currency) for eco-friendly behavior
  • โœ… Challenges - Complete sustainability challenges like "Touch Grass" and "Low-Carbon Life"
  • โœ… History & Analytics - View historical data with interactive charts (powered by Recharts)
  • โœ… Process Management - Kill energy-hungry apps and tabs directly from the dashboard
  • โœ… Chrome Extension - Monitor tabs with a popup UI showing mood-based system status
  • โœ… Tab & App Tracking - See which apps/tabs are consuming the most energy

Architecture

SwampSaver consists of three integrated components:

1. FastAPI Backend (/backend)

  • Port: 8000
  • Framework: FastAPI with uvicorn
  • Routes:
    • GET /stats - Returns system stats, apps, tabs, history, and AI tips
    • POST /update-tabs - Receives tab data from Chrome extension
    • GET /extension-poll - Extension polls for pending actions (tab kills)
    • POST /kill-process - Terminates a process by PID
    • POST /kill-tab - Queues a tab to be closed by the extension
    • GET /leaves - Returns current leaf count
    • POST /bank-leaves - Adds leaves to your balance
    • POST /chat - AI chatbot endpoint with context-aware responses
    • GET /challenges - Returns current challenge status
    • POST /reset-challenge - Reset a specific challenge

2. Chrome Extension (/extension)

  • Manifest V3 compliant
  • Popup UI with mood-based status indicators (happy/concerned/mad)
  • Monitors tab changes and open tab count
  • Sends tab data to backend via POST /update-tabs
  • Polls for and executes tab kill commands
  • Quick access "Open Dashboard" button

3. React Frontend (/frontend)

  • Dashboard - Real-time stats, AI tips, app/tab management, chatbot
  • Challenges Page - View and complete sustainability challenges
  • History Page - Interactive charts for energy tracking over time
  • Styled with custom CSS (dark theme with accent colors)
  • Polls backend every 3 seconds for live updates

Quick Start

Prerequisites

  • Python 3.8+
  • Node.js 16+
  • Chrome browser
  • OpenAI API key (for AI features)

Backend Setup

On macOS/Linux:

cd backend

# Create virtual environment
python3 -m venv venv

# Activate virtual environment
source venv/bin/activate

# Install dependencies
pip install -r requirements.txt

# Create .env file with your OpenAI API key
echo "OPENAI_API_KEY=your_api_key_here" > .env

# Run the server
python app.py

On Windows:

cd backend

# Create virtual environment
python -m venv venv

# Activate virtual environment
venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Create .env file with your OpenAI API key
echo OPENAI_API_KEY=your_api_key_here > .env

# Run the server
python app.py

The FastAPI server will start on http://localhost:8000

Frontend Setup

cd frontend
npm install
npm start

The React app will open on http://localhost:3000

Chrome Extension Setup

  1. Open Chrome and go to chrome://extensions/
  2. Enable "Developer mode" (top right)
  3. Click "Load unpacked"
  4. Select the /extension folder
  5. The extension will appear in your Chrome toolbar
  6. Click the extension icon to see the popup with real-time stats

How It Works

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Chrome Extension      โ”‚
โ”‚   (background.js)       โ”‚
โ”‚                         โ”‚
โ”‚ Monitors tab changes    โ”‚โ”€โ”€POST /update-tabsโ”€โ”€โ”
โ”‚ Counts open tabs        โ”‚                     โ”‚
โ”‚ Polls for tab kills     โ”‚โ—„โ”€GET /extension-pollโ”‚
โ”‚ Shows popup UI          โ”‚                     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                     โ”‚
                                                โ–ผ
                          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                          โ”‚  FastAPI Backend        โ”‚
                          โ”‚  (app.py)               โ”‚
                          โ”‚  Port: 8000             โ”‚
                          โ”‚                         โ”‚
                          โ”‚ โšก System monitoring    โ”‚
                          โ”‚ ๐Ÿค– OpenAI integration   โ”‚
                          โ”‚ ๐ŸŽฎ Challenge tracking   โ”‚
                          โ”‚ ๐Ÿƒ Leaf currency system โ”‚
                          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                    โ–ฒ
                                    โ”‚
                               GET /stats
                            (poll every 3s)
                                    โ”‚
                          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                          โ”‚  React Frontend         โ”‚
                          โ”‚  Port: 3000             โ”‚
                          โ”‚                         โ”‚
                          โ”‚ ๐Ÿ“Š Dashboard            โ”‚
                          โ”‚ ๐Ÿ“ˆ History charts       โ”‚
                          โ”‚ ๐Ÿ† Challenges           โ”‚
                          โ”‚ ๐Ÿ’ฌ AI Chatbot           โ”‚
                          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Gamification System

Leaves ๐Ÿƒ

Earn leaves by maintaining low energy consumption. The formula:

Leaves = ((Baseline - Current) / 1000) ร— minutes

Challenges

  • The Low-Carbon Life - Keep system draw below 32560 mW
  • Touch Grass - Stay idle for 15+ minutes (auto-detected)
  • Night Mode Master - Keep watts < 25W for 30 minutes
  • Energy Efficiency Champion - Reduce energy by 50% for 1 hour

File Structure

SwampSaver/
โ”œโ”€โ”€ backend/
โ”‚   โ”œโ”€โ”€ app.py              # FastAPI server (main application)
โ”‚   โ”œโ”€โ”€ ai_tips.py          # AI tip generation utilities
โ”‚   โ”œโ”€โ”€ coins.txt           # Persistent leaf storage
โ”‚   โ”œโ”€โ”€ requirements.txt    # Python dependencies
โ”‚   โ””โ”€โ”€ .env                # Environment variables (API keys)
โ”œโ”€โ”€ extension/
โ”‚   โ”œโ”€โ”€ manifest.json       # Extension configuration (Manifest V3)
โ”‚   โ”œโ”€โ”€ background.js       # Service worker (tab monitoring)
โ”‚   โ”œโ”€โ”€ popup.html          # Extension popup UI
โ”‚   โ”œโ”€โ”€ popup.js            # Popup logic
โ”‚   โ”œโ”€โ”€ popup.css           # Popup styling
โ”‚   โ””โ”€โ”€ *.png               # Mood icons (happy, concerned, mad)
โ”œโ”€โ”€ frontend/
โ”‚   โ”œโ”€โ”€ public/
โ”‚   โ”‚   โ””โ”€โ”€ index.html
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ App.js          # Main dashboard component
โ”‚   โ”‚   โ”œโ”€โ”€ App.css         # Dashboard styles
โ”‚   โ”‚   โ”œโ”€โ”€ Challenges.js   # Challenges page component
โ”‚   โ”‚   โ”œโ”€โ”€ History.js      # History/analytics page
โ”‚   โ”‚   โ”œโ”€โ”€ HistoryManager.js # Data persistence for history
โ”‚   โ”‚   โ”œโ”€โ”€ index.js        # React entry point
โ”‚   โ”‚   โ””โ”€โ”€ index.css       # Global styles
โ”‚   โ””โ”€โ”€ package.json
โ””โ”€โ”€ README.md

Testing the Connection

Prerequisites:

  • Virtual environment activated in backend directory
  • OpenAI API key configured in .env
  • Backend running on http://localhost:8000
  • Frontend running on http://localhost:3000
  • Chrome extension loaded in developer mode

Steps:

  1. Start the FastAPI backend (see Backend Setup above)
  2. In a new terminal, start the React frontend (see Frontend Setup above)
  3. Load the Chrome extension
  4. Open multiple tabs in Chrome
  5. Switch between tabs
  6. Watch the React dashboard update in real-time
  7. Try the AI chatbot - ask about your energy usage
  8. Check the Challenges page to see your progress
  9. View the History page for energy trends over time

Troubleshooting:

  • Backend not connecting? Make sure uvicorn is running on port 8000
  • AI tips not working? Verify your OpenAI API key is set in .env
  • Port already in use? On macOS, AirPlay may use port 5000. SwampSaver uses port 8000 by default
  • Virtual environment issues? Ensure you've activated the venv before running pip install and python app.py
  • Extension not updating? Check the extension's service worker in chrome://extensions/ for errors

Dependencies

Backend:

  • FastAPI
  • uvicorn
  • psutil 5.9.5
  • openai
  • python-dotenv

Frontend:

  • React 18.2.0
  • React DOM 18.2.0
  • Recharts 3.7.0

Extension:

  • Vanilla JavaScript
  • Chrome Manifest V3 APIs

Environment Variables

Create a .env file in the /backend directory:

OPENAI_API_KEY=your_openai_api_key_here

Built with โค๏ธ for the SwampHacks hackathon ๐ŸŠ

About

This web-app and extension will allow people to save battery. It will alert the user if a certain app/website is using too much power. And, it will give the viewer suggestions to reduce power spending.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors