cd /news/artificial-intelligence/building-an-ai-powered-carbon-footpr… Β· home β€Ί topics β€Ί artificial-intelligence β€Ί article
[ARTICLE Β· art-33653] src=dev.to β†— pub= topic=artificial-intelligence verified=true sentiment=↑ positive

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.

read3 min views1 publishedJun 19, 2026

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).

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.

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.<span role="img" aria-label="...">

tags for screen-reader compatibility..sr-only

class to visually hide labels while exposing them to accessibility scanners.<h1>

header.4.5:1

on dark backgrounds.I built a comprehensive test suite in tests/test_app.py

containing 47 total assertions across five key test domains:

test_carbon_calculation

test_api_response

test_ai_json_structure

test_login

test_input_validation

pytest tests/test_app.py -v

Building this platform with Google Antigravity shifted my focus from syntax debugging to high-level intent design.

The resulting codebase is performant, accessible, secure, and ready to scale. Let's make the web a greener place! 🌿

This build is part of my journey with RohithBuilds, where I document building real AI applications with zero fluff.

If you want to learn how to build production-grade AI systems, check out my courses at rohith-builds.onrender.com.

── more in #artificial-intelligence 4 stories Β· sorted by recency
── more on @carbonwise 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain β€” perfect for shipping the agent you just read about.

$git push zahid main
β†’ Live at https://your-agent.zahid.host βœ“
Get free account β†’ Pricing
from €0/mo Β· no card required
LIVE [news/building-an-ai-power…] indexed:0 read:3min 2026-06-19 Β· β€”