ecoChef / carbon_model.py
tejasashinde's picture
Added project
d9d1fcf
import pandas as pd
from rapidfuzz import process, fuzz
# Load Excel sheet once
excel_path = "SuEatableLife_Food_Fooprint_database.xlsx"
sheet_name = "SEL CF for users"
df = pd.read_excel(excel_path, sheet_name=sheet_name, engine='openpyxl')
# Clean column names
df.columns = df.columns.str.strip()
# Define lookup column and value column
item_col = "Food commodity ITEM"
carbon_col = "Carbon Footprint kg CO2eq/kg or l of food ITEM"
# Drop rows with missing values
df = df[[item_col, carbon_col]].dropna()
# Prepare choices
choices = df[item_col].str.lower().tolist()
data = df.to_dict(orient="records")
# Main function to estimate carbon footprint
def estimate_carbon(item, data_lookup=data, top_n=3, score_threshold=80):
name = item.get("name", "").strip().lower()
top_matches = process.extract(name, choices, scorer=fuzz.token_set_ratio, limit=top_n)
for matched_name, score, match_index in top_matches:
if score >= score_threshold:
match = data_lookup[match_index]
return {
"name": item["name"],
"estimated_kg_CO2e_per_kg": match.get(carbon_col, "unknown")
}
return {
"name": item["name"],
"estimated_kg_CO2e_per_kg": "unknown"
}