samuelrince commited on
Commit
a7e707d
·
1 Parent(s): b443202

refactor: remove unused code and format

Browse files
Files changed (7) hide show
  1. app.py +1 -1
  2. src/__init__.py +1 -1
  3. src/calculator.py +98 -98
  4. src/electricity_mix.py +0 -19
  5. src/expert.py +7 -7
  6. src/impacts.py +0 -9
  7. src/utils.py +4 -50
app.py CHANGED
@@ -15,7 +15,7 @@ from src.expert import expert_mode
15
  from src.calculator import calculator_mode
16
  from src.token_estimator import token_estimator
17
 
18
- st.set_page_config(layout="wide", page_title="ECOLOGITS", page_icon="💬")
19
 
20
  with open("src/style.css") as css:
21
  st.markdown(f"<style>{css.read()}</style>", unsafe_allow_html=True)
 
15
  from src.calculator import calculator_mode
16
  from src.token_estimator import token_estimator
17
 
18
+ st.set_page_config(layout="wide", page_title="EcoLogits Calculator", page_icon="🧮")
19
 
20
  with open("src/style.css") as css:
21
  st.markdown(f"<style>{css.read()}</style>", unsafe_allow_html=True)
src/__init__.py CHANGED
@@ -4,6 +4,6 @@ from .expert import expert_mode
4
  from .token_estimator import token_estimator
5
  from .utils import *
6
  from .calculator import calculator_mode
7
- from .impacts import get_impacts, display_impacts
8
  from .models import load_models
9
  from .electricity_mix import *
 
4
  from .token_estimator import token_estimator
5
  from .utils import *
6
  from .calculator import calculator_mode
7
+ from .impacts import display_impacts
8
  from .models import load_models
9
  from .electricity_mix import *
src/calculator.py CHANGED
@@ -1,99 +1,99 @@
1
- import streamlit as st
2
-
3
- from ecologits.tracers.utils import llm_impacts
4
- from src.impacts import get_impacts, display_impacts, display_equivalent_ghg, display_equivalent_energy
5
- from src.utils import format_impacts
6
- from src.content import WARNING_CLOSED_SOURCE, WARNING_MULTI_MODAL, WARNING_BOTH, HOW_TO_TEXT
7
- from src.models import load_models
8
-
9
- from src.constants import PROMPTS
10
-
11
-
12
- def calculator_mode():
13
-
14
- st.expander("How to use this calculator?", expanded = False).markdown(HOW_TO_TEXT)
15
-
16
- with st.container(border=True):
17
- df = load_models(filter_main=True)
18
-
19
- col1, col2, col3 = st.columns(3)
20
-
21
- with col1:
22
- providers_clean = [x for x in df["provider_clean"].unique()]
23
- provider = st.selectbox(
24
- label="Provider",
25
- options=providers_clean,
26
- index=providers_clean.index("OpenAI"),
27
- )
28
-
29
- with col2:
30
- model = st.selectbox(
31
- label="Model",
32
- options=[
33
- x
34
- for x in df["name_clean"].unique()
35
- if x in df[df["provider_clean"] == provider]["name_clean"].unique()
36
- ],
37
- )
38
-
39
- with col3:
40
- output_tokens = st.selectbox("Example prompt", [x[0] for x in PROMPTS])
41
-
42
- # WARNING DISPLAY
43
- provider_raw = df[
44
- (df["provider_clean"] == provider) & (df["name_clean"] == model)
45
- ]["provider"].values[0]
46
- model_raw = df[
47
- (df["provider_clean"] == provider) & (df["name_clean"] == model)
48
- ]["name"].values[0]
49
-
50
- df_filtered = df[
51
- (df["provider_clean"] == provider) & (df["name_clean"] == model)
52
- ]
53
-
54
- if (
55
- df_filtered["warning_arch"].values[0]
56
- and not df_filtered["warning_multi_modal"].values[0]
57
- ):
58
- st.warning(WARNING_CLOSED_SOURCE, icon="⚠️")
59
- if (
60
- df_filtered["warning_multi_modal"].values[0]
61
- and not df_filtered["warning_arch"].values[0]
62
- ):
63
- st.warning(WARNING_MULTI_MODAL, icon="⚠️")
64
- if (
65
- df_filtered["warning_arch"].values[0]
66
- and df_filtered["warning_multi_modal"].values[0]
67
- ):
68
- st.warning(WARNING_BOTH, icon="⚠️")
69
-
70
- try:
71
- impacts = llm_impacts(
72
- provider=provider_raw,
73
- model_name=model_raw,
74
- output_token_count=[x[1] for x in PROMPTS if x[0] == output_tokens][0],
75
- request_latency=100000,
76
- )
77
-
78
- impacts, _, _ = format_impacts(impacts)
79
-
80
- with st.container(border=True):
81
-
82
- st.markdown('<h3 align = "center">Environmental impacts</h3>', unsafe_allow_html=True)
83
- #st.markdown('<p align = "center">To understand how the environmental impacts are computed go to the 📖 Methodology tab.</p>', unsafe_allow_html=True)
84
- display_impacts(impacts)
85
-
86
- with st.container(border=False):
87
- st.markdown('<h3 align = "center">Equivalences</h3>', unsafe_allow_html=True)
88
- st.markdown('<p align = "center">Making this request to the LLM is equivalent to the following actions :</p>', unsafe_allow_html=True)
89
- page = st.radio(' ', ['Energy' , 'GHG'], horizontal=True)
90
-
91
- with st.container(border=True):
92
- if page == 'Energy' :
93
- display_equivalent_energy(impacts)
94
- else :
95
- display_equivalent_ghg(impacts)
96
-
97
-
98
- except Exception as e:
99
  st.error('Could not find the model in the repository. Please try another model.')
 
1
+ import streamlit as st
2
+
3
+ from ecologits.tracers.utils import llm_impacts
4
+ from src.impacts import display_impacts, display_equivalent_ghg, display_equivalent_energy
5
+ from src.utils import format_impacts
6
+ from src.content import WARNING_CLOSED_SOURCE, WARNING_MULTI_MODAL, WARNING_BOTH, HOW_TO_TEXT
7
+ from src.models import load_models
8
+
9
+ from src.constants import PROMPTS
10
+
11
+
12
+ def calculator_mode():
13
+
14
+ st.expander("How to use this calculator?", expanded = False).markdown(HOW_TO_TEXT)
15
+
16
+ with st.container(border=True):
17
+ df = load_models(filter_main=True)
18
+
19
+ col1, col2, col3 = st.columns(3)
20
+
21
+ with col1:
22
+ providers_clean = [x for x in df["provider_clean"].unique()]
23
+ provider = st.selectbox(
24
+ label="Provider",
25
+ options=providers_clean,
26
+ index=providers_clean.index("OpenAI"),
27
+ )
28
+
29
+ with col2:
30
+ model = st.selectbox(
31
+ label="Model",
32
+ options=[
33
+ x
34
+ for x in df["name_clean"].unique()
35
+ if x in df[df["provider_clean"] == provider]["name_clean"].unique()
36
+ ],
37
+ )
38
+
39
+ with col3:
40
+ output_tokens = st.selectbox("Example prompt", [x[0] for x in PROMPTS])
41
+
42
+ # WARNING DISPLAY
43
+ provider_raw = df[
44
+ (df["provider_clean"] == provider) & (df["name_clean"] == model)
45
+ ]["provider"].values[0]
46
+ model_raw = df[
47
+ (df["provider_clean"] == provider) & (df["name_clean"] == model)
48
+ ]["name"].values[0]
49
+
50
+ df_filtered = df[
51
+ (df["provider_clean"] == provider) & (df["name_clean"] == model)
52
+ ]
53
+
54
+ if (
55
+ df_filtered["warning_arch"].values[0]
56
+ and not df_filtered["warning_multi_modal"].values[0]
57
+ ):
58
+ st.warning(WARNING_CLOSED_SOURCE, icon="⚠️")
59
+ if (
60
+ df_filtered["warning_multi_modal"].values[0]
61
+ and not df_filtered["warning_arch"].values[0]
62
+ ):
63
+ st.warning(WARNING_MULTI_MODAL, icon="⚠️")
64
+ if (
65
+ df_filtered["warning_arch"].values[0]
66
+ and df_filtered["warning_multi_modal"].values[0]
67
+ ):
68
+ st.warning(WARNING_BOTH, icon="⚠️")
69
+
70
+ try:
71
+ impacts = llm_impacts(
72
+ provider=provider_raw,
73
+ model_name=model_raw,
74
+ output_token_count=[x[1] for x in PROMPTS if x[0] == output_tokens][0],
75
+ request_latency=100000,
76
+ )
77
+
78
+ impacts, _, _ = format_impacts(impacts)
79
+
80
+ with st.container(border=True):
81
+
82
+ st.markdown('<h3 align = "center">Environmental impacts</h3>', unsafe_allow_html=True)
83
+ #st.markdown('<p align = "center">To understand how the environmental impacts are computed go to the 📖 Methodology tab.</p>', unsafe_allow_html=True)
84
+ display_impacts(impacts)
85
+
86
+ with st.container(border=False):
87
+ st.markdown('<h3 align = "center">Equivalences</h3>', unsafe_allow_html=True)
88
+ st.markdown('<p align = "center">Making this request to the LLM is equivalent to the following actions :</p>', unsafe_allow_html=True)
89
+ page = st.radio(' ', ['Energy' , 'GHG'], horizontal=True)
90
+
91
+ with st.container(border=True):
92
+ if page == 'Energy' :
93
+ display_equivalent_energy(impacts)
94
+ else :
95
+ display_equivalent_ghg(impacts)
96
+
97
+
98
+ except Exception as e:
99
  st.error('Could not find the model in the repository. Please try another model.')
src/electricity_mix.py CHANGED
@@ -74,22 +74,3 @@ def format_country_name(code: str) -> str | None:
74
 
75
  def format_electricity_mix_criterion(criterion: str) -> str | None:
76
  return CRITERIA.get(criterion)
77
-
78
-
79
- def dataframe_electricity_mix(countries: list):
80
- df = pd.read_csv("src/data/electricity_mix.csv")
81
-
82
-
83
- df["name_unit"] = df["name"] + " (" + df["unit"] + ")"
84
- df = df[["name_unit"] + [x[1] for x in COUNTRY_CODES if x[0] in countries]]
85
-
86
- df_melted = df.melt(
87
- id_vars=["name_unit"],
88
- value_vars=[x[1] for x in COUNTRY_CODES if x[0] in countries],
89
- var_name="country",
90
- value_name="value",
91
- )
92
-
93
- df = df_melted.pivot(columns="name_unit", index="country", values="value")
94
-
95
- return df
 
74
 
75
  def format_electricity_mix_criterion(criterion: str) -> str | None:
76
  return CRITERIA.get(criterion)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/expert.py CHANGED
@@ -3,7 +3,7 @@ import streamlit as st
3
  from ecologits.electricity_mix_repository import electricity_mixes
4
  from ecologits.impacts.llm import compute_llm_impacts
5
 
6
- from src.utils import format_impacts, average_range_impacts
7
  from src.impacts import display_impacts
8
  from src.electricity_mix import COUNTRY_CODES, format_electricity_mix_criterion, format_country_name
9
  from src.models import load_models
@@ -175,8 +175,8 @@ def expert_mode():
175
  with col_ghg_comparison:
176
  fig_gwp = px.pie(
177
  values=[
178
- average_range_impacts(usage.gwp.value),
179
- average_range_impacts(embodied.gwp.value),
180
  ],
181
  names=["usage", "embodied"],
182
  title="GHG emissions",
@@ -190,8 +190,8 @@ def expert_mode():
190
  with col_adpe_comparison:
191
  fig_adpe = px.pie(
192
  values=[
193
- average_range_impacts(usage.adpe.value),
194
- average_range_impacts(embodied.adpe.value),
195
  ],
196
  names=["usage", "embodied"],
197
  title="Abiotic depletion",
@@ -205,8 +205,8 @@ def expert_mode():
205
  with col_pe_comparison:
206
  fig_pe = px.pie(
207
  values=[
208
- average_range_impacts(usage.pe.value),
209
- average_range_impacts(embodied.pe.value),
210
  ],
211
  names=["usage", "embodied"],
212
  title="Primary energy",
 
3
  from ecologits.electricity_mix_repository import electricity_mixes
4
  from ecologits.impacts.llm import compute_llm_impacts
5
 
6
+ from src.utils import format_impacts
7
  from src.impacts import display_impacts
8
  from src.electricity_mix import COUNTRY_CODES, format_electricity_mix_criterion, format_country_name
9
  from src.models import load_models
 
175
  with col_ghg_comparison:
176
  fig_gwp = px.pie(
177
  values=[
178
+ usage.gwp.value if isinstance(usage.gwp.value, float) else usage.gwp.value.mean,
179
+ embodied.gwp.value if isinstance(embodied.gwp.value, float) else embodied.gwp.value.mean,
180
  ],
181
  names=["usage", "embodied"],
182
  title="GHG emissions",
 
190
  with col_adpe_comparison:
191
  fig_adpe = px.pie(
192
  values=[
193
+ usage.adpe.value if isinstance(usage.adpe.value, float) else usage.adpe.value.mean,
194
+ embodied.adpe.value if isinstance(embodied.adpe.value, float) else embodied.adpe.value.mean,
195
  ],
196
  names=["usage", "embodied"],
197
  title="Abiotic depletion",
 
205
  with col_pe_comparison:
206
  fig_pe = px.pie(
207
  values=[
208
+ usage.pe.value if isinstance(usage.pe.value, float) else usage.pe.value.mean,
209
+ embodied.pe.value if isinstance(embodied.pe.value, float) else embodied.pe.value.mean,
210
  ],
211
  names=["usage", "embodied"],
212
  title="Primary energy",
src/impacts.py CHANGED
@@ -10,15 +10,6 @@ from src.utils import (
10
  EnergyProduction,
11
  )
12
 
13
- ############################################################################################################
14
-
15
-
16
- def get_impacts(model, active_params, total_params, mix_ghg, mix_adpe, mix_pe):
17
- return 1
18
-
19
-
20
- ############################################################################################################
21
-
22
 
23
  def display_impacts(impacts):
24
  st.divider()
 
10
  EnergyProduction,
11
  )
12
 
 
 
 
 
 
 
 
 
 
13
 
14
  def display_impacts(impacts):
15
  st.divider()
src/utils.py CHANGED
@@ -1,12 +1,10 @@
1
  from dataclasses import dataclass
2
  from enum import Enum
3
 
4
- from ecologits.impacts.modeling import Impacts, Energy, GWP, ADPe, PE, WCF
5
 
6
- # from ecologits.tracers.utils import llm_impacts
7
  from pint import UnitRegistry, Quantity
8
  import streamlit as st
9
- import plotly.express as px
10
  import plotly.graph_objects as go
11
 
12
  #####################################################################################
@@ -163,7 +161,7 @@ def format_wcf(wcf_value: float, wcf_unit = WCF(value=0.).unit) -> Quantity:
163
  return val
164
 
165
 
166
- def format_impacts(impacts: Impacts) -> QImpacts:
167
  if isinstance(impacts.energy.value, float):
168
  return QImpacts(
169
  energy=format_energy(impacts.energy.value),
@@ -192,50 +190,6 @@ def format_impacts(impacts: Impacts) -> QImpacts:
192
  wcf_max=format_wcf(impacts.wcf.value.max),
193
  ranges=True
194
  ), impacts.usage, impacts.embodied
195
-
196
-
197
- def split_impacts_u_e(impacts: Impacts) -> QImpacts:
198
- return impacts.usage, impacts.embodied
199
-
200
-
201
- def average_range_impacts(x):
202
- if isinstance(x, float):
203
- return x
204
- else:
205
- return (x.max + x.min) / 2
206
-
207
-
208
- def format_impacts_expert(impacts: Impacts, display_range: bool) -> QImpacts:
209
- if display_range:
210
- return (
211
- QImpacts(
212
- energy=format_energy(impacts.energy),
213
- gwp=format_gwp(impacts.gwp),
214
- adpe=format_adpe(impacts.adpe),
215
- pe=format_pe(impacts.pe),
216
- ),
217
- impacts.usage,
218
- impacts.embodied,
219
- )
220
-
221
- else:
222
- energy = {
223
- "value": (impacts.energy.value.max + impacts.energy.value.min) / 2,
224
- "unit": impacts.energy.unit,
225
- }
226
- gwp = (impacts.gwp.value.max + impacts.gwp.value.min) / 2
227
- adpe = (impacts.adpe.value.max + impacts.adpe.value.min) / 2
228
- pe = (impacts.pe.value.max + impacts.pe.value.min) / 2
229
- return (
230
- QImpacts(
231
- energy=format_energy(energy),
232
- gwp=format_gwp(gwp),
233
- adpe=format_adpe(adpe),
234
- pe=format_pe(pe),
235
- ),
236
- impacts.usage,
237
- impacts.embodied,
238
- )
239
 
240
 
241
  #####################################################################################
@@ -295,7 +249,7 @@ def format_energy_eq_electricity_consumption_ireland(energy: Quantity) -> Quanti
295
  def format_gwp_eq_airplane_paris_nyc(gwp: Quantity) -> Quantity:
296
  gwp_eq = gwp * ONE_PERCENT_WORLD_POPULATION * DAYS_IN_YEAR
297
  gwp_eq = gwp_eq.to("kgCO2eq")
298
- return gwp_eq / AIRPLANE_PARIS_NYC_GWP_EQ####################################################################################### MODELS PARAMETER####################################################################################
299
 
300
  #####################################################################################
301
  ### VISUALIZATIONS
@@ -354,4 +308,4 @@ def range_plot (mean_val, min_val, max_val, unit):
354
  )
355
 
356
  # Show the plot in Streamlit
357
- st.plotly_chart(fig, use_container_width=True)
 
1
  from dataclasses import dataclass
2
  from enum import Enum
3
 
4
+ from ecologits.impacts.modeling import Impacts, Energy, GWP, ADPe, PE, WCF, Usage, Embodied
5
 
 
6
  from pint import UnitRegistry, Quantity
7
  import streamlit as st
 
8
  import plotly.graph_objects as go
9
 
10
  #####################################################################################
 
161
  return val
162
 
163
 
164
+ def format_impacts(impacts: Impacts) -> tuple[QImpacts, Usage, Embodied]:
165
  if isinstance(impacts.energy.value, float):
166
  return QImpacts(
167
  energy=format_energy(impacts.energy.value),
 
190
  wcf_max=format_wcf(impacts.wcf.value.max),
191
  ranges=True
192
  ), impacts.usage, impacts.embodied
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
 
194
 
195
  #####################################################################################
 
249
  def format_gwp_eq_airplane_paris_nyc(gwp: Quantity) -> Quantity:
250
  gwp_eq = gwp * ONE_PERCENT_WORLD_POPULATION * DAYS_IN_YEAR
251
  gwp_eq = gwp_eq.to("kgCO2eq")
252
+ return gwp_eq / AIRPLANE_PARIS_NYC_GWP_EQ
253
 
254
  #####################################################################################
255
  ### VISUALIZATIONS
 
308
  )
309
 
310
  # Show the plot in Streamlit
311
+ st.plotly_chart(fig, use_container_width=True)