samuelrince commited on
Commit
e9f53f0
1 Parent(s): 3a3f5f9

feat: use pint to manage units

Browse files
Files changed (3) hide show
  1. app.py +26 -5
  2. requirements-dev.txt +2 -1
  3. requirements.txt +2 -1
app.py CHANGED
@@ -1,5 +1,16 @@
1
  import gradio as gr
2
  from ecologits.tracers.utils import compute_llm_impacts
 
 
 
 
 
 
 
 
 
 
 
3
 
4
 
5
  MODELS = [
@@ -42,7 +53,7 @@ PROMPTS = [
42
  def format_indicator(name: str, value: str, unit: str) -> str:
43
  return f"""
44
  ## {name}
45
- $$ \LARGE {value} \ \small {unit} $$
46
  """
47
 
48
 
@@ -57,11 +68,21 @@ def form(
57
  output_token_count=prompt_generated_tokens,
58
  request_latency=100000
59
  )
 
 
 
 
 
 
 
 
 
 
60
  return (
61
- format_indicator("⚡️ Energy", f"{impacts.energy.value:.3f}", impacts.energy.unit),
62
- format_indicator("🌍 GHG Emissions", f"{impacts.gwp.value:.3f}", impacts.gwp.unit),
63
- format_indicator("🪨 Abiotic Resources", f"{impacts.adpe.value:.3e}", impacts.adpe.unit),
64
- format_indicator("⛽️ Primary Energy", f"{round(impacts.pe.value)}", impacts.pe.unit),
65
  )
66
 
67
 
 
1
  import gradio as gr
2
  from ecologits.tracers.utils import compute_llm_impacts
3
+ from pint import UnitRegistry
4
+
5
+ u = UnitRegistry()
6
+ u.define('kWh = kilowatt_hour')
7
+ u.define('Wh = watt_hour')
8
+ u.define('gCO2eq = gram')
9
+ u.define('kgCO2eq = kilogram')
10
+ u.define('kgSbeq = kilogram')
11
+ u.define('MJ = megajoule')
12
+ u.define('kJ = kilojoule')
13
+ q = u.Quantity
14
 
15
 
16
  MODELS = [
 
53
  def format_indicator(name: str, value: str, unit: str) -> str:
54
  return f"""
55
  ## {name}
56
+ $$ \LARGE {value} \ \large {unit} $$
57
  """
58
 
59
 
 
68
  output_token_count=prompt_generated_tokens,
69
  request_latency=100000
70
  )
71
+ energy_ = q(impacts.energy.value, impacts.energy.unit)
72
+ if energy_ < q("1 kWh"):
73
+ energy_ = energy_.to("Wh")
74
+ gwp_ = q(impacts.gwp.value, impacts.gwp.unit)
75
+ if gwp_ < q("1 kgCO2eq"):
76
+ gwp_ = gwp_.to("1 gCO2eq")
77
+ adpe_ = q(impacts.adpe.value, impacts.adpe.unit)
78
+ pe_ = q(impacts.pe.value, impacts.pe.unit)
79
+ if pe_ < q("1 MJ"):
80
+ pe_ = pe_.to("kJ")
81
  return (
82
+ format_indicator("⚡️ Energy", f"{energy_.magnitude:.3g}", energy_.units),
83
+ format_indicator("🌍 GHG Emissions", f"{gwp_.magnitude:.3g}", gwp_.units),
84
+ format_indicator("🪨 Abiotic Resources", f"{adpe_.magnitude:.3g}", adpe_.units),
85
+ format_indicator("⛽️ Primary Energy", f"{pe_.magnitude:.3g}", pe_.units),
86
  )
87
 
88
 
requirements-dev.txt CHANGED
@@ -1,2 +1,3 @@
1
  gradio
2
- ecologits
 
 
1
  gradio
2
+ ecologits
3
+ pint
requirements.txt CHANGED
@@ -1 +1,2 @@
1
- ecologits
 
 
1
+ ecologits
2
+ pint