Building an AI-Powered Carbon Footprint Awareness Platform with Flask, SQLite, and Groq (Llama 3.1) A developer built CarbonWise, a carbon footprint awareness platform using Flask, SQLite, and the Groq LLM API (Llama 3.1). The platform combines deterministic carbon calculations with personalized AI reduction strategies, and features session caching, database optimization, and security headers for production readiness. As climate awareness grows, individuals are looking for actionable ways to reduce their personal carbon footprints. However, most carbon calculators are either too complex or offer generic, unhelpful advice. To solve this, I built CarbonWise —a production-ready Carbon Footprint Awareness Platform. It combines deterministic scientific carbon calculations with real-time, personalized AI reduction strategies using the Groq LLM API. Here is a technical deep-dive into how I built, secured, and optimized this application for the PromptWars: Virtual challenge. The application is designed to be lightweight, secure, and highly performant, avoiding heavy framework overhead. ┌──────────────────────────────────────────────────────────┐ │ User Browser │ └─────────────┬──────────────────────────────▲─────────────┘ │ HTTPS POST / GET │ Rendered HTML/CSS ┌─────────────▼──────────────────────────────┴─────────────┐ │ Flask App app.py │ └─────────────┬──────────────┬───────────────▲─────────────┘ │ │ │ ┌─────────────▼──────────┐ ┌─▼─────────────┐ │ │ SQLite DB carbon.db │ │Secure Session │ │ │ - Users & Logs │ │ Cookies │ │ │ - WAL Mode Enabled │ └───────────────┘ │ └────────────────────────┘ │ Structured JSON Insights ┌────────────────────────────────────────────┴─────────────┐ │ Groq API Llama 3.1 │ │ - Model: llama-3.1-8b-instant │ └──────────────────────────────────────────────────────────┘ llama-3.1-8b-instant . carbon engine.py To prevent calculation drift, the engine uses fixed conversion factors for logging three main categories: 0.21 kg/km , Motorbike 0.05 kg/km , Bus 0.08 kg/km , and Flight 0.255 kg/km . 0.5 kg/meal and Non-vegetarian meal 2.5 kg/meal . 0.82 kg/kWh . php def calculate co2 activity type: str, quantity: float - float: if activity type not in EMISSION FACTORS: raise ValueError f"Unknown activity type '{activity type}'" if quantity <= 0: raise ValueError "Quantity must be positive" return round EMISSION FACTORS activity type quantity, 4 ai engine.py To keep the dashboard fast and prevent redundant external HTTP calls, I implemented a session caching mechanism: summary , top emission sources , suggestions 3–5 tips , eco score 0–100 , and motivation . /log , the cache is invalidated so the dashboard gets fresh insights on the next load. Check session cache insights = session.get "ai insights" if not insights: all acts = get all activities 7days user id insights = get ai insights all acts, daily total, category breakdown session "ai insights" = insights To make sure data queries are incredibly fast, I optimized the database schema by introducing a covering index : CREATE INDEX IF NOT EXISTS idx activities covering ON activities user id, timestamp, activity type, co2 kg ; With this index in place, queries for the weekly analytics dashboard and category breakdowns are satisfied directly from index pages, bypassing expensive full-table scans. We also optimized category queries to aggregate in a single SQL operation: SELECT CASE WHEN activity type IN 'car', 'bike', 'bus', 'flight' THEN 'travel' WHEN activity type IN 'veg', 'non veg' THEN 'food' WHEN activity type IN 'electricity' THEN 'electricity' END AS category, COALESCE SUM co2 kg , 0 AS total FROM activities WHERE user id = ? AND timestamp = DATE 'now', '-6 days' GROUP BY category X-Content-Type-Options: nosniff X-Frame-Options: DENY X-XSS-Protection: 1; mode=block Referrer-Policy: strict-origin-when-cross-origin Content-Security-Policy CSP restricting assets to 'self' and specific secure fonts. Secure , HttpOnly , and SameSite='Lax' flags.