Adrien Banse commited on
Commit
5ce3bc6
1 Parent(s): 9f964cb

Implement more information and expert mode

Browse files
Files changed (2) hide show
  1. README.md +2 -2
  2. app.py +125 -20
README.md CHANGED
@@ -16,7 +16,7 @@ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-
16
  To do:
17
 
18
  - [ ] Add a tab with basic documentation
19
- - [ ] Definition of each indicator (tool tip) (Adrien)
20
  - [ ] Basic explanation of the methodology
21
  - Why it depends on the number of tokens? (What is a token?)
22
  - What is taken into account? (usage, manufacturing for inference != training)
@@ -26,6 +26,6 @@ To do:
26
  - [ ] Call to actions: (Valentin)
27
  - Follow us on LinkedIn
28
  - Share the results of a simulation (e.g. export an image generated with plotly for instance?)
29
- - [ ] Add an advanced/expert tab (Adrien)
30
  - True number of tokens
31
  - Expose more inputs like the electricity mix
 
16
  To do:
17
 
18
  - [ ] Add a tab with basic documentation
19
+ - [x] Definition of each indicator (tool tip)
20
  - [ ] Basic explanation of the methodology
21
  - Why it depends on the number of tokens? (What is a token?)
22
  - What is taken into account? (usage, manufacturing for inference != training)
 
26
  - [ ] Call to actions: (Valentin)
27
  - Follow us on LinkedIn
28
  - Share the results of a simulation (e.g. export an image generated with plotly for instance?)
29
+ - [x] Add an advanced/expert tab
30
  - True number of tokens
31
  - Expose more inputs like the electricity mix
app.py CHANGED
@@ -1,7 +1,12 @@
 
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')
@@ -42,12 +47,13 @@ MODELS = [
42
 
43
 
44
  PROMPTS = [
 
45
  ("Write an email", 170),
46
  ("Write an article summary", 250),
47
- ("Write a Tweet", 50),
48
  ("Write a report of 5 pages", 5000),
49
- ("Small conversation with a chatbot", 400)
50
  ]
 
51
 
52
 
53
  def format_indicator(name: str, value: str, unit: str) -> str:
@@ -57,17 +63,7 @@ def format_indicator(name: str, value: str, unit: str) -> str:
57
  """
58
 
59
 
60
- def form(
61
- model_name: str,
62
- prompt_generated_tokens: int,
63
- ):
64
- provider, model_name = model_name.split('/', 1)
65
- impacts = compute_llm_impacts(
66
- provider=provider,
67
- model_name=model_name,
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")
@@ -86,15 +82,61 @@ def form(
86
  )
87
 
88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  with gr.Blocks() as demo:
 
 
 
90
  gr.Markdown("""
91
  # 🌱 EcoLogits Calculator
92
 
93
  **EcoLogits** is a python library that tracks the **energy consumption** and **environmental footprint** of using
94
  **generative AI** models through APIs.
95
 
96
- ⭐️ us on GitHub: [genai-impact/ecologits](https://github.com/genai-impact/ecologits) | Read the documentation:
97
- [ecologits.ai](https://ecologits.ai)
 
 
 
 
 
 
98
  """)
99
 
100
  with gr.Row():
@@ -106,8 +148,8 @@ with gr.Blocks() as demo:
106
  )
107
  prompt = gr.Dropdown(
108
  PROMPTS,
109
- label="Prompt",
110
- value=170
111
  )
112
 
113
  with gr.Row():
@@ -128,9 +170,72 @@ with gr.Blocks() as demo:
128
  latex_delimiters=[{"left": "$$", "right": "$$", "display": False}]
129
  )
130
 
131
- btn = gr.Button("Submit")
132
- btn.click(fn=form, inputs=[model, prompt], outputs=[energy, gwp, adpe, pe])
133
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
 
135
  if __name__ == '__main__':
136
  demo.launch()
 
1
+ from typing import Optional
2
  import gradio as gr
 
3
  from pint import UnitRegistry
4
 
5
+ from ecologits.tracers.utils import compute_llm_impacts, _avg
6
+ from ecologits.impacts.llm import compute_llm_impacts as compute_llm_impacts_expert
7
+ from ecologits.impacts.llm import IF_ELECTRICITY_MIX_GWP, IF_ELECTRICITY_MIX_ADPE, IF_ELECTRICITY_MIX_PE
8
+ from ecologits.model_repository import models
9
+
10
  u = UnitRegistry()
11
  u.define('kWh = kilowatt_hour')
12
  u.define('Wh = watt_hour')
 
47
 
48
 
49
  PROMPTS = [
50
+ ("Write a Tweet", 50),
51
  ("Write an email", 170),
52
  ("Write an article summary", 250),
53
+ ("Small conversation with a chatbot", 400),
54
  ("Write a report of 5 pages", 5000),
 
55
  ]
56
+ PROMPTS = [(s + f" ({v} output tokens)", v) for (s, v) in PROMPTS]
57
 
58
 
59
  def format_indicator(name: str, value: str, unit: str) -> str:
 
63
  """
64
 
65
 
66
+ def form_output(impacts):
 
 
 
 
 
 
 
 
 
 
67
  energy_ = q(impacts.energy.value, impacts.energy.unit)
68
  if energy_ < q("1 kWh"):
69
  energy_ = energy_.to("Wh")
 
82
  )
83
 
84
 
85
+ def form(
86
+ model_name: str,
87
+ prompt_generated_tokens: int
88
+ ):
89
+ provider, model_name = model_name.split('/', 1)
90
+ impacts = compute_llm_impacts(
91
+ provider=provider,
92
+ model_name=model_name,
93
+ output_token_count=prompt_generated_tokens,
94
+ request_latency=100000
95
+ )
96
+ return form_output(impacts)
97
+
98
+
99
+ def form_expert(
100
+ model_name: str,
101
+ prompt_generated_tokens: int,
102
+ mix_gwp: float,
103
+ mix_adpe: float,
104
+ mix_pe: float
105
+ ):
106
+ provider, model_name = model_name.split('/', 1)
107
+ model = models.find_model(provider=provider, model_name=model_name)
108
+ model_active_params = model.active_parameters or _avg(model.active_parameters_range) # TODO: handle ranges
109
+ model_total_params = model.total_parameters or _avg(model.total_parameters_range)
110
+ impacts = compute_llm_impacts_expert(
111
+ model_active_parameter_count=model_active_params,
112
+ model_total_parameter_count=model_total_params,
113
+ output_token_count=prompt_generated_tokens,
114
+ request_latency=100000,
115
+ if_electricity_mix_gwp=mix_gwp,
116
+ if_electricity_mix_adpe=mix_adpe,
117
+ if_electricity_mix_pe=mix_pe
118
+ )
119
+ return form_output(impacts)
120
+
121
+
122
  with gr.Blocks() as demo:
123
+
124
+ ### TITLE
125
+
126
  gr.Markdown("""
127
  # 🌱 EcoLogits Calculator
128
 
129
  **EcoLogits** is a python library that tracks the **energy consumption** and **environmental footprint** of using
130
  **generative AI** models through APIs.
131
 
132
+ Read the documentation:
133
+ [ecologits.ai](https://ecologits.ai) | ⭐️ us on GitHub: [genai-impact/ecologits](https://github.com/genai-impact/ecologits)
134
+ """)
135
+
136
+ ### SIMPLE CALCULATOR
137
+
138
+ gr.Markdown("""
139
+ ## 😊 Calculator
140
  """)
141
 
142
  with gr.Row():
 
148
  )
149
  prompt = gr.Dropdown(
150
  PROMPTS,
151
+ label="Example prompt",
152
+ value=50
153
  )
154
 
155
  with gr.Row():
 
170
  latex_delimiters=[{"left": "$$", "right": "$$", "display": False}]
171
  )
172
 
173
+ submit_btn = gr.Button("Submit")
174
+ submit_btn.click(fn=form, inputs=[model, prompt], outputs=[energy, gwp, adpe, pe])
175
 
176
+ ### EXPERT CALCULATOR
177
+
178
+ gr.Markdown("""
179
+ ## 🤓 Expert mode
180
+ """)
181
+ model = gr.Dropdown(
182
+ MODELS,
183
+ label="Model name",
184
+ value="openai/gpt-3.5-turbo",
185
+ filterable=True,
186
+ )
187
+ tokens = gr.Number(
188
+ label="Output tokens",
189
+ value=100
190
+ )
191
+ mix_gwp = gr.Number(
192
+ label="Electricity mix - GHG emissions [kgCO2eq / kWh]",
193
+ value=IF_ELECTRICITY_MIX_GWP
194
+ )
195
+ mix_adpe = gr.Number(
196
+ label="Electricity mix - Abiotic resources [kgSbeq / kWh]",
197
+ value=IF_ELECTRICITY_MIX_ADPE
198
+ )
199
+ mix_pe = gr.Number(
200
+ label="Electricity mix - Primary energy [MJ / kWh]",
201
+ value=IF_ELECTRICITY_MIX_PE
202
+ )
203
+
204
+ with gr.Row():
205
+ energy = gr.Markdown(
206
+ label="energy",
207
+ latex_delimiters=[{"left": "$$", "right": "$$", "display": False}]
208
+ )
209
+ gwp = gr.Markdown(
210
+ label="gwp",
211
+ latex_delimiters=[{"left": "$$", "right": "$$", "display": False}]
212
+ )
213
+ adpe = gr.Markdown(
214
+ label="adpe",
215
+ latex_delimiters=[{"left": "$$", "right": "$$", "display": False}]
216
+ )
217
+ pe = gr.Markdown(
218
+ label="pe",
219
+ latex_delimiters=[{"left": "$$", "right": "$$", "display": False}]
220
+ )
221
+
222
+ submit_btn = gr.Button("Submit")
223
+ submit_btn.click(
224
+ fn=form_expert,
225
+ inputs=[model, tokens, mix_gwp, mix_adpe, mix_pe],
226
+ outputs=[energy, gwp, adpe, pe]
227
+ )
228
+
229
+ ### INFORMATION ABOUT INDICATORS
230
+
231
+ gr.Markdown("""
232
+ ## 📊 More about the indicators
233
+
234
+ - ⚡️ **Energy**: Final energy consumption,
235
+ - 🌍 **GHG Emissions**: Potential impact on global warming (commonly known as GHG/carbon emissions),
236
+ - 🪨 **Abiotic Resources**: Impact on the depletion of non-living resources such as minerals or metals,
237
+ - ⛽️ **Primary Energy**: Total energy consumed from primary sources.
238
+ """)
239
 
240
  if __name__ == '__main__':
241
  demo.launch()