Spaces:
Runtime error
Runtime error
sashavor
commited on
Commit
·
393f86d
1
Parent(s):
912ee6f
major app update
Browse files
app.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
HF_TOKEN = os.getenv('HUGGING_FACE_HUB_TOKEN')
|
| 7 |
+
hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "co2_submissions")
|
| 8 |
+
|
| 9 |
+
st.set_page_config(
|
| 10 |
+
page_title="AI Carbon Calculator",
|
| 11 |
+
layout="wide",
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
tdp_url = "https://raw.githubusercontent.com/mlco2/impact/master/data/gpus.csv"
|
| 15 |
+
compute_url = "https://raw.githubusercontent.com/mlco2/impact/master/data/impact.csv"
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
electricity_url = "https://raw.githubusercontent.com/mlco2/impact/master/data/2021-10-27yearly_averages.csv"
|
| 19 |
+
|
| 20 |
+
server_sheet_id = "1DqYgQnEDLQVQm5acMAhLgHLD8xXCG9BIrk-_Nv6jF3k"
|
| 21 |
+
server_sheet_name = "Server%20Carbon%20Footprint"
|
| 22 |
+
server_url = f"https://docs.google.com/spreadsheets/d/{server_sheet_id}/gviz/tq?tqx=out:csv&sheet={server_sheet_name}"
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
embodied_gpu_sheet_id = "1DqYgQnEDLQVQm5acMAhLgHLD8xXCG9BIrk-_Nv6jF3k"
|
| 26 |
+
embodied_gpu_sheet_name = "Scope%203%20Ratios"
|
| 27 |
+
embodied_gpu_url = f"https://docs.google.com/spreadsheets/d/{embodied_gpu_sheet_id}/gviz/tq?tqx=out:csv&sheet={embodied_gpu_sheet_name}"
|
| 28 |
+
|
| 29 |
+
TDP =pd.read_csv(tdp_url)
|
| 30 |
+
|
| 31 |
+
instances = pd.read_csv(compute_url)
|
| 32 |
+
providers = [p.upper() for p in instances['provider'].unique().tolist()]
|
| 33 |
+
providers.append('Local/Private Infastructure')
|
| 34 |
+
|
| 35 |
+
kg_per_mile = 0.348
|
| 36 |
+
|
| 37 |
+
electricity = pd.read_csv(electricity_url)
|
| 38 |
+
servers = pd.read_csv(server_url)
|
| 39 |
+
embodied_gpu = pd.read_csv(embodied_gpu_url)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
st.title("AI Carbon Calculator")
|
| 43 |
+
|
| 44 |
+
st.markdown('## Estimate your model\'s CO2 carbon footprint!')
|
| 45 |
+
|
| 46 |
+
st.markdown('Building on the work of the [ML CO2 Calculator](https://mlco2.github.io/impact/), this tool allows you to consider'
|
| 47 |
+
'other aspects of your model\'s carbon footprint based on the LCA methodology.')
|
| 48 |
+
|
| 49 |
+
st.markdown('We will consider 3 aspects of your model: the dynamic emissions, idle emissions embodied emissions.')
|
| 50 |
+
|
| 51 |
+
st.markdown('### Dynamic Emissions')
|
| 52 |
+
with st.expander("Calculate the emissions produced by energy consumption of model training"):
|
| 53 |
+
with st.form(key='dynamic_emissions'):
|
| 54 |
+
col1, col2, col3, col4 = st.columns(4)
|
| 55 |
+
with col1:
|
| 56 |
+
hardware = st.selectbox('GPU used', TDP['name'].tolist())
|
| 57 |
+
gpu_tdp = TDP['tdp_watts'][TDP['name'] == hardware].tolist()[0]
|
| 58 |
+
st.markdown("Different GPUs have different TDP (Thermal Design Power), which impacts how much energy you use.")
|
| 59 |
+
with col2:
|
| 60 |
+
training_time = st.number_input('Total number of GPU hours')
|
| 61 |
+
st.markdown('This is calculated by multiplying the number of GPUs you used by the training time: '
|
| 62 |
+
'i.e. if you used 100 GPUs for 10 hours, this is equal to 100x10 = 1,000 GPU hours.')
|
| 63 |
+
with col3:
|
| 64 |
+
provider = st.selectbox('Provider used', providers)
|
| 65 |
+
st.markdown('If you can\'t find your provider here, select "Local/Private Infrastructure".')
|
| 66 |
+
with col4:
|
| 67 |
+
if provider != 'Local/Private Infastructure':
|
| 68 |
+
provider_instances = instances['region'][instances['provider'] == provider.lower()].unique().tolist()
|
| 69 |
+
region = st.selectbox('Provider used', provider_instances)
|
| 70 |
+
carbon_intensity = instances['impact'][(instances['provider'] == provider.lower()) & (instances['region'] == region)].tolist()[0]
|
| 71 |
+
|
| 72 |
+
else:
|
| 73 |
+
carbon_intensity = st.number_input('Carbon intensity of your energy grid, in grams of CO2 per kWh')
|
| 74 |
+
st.markdown('You can consult a resource like the [IEA](https://www.iea.org/countries) or '
|
| 75 |
+
' [Electricity Map](https://app.electricitymaps.com/) to get this information.')
|
| 76 |
+
|
| 77 |
+
dynamic_emissions = round(gpu_tdp * training_time * carbon_intensity/1000000)
|
| 78 |
+
st.metric(label="Dynamic emissions", value=str(dynamic_emissions)+' kilograms of CO2eq')
|
| 79 |
+
st.markdown('This is roughly equivalent to '+ str(round(dynamic_emissions/kg_per_mile,1)) + ' miles driven in an average US car'
|
| 80 |
+
' produced in 2021. [(Source: energy.gov)](https://www.energy.gov/eere/vehicles/articles/fotw-1223-january-31-2022-average-carbon-dioxide-emissions-2021-model-year)')
|
| 81 |
+
hf_writer.setup([hardware, training_time, provider, carbon_intensity, dynamic_emissions], "dynamic_emissions")
|
| 82 |
+
st.form_submit_button(label="Share my data", help="Submit the data from your model anonymously for research purposes!",
|
| 83 |
+
onclick=hf_writer.flag([hardware, training_time, provider, carbon_intensity, dynamic_emissions]))
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
st.markdown('### Idle Emissions')
|
| 88 |
+
st.markdown('Do you know what the PUE (Power Usage Effectiveness) of your infrastructure is?')
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
st.markdown('### Embodied Emissions')
|
| 93 |
+
st.markdown('Choose your hardware, runtime and cloud provider/physical infrastructure to estimate the carbon impact of your research.')
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
st.markdown('#### More information about our Methodology')
|
| 99 |
+
|
| 100 |
+
st.image('images/LCA_CO2.png', caption='The LCA methodology - the parts in green are those we focus on.')
|
| 101 |
+
|
| 102 |
+
modelname = st.selectbox('Choose a model to test', TDP)
|