vdwow commited on
Commit
1d2dea8
·
1 Parent(s): b808e59

feat: plots update + usage%embodied

Browse files
Files changed (2) hide show
  1. app.py +27 -43
  2. src/utils.py +17 -2
app.py CHANGED
@@ -5,9 +5,6 @@ from bs4 import BeautifulSoup
5
 
6
  import tiktoken
7
 
8
- import matplotlib
9
- import matplotlib.pyplot as plt
10
-
11
  from ecologits.tracers.utils import compute_llm_impacts, _avg
12
  from ecologits.impacts.llm import compute_llm_impacts as compute_llm_impacts_expert
13
  from ecologits.impacts.llm import IF_ELECTRICITY_MIX_GWP, IF_ELECTRICITY_MIX_ADPE, IF_ELECTRICITY_MIX_PE
@@ -35,11 +32,13 @@ from src.constants import (
35
  )
36
  from src.utils import (
37
  format_impacts,
 
38
  format_energy_eq_physical_activity,
39
  PhysicalActivity,
40
  format_energy_eq_electric_vehicle,
41
  format_gwp_eq_streaming, format_energy_eq_electricity_production, EnergyProduction,
42
- format_gwp_eq_airplane_paris_nyc, format_energy_eq_electricity_consumption_ireland
 
43
  )
44
 
45
  CUSTOM = "Custom"
@@ -364,10 +363,10 @@ with gr.Blocks(css=custom_css) as demo:
364
  if_electricity_mix_adpe=mix_adpe,
365
  if_electricity_mix_pe=mix_pe
366
  )
367
- impacts = format_impacts(impacts)
368
 
369
  with gr.Blocks():
370
-
371
  with gr.Row():
372
  gr.Markdown(f"""
373
  <h2 align = "center">Environmental impacts</h2>
@@ -380,61 +379,46 @@ with gr.Blocks(css=custom_css) as demo:
380
  $$ \Large {impacts.energy.magnitude:.3g} \ \large {impacts.energy.units} $$
381
  <p align="center"><i>Evaluates the electricity consumption<i></p><br>
382
  """)
 
383
  with gr.Column(scale=1, min_width=220):
384
  gr.Markdown(f"""
385
  <h2 align="center">🌍️ GHG Emissions</h2>
386
  $$ \Large {impacts.gwp.magnitude:.3g} \ \large {impacts.gwp.units} $$
387
  <p align="center"><i>Evaluates the effect on global warming<i></p><br>
 
 
388
  """)
 
389
  with gr.Column(scale=1, min_width=220):
390
  gr.Markdown(f"""
391
  <h2 align="center">🪨 Abiotic Resources</h2>
392
  $$ \Large {impacts.adpe.magnitude:.3g} \ \large {impacts.adpe.units} $$
393
  <p align="center"><i>Evaluates the use of metals and minerals<i></p><br>
 
 
394
  """)
 
395
  with gr.Column(scale=1, min_width=220):
396
  gr.Markdown(f"""
397
  <h2 align="center">⛽️ Primary Energy</h2>
398
  $$ \Large {impacts.pe.magnitude:.3g} \ \large {impacts.pe.units} $$
399
  <p align="center"><i>Evaluates the use of energy resources<i></p><br>
 
 
400
  """)
401
-
402
- with gr.Blocks():
403
- with gr.Row():
404
- gr.Markdown(f"""
405
- <h2 align="center">How can location impact the footprint ?</h2>
406
- """)
407
- with gr.Row():
408
- def create_static_bar_plot():
409
- categories = ['Sweden', 'France', 'Canada', 'USA', 'China', 'Australia', 'India']
410
- values = [46, 81, 238, 679, 1057, 1123, 1583]
411
-
412
- def addlabels(x,y):
413
- for i in range(len(x)):
414
- plt.text(i, y[i], y[i], ha = 'center')
415
-
416
- fig, ax = plt.subplots(figsize=(15,5), facecolor='#1F2937')
417
- ax.bar(categories, values)
418
- #ax.set_xlabel('Countries')
419
- ax.set_ylabel('GHG emissions (gCO2eq) for 1kWh')
420
- ax.set_title('GWP emissions for 1 kWh of electricity consumption')
421
- ax.set_facecolor("#0B0F19")
422
-
423
- addlabels(categories, values)
424
-
425
- font = {'family' : 'monospace',
426
- 'weight' : 'normal',
427
- 'size' : 14}
428
-
429
- matplotlib.rc('font', **font)
430
- matplotlib.rcParams.update({'text.color':'white',
431
- 'axes.labelcolor':'white',
432
- 'xtick.color':'white',
433
- 'ytick.color':'white'})
434
-
435
- return fig
436
-
437
- static_plot = gr.Plot(value=create_static_bar_plot())
438
 
439
  with gr.Tab("🔍 Evaluate your own usage"):
440
 
 
5
 
6
  import tiktoken
7
 
 
 
 
8
  from ecologits.tracers.utils import compute_llm_impacts, _avg
9
  from ecologits.impacts.llm import compute_llm_impacts as compute_llm_impacts_expert
10
  from ecologits.impacts.llm import IF_ELECTRICITY_MIX_GWP, IF_ELECTRICITY_MIX_ADPE, IF_ELECTRICITY_MIX_PE
 
32
  )
33
  from src.utils import (
34
  format_impacts,
35
+ format_impacts_expert,
36
  format_energy_eq_physical_activity,
37
  PhysicalActivity,
38
  format_energy_eq_electric_vehicle,
39
  format_gwp_eq_streaming, format_energy_eq_electricity_production, EnergyProduction,
40
+ format_gwp_eq_airplane_paris_nyc, format_energy_eq_electricity_consumption_ireland,
41
+ df_elec_mix_for_plot
42
  )
43
 
44
  CUSTOM = "Custom"
 
363
  if_electricity_mix_adpe=mix_adpe,
364
  if_electricity_mix_pe=mix_pe
365
  )
366
+ impacts, usage, embodied = format_impacts_expert(impacts)
367
 
368
  with gr.Blocks():
369
+
370
  with gr.Row():
371
  gr.Markdown(f"""
372
  <h2 align = "center">Environmental impacts</h2>
 
379
  $$ \Large {impacts.energy.magnitude:.3g} \ \large {impacts.energy.units} $$
380
  <p align="center"><i>Evaluates the electricity consumption<i></p><br>
381
  """)
382
+
383
  with gr.Column(scale=1, min_width=220):
384
  gr.Markdown(f"""
385
  <h2 align="center">🌍️ GHG Emissions</h2>
386
  $$ \Large {impacts.gwp.magnitude:.3g} \ \large {impacts.gwp.units} $$
387
  <p align="center"><i>Evaluates the effect on global warming<i></p><br>
388
+ $$ \Large {100*usage.gwp.value / (usage.gwp.value + embodied.gwp.value):.3} $$
389
+ <p align="center"><i>% of GWP by usage (vs embodied)<i></p><br>
390
  """)
391
+
392
  with gr.Column(scale=1, min_width=220):
393
  gr.Markdown(f"""
394
  <h2 align="center">🪨 Abiotic Resources</h2>
395
  $$ \Large {impacts.adpe.magnitude:.3g} \ \large {impacts.adpe.units} $$
396
  <p align="center"><i>Evaluates the use of metals and minerals<i></p><br>
397
+ $$ \Large {100*usage.adpe.value / (usage.adpe.value + embodied.adpe.value):.3} $$
398
+ <p align="center"><i>% of ADPE by usage (vs embodied)<i></p><br>
399
  """)
400
+
401
  with gr.Column(scale=1, min_width=220):
402
  gr.Markdown(f"""
403
  <h2 align="center">⛽️ Primary Energy</h2>
404
  $$ \Large {impacts.pe.magnitude:.3g} \ \large {impacts.pe.units} $$
405
  <p align="center"><i>Evaluates the use of energy resources<i></p><br>
406
+ $$ \Large {100*usage.pe.value / (usage.pe.value + embodied.pe.value):.3} $$
407
+ <p align="center"><i>% of PE by usage (vs embodied)<i></p><br>
408
  """)
409
+
410
+ with gr.Row():
411
+ gr.Markdown(f"""
412
+ <h2 align="center">How can location impact the footprint ?</h2>
413
+ """)
414
+
415
+ with gr.Row():
416
+ gr.BarPlot(df_elec_mix_for_plot,
417
+ x='country',
418
+ y='electricity_mix',
419
+ sort='y',
420
+ x_title=None,
421
+ y_title='electricity mix in gCO2eq / kWh')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
422
 
423
  with gr.Tab("🔍 Evaluate your own usage"):
424
 
src/utils.py CHANGED
@@ -1,6 +1,8 @@
1
  from dataclasses import dataclass
2
  from enum import Enum
3
 
 
 
4
  from ecologits.impacts.models import Impacts, Energy, GWP, ADPe, PE
5
  from pint import UnitRegistry, Quantity
6
 
@@ -50,6 +52,11 @@ COUNTRIES = [
50
  ("samoa", 100, 821_632),
51
  ]
52
 
 
 
 
 
 
53
 
54
  # From https://www.runningtools.com/energyusage.htm
55
  RUNNING_ENERGY_EQ = q("294 kJ / km") # running 1 km at 10 km/h with a weight of 70 kg
@@ -108,15 +115,23 @@ def format_pe(pe: PE) -> Quantity:
108
  val = val.to("kJ")
109
  return val
110
 
111
-
112
  def format_impacts(impacts: Impacts) -> QImpacts:
113
  return QImpacts(
114
  energy=format_energy(impacts.energy),
115
  gwp=format_gwp(impacts.gwp),
116
  adpe=format_adpe(impacts.adpe),
117
- pe=format_pe(impacts.pe),
118
  )
119
 
 
 
 
 
 
 
 
 
 
120
 
121
  def format_energy_eq_physical_activity(energy: Quantity) -> tuple[PhysicalActivity, Quantity]:
122
  energy = energy.to("kJ")
 
1
  from dataclasses import dataclass
2
  from enum import Enum
3
 
4
+ import pandas as pd
5
+
6
  from ecologits.impacts.models import Impacts, Energy, GWP, ADPe, PE
7
  from pint import UnitRegistry, Quantity
8
 
 
52
  ("samoa", 100, 821_632),
53
  ]
54
 
55
+ def df_elec_mix_for_plot():
56
+ return pd.DataFrame({
57
+ 'country': ['Sweden', 'France', 'Canada', 'USA', 'China', 'Australia', 'India'],
58
+ 'electricity_mix': [46, 81, 238, 679, 1057, 1123, 1583]
59
+ })
60
 
61
  # From https://www.runningtools.com/energyusage.htm
62
  RUNNING_ENERGY_EQ = q("294 kJ / km") # running 1 km at 10 km/h with a weight of 70 kg
 
115
  val = val.to("kJ")
116
  return val
117
 
 
118
  def format_impacts(impacts: Impacts) -> QImpacts:
119
  return QImpacts(
120
  energy=format_energy(impacts.energy),
121
  gwp=format_gwp(impacts.gwp),
122
  adpe=format_adpe(impacts.adpe),
123
+ pe=format_pe(impacts.pe)
124
  )
125
 
126
+ def format_impacts_expert(impacts: Impacts) -> QImpacts:
127
+ return QImpacts(
128
+ energy=format_energy(impacts.energy),
129
+ gwp=format_gwp(impacts.gwp),
130
+ adpe=format_adpe(impacts.adpe),
131
+ pe=format_pe(impacts.pe)
132
+
133
+ ), impacts.usage, impacts.embodied
134
+
135
 
136
  def format_energy_eq_physical_activity(energy: Quantity) -> tuple[PhysicalActivity, Quantity]:
137
  energy = energy.to("kJ")