Spaces:
Sleeping
Sleeping
Valentin D. commited on
Commit ·
faf13f8
1
Parent(s): 197f5ec
update: filter models + clean files
Browse filesFiles changed (22)
- __init__.pyDeleted-8
- calculator.pyDeleted-67
- constants.pyDeleted-106
- content.pyDeleted-232
- electricity_mix.pyDeleted-175
- expert.pyDeleted-156
- impacts.pyDeleted-116
- models.pyDeleted-46
- src/__pycache__/__init__.cpython-312.pyc
- src/__pycache__/calculator.cpython-312.pyc
- src/__pycache__/constants.cpython-312.pyc
- src/__pycache__/content.cpython-312.pyc
- src/__pycache__/electricity_mix.cpython-312.pyc
- src/__pycache__/expert.cpython-312.pyc
- src/__pycache__/impacts.cpython-312.pyc
- src/__pycache__/models.cpython-312.pyc
- src/__pycache__/utils.cpython-312.pyc
- src/calculator.py+15-8
- src/constants.py+85-89
- src/models.py+12-5
- utils.pyDeleted-262
- uv.lock-1
12345678from .content import *
from .constants import *
from .expert import expert_mode
from .utils import *
from .calculator import calculator_mode
from .impacts import get_impacts, display_impacts
from .models import load_models
from .electricity_mix import *12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667import streamlit as st
import pandas as pd
from ecologits.tracers.utils import llm_impacts
from src.impacts import get_impacts, display_impacts, display_equivalent
from src.utils import format_impacts
from src.content import WARNING_CLOSED_SOURCE, WARNING_MULTI_MODAL, WARNING_BOTH
from src.models import load_models, clean_models_data
from src.constants import PROMPTS
def calculator_mode():
with st.container(border=True):
df = load_models()
col1, col2, col3 = st.columns(3)
with col1:
provider = st.selectbox(label = 'Provider',
options = [x for x in df['provider_clean'].unique()],
index = 9)
provider_raw = df[df['provider_clean'] == provider]['provider'].values[0]
with col2:
model = st.selectbox('Model', [x for x in df['name_clean'].unique() if x in df[df['provider_clean'] == provider]['name_clean'].unique()])
model_raw = df[(df['provider_clean'] == provider) & (df['name_clean'] == model)]['name'].values[0]
with col3:
output_tokens = st.selectbox('Example prompt', [x[0] for x in PROMPTS])
# WARNING DISPLAY
df_filtered = df[(df['provider_clean'] == provider) & (df['name_clean'] == model)]
if df_filtered['warning_arch'].values[0] and not df_filtered['warning_multi_modal'].values[0]:
st.warning(WARNING_CLOSED_SOURCE)
if df_filtered['warning_multi_modal'].values[0] and not df_filtered['warning_arch'].values[0]:
st.warning(WARNING_MULTI_MODAL)
if df_filtered['warning_arch'].values[0] and df_filtered['warning_multi_modal'].values[0]:
st.warning(WARNING_BOTH)
try:
impacts = llm_impacts(
provider=provider_raw,
model_name=model_raw,
output_token_count=[x[1] for x in PROMPTS if x[0] == output_tokens][0],
request_latency=100000
)
impacts, _, _ = format_impacts(impacts)
with st.container(border=True):
st.markdown('<h3 align = "center">Environmental impacts</h3>', unsafe_allow_html=True)
st.markdown('<p align = "center">To understand how the environmental impacts are computed go to the 📖 Methodology tab.</p>', unsafe_allow_html=True)
display_impacts(impacts)
with st.container(border=True):
st.markdown('<h3 align = "center">That\'s equivalent to ...</h3>', unsafe_allow_html=True)
st.markdown('<p align = "center">Making this request to the LLM is equivalent to the following actions :</p>', unsafe_allow_html=True)
display_equivalent(impacts)
except Exception as e:
st.error('Could not find the model in the repository. Please try another model.')123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106PROVIDERS = [
("OpenAI", "openai"),
("Anthropic", "anthropic"),
("Cohere", "cohere"),
("Meta", "huggingface_hub/meta"),
("Mistral AI", "mistralai"),
]
OPENAI_MODELS = [
("GPT-4o", "gpt-4o"),
("GPT-4-Turbo", "gpt-4-turbo"),
("GPT-4", "gpt-4"),
("GPT-3.5-Turbo", "gpt-3.5-turbo"),
]
ANTHROPIC_MODELS = [
("Claude 3 Opus", "claude-3-opus-20240229"),
("Claude 3 Sonnet", "claude-3-sonnet-20240229"),
("Claude 3 Haiku", "claude-3-haiku-20240307"),
("Claude 2.1", "claude-2.1"),
("Claude 2.0", "claude-2.0"),
("Claude Instant 1.2", "claude-instant-1.2"),
]
COHERE_MODELS = [
("Command Light", "command-light"),
("Command", "command"),
("Command R", "command-r"),
("Command R+", "command-r-plus"),
]
META_MODELS = [
("Llama 3 8B", "meta-llama/Meta-Llama-3-8B"),
("Llama 3 70B", "meta-llama/Meta-Llama-3-70B"),
("Llama 2 7B", "meta-llama/Llama-2-7b-hf"),
("Llama 2 13B", "meta-llama/Llama-2-13b-hf"),
("Llama 2 70B", "meta-llama/Llama-2-70b-hf"),
]
MISTRALAI_MODELS = [
("Mistral 7B", "open-mistral-7b"),
("Mixtral 8x7B", "open-mixtral-8x7b"),
("Mixtral 8x22B", "open-mixtral-8x22b"),
("Tiny", "mistral-tiny-2312"),
("Small", "mistral-small-2402"),
("Medium", "mistral-medium-2312"),
("Large", "mistral-large-2402"),
]
PROMPTS = [
("Write a Tweet", 50),
("Write an email", 170),
("Write an article summary", 250),
("Small conversation with a chatbot", 400),
("Write a report of 5 pages", 5000),
("Write the code for this app", 10000)
]
PROMPTS = [(s + f" ({v} output tokens)", v) for (s, v) in PROMPTS]
CLOSED_SOURCE_MODELS = {
"openai/gpt-4o",
"openai/gpt-4-turbo",
"openai/gpt-4",
"openai/gpt-3.5-turbo",
"anthropic/claude-3-opus-20240229",
"anthropic/claude-3-sonnet-20240229",
"anthropic/claude-3-haiku-20240307",
"anthropic/claude-2.1",
"anthropic/claude-2.0",
"anthropic/claude-instant-1.2",
"mistralai/mistral-tiny-2312",
"mistralai/mistral-small-2402",
"mistralai/mistral-medium-2312",
"mistralai/mistral-large-2402",
}
MODELS = [
("OpenAI / GPT-4o", "openai/gpt-4o"),
("OpenAI / GPT-4-Turbo", "openai/gpt-4-turbo"),
("OpenAI / GPT-4", "openai/gpt-4"),
("OpenAI / GPT-3.5-Turbo", "openai/gpt-3.5-turbo"),
("Anthropic / Claude 3 Opus", "anthropic/claude-3-opus-20240229"),
("Anthropic / Claude 3 Sonnet", "anthropic/claude-3-sonnet-20240229"),
("Anthropic / Claude 3 Haiku", "anthropic/claude-3-haiku-20240307"),
("Anthropic / Claude 2.1", "anthropic/claude-2.1"),
("Anthropic / Claude 2.0", "anthropic/claude-2.0"),
("Anthropic / Claude Instant 1.2", "anthropic/claude-instant-1.2"),
("Mistral AI / Mistral 7B", "mistralai/open-mistral-7b"),
("Mistral AI / Mixtral 8x7B", "mistralai/open-mixtral-8x7b"),
("Mistral AI / Mixtral 8x22B", "mistralai/open-mixtral-8x22b"),
("Mistral AI / Tiny", "mistralai/mistral-tiny-2312"),
("Mistral AI / Small", "mistralai/mistral-small-2402"),
("Mistral AI / Medium", "mistralai/mistral-medium-2312"),
("Mistral AI / Large", "mistralai/mistral-large-2402"),
("Meta / Llama 3 8B", "huggingface_hub/meta-llama/Meta-Llama-3-8B"),
("Meta / Llama 3 70B", "huggingface_hub/meta-llama/Meta-Llama-3-70B"),
("Meta / Llama 2 7B", "huggingface_hub/meta-llama/Llama-2-7b-hf"),
("Meta / Llama 2 13B", "huggingface_hub/meta-llama/Llama-2-13b-hf"),
("Meta / Llama 2 70B", "huggingface_hub/meta-llama/Llama-2-70b-hf"),
("Cohere / Command Light", "cohere/command-light"),
("Cohere / Command", "cohere/command"),
("Cohere / Command R", "cohere/command-r"),
("Cohere / Command R+", "cohere/command-r-plus"),
]
MODEL_REPOSITORY_URL = "https://raw.githubusercontent.com/genai-impact/ecologits/refs/heads/main/ecologits/data/models.json"123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232HERO_TEXT = """
<div align="center">
<a href="https://ecologits.ai/">
<img style="max-height: 80px" alt="EcoLogits" src="https://raw.githubusercontent.com/genai-impact/ecologits/main/docs/assets/logo_light.png">
</a>
</div>
<h1 align="center">🧮 EcoLogits Calculator</h1>
<div align="center">
<p style="max-width: 500px; text-align: center">
<i><b>EcoLogits</b> is a python library that tracks the <b>energy consumption</b> and <b>environmental
footprint</b> of using <b>generative AI</b> models through APIs.</i>
</p>
</div>
<br>
"""
INTRO_TEXT = """
This tool is developed and maintained by [GenAI Impact](https://genai-impact.org/) non-profit. Learn more about
🌱 EcoLogits by reading the documentation on [ecologits.ai](https://ecologits.ai).
🩷 Support us by giving a ⭐️ on our [GitHub repository](https://github.com/genai-impact/ecologits) and by following our [LinkedIn page](https://www.linkedin.com/company/genai-impact/).
"""
WARNING_CLOSED_SOURCE = """
⚠️ The model architecture has not been publicly released, expect lower precision of estimations.
"""
WARNING_MULTI_MODAL = """
⚠️ The model architecture is multimodal, expect lower precision of estimations.
"""
WARNING_BOTH = """
⚠️ The model architecture has not been publicly released and is multimodal, expect lower precision of estimations.
"""
ABOUT_TEXT = r"""
## 🎯 Our goal
**The main goal of the EcoLogits Calculator is to raise awareness on the environmental impacts of LLM inference.**
The rapid evolution of generative AI is reshaping numerous industries and aspects of our daily lives. While these
advancements offer some benefits, they also **pose substantial environmental challenges that cannot be overlooked**.
Plus the issue of AI's environmental footprint has been mainly discussed at training stage but rarely at the inference
stage. That is an issue because **inference impacts for LLMs can largely overcome the training impacts when deployed
at large scales**.
At **[GenAI Impact](https://genai-impact.org/) we are dedicated to understanding and mitigating the environmental
impacts of generative AI** through rigorous research, innovative tools, and community engagement. Especially, in early
2024 we have launched an new open-source tool called [EcoLogits](https://github.com/genai-impact/ecologits) that tracks
the energy consumption and environmental footprint of using generative AI models through APIs.
## 🙋 FAQ
**How we assess the impacts of closed-source models?**
Environmental impacts are calculated based on model architecture and parameter count. For closed-source models, we
lack transparency from providers, so we estimate parameter counts using available information. For GPT models, we
based our estimates on leaked GPT-4 architecture and scaled parameters count for GPT-4-Turbo and GPT-4o based on
pricing differences. For other proprietary models like Anthropic's Claude, we assume similar impacts for models
released around the same time with similar performance on public benchmarks. Please note that these estimates are
based on assumptions and may not be exact. Our methods are open-source and transparent so you can always see the
hypotheses we use.
**Which generative AI models or providers are supported?**
To see the full list of **generative AI providers** currently supported by EcoLogits, see the following
[documentation page](https://ecologits.ai/providers/). As of today we only support LLMs but we plan to add support for
embeddings, image generation, multi-modal models and more. If you are interested don't hesitate to
[join us](https://genai-impact.org/contact/) and accelerate our work!
**How to reduce AI environmental impacts?**
* Look at **indirect impacts** of your project. Does the finality of your project is impacting negatively the
environment?
* **Be frugal** and question your usage or need of AI
* Do you really need AI to solve your problem?
* Do you really need GenAI to solve your problem? (you can read this [paper](https://aclanthology.org/2023.emnlp-industry.39.pdf))
* Use small and specialized models to solve your problem.
* Evaluate before, during and after the development of your project the environmental impacts with tools like
🌱 [EcoLogits](https://github.com/genai-impact/ecologits) or [CodeCarbon](https://github.com/mlco2/codecarbon)
(see [more tools](https://github.com/samuelrince/awesome-green-ai))
* Restrict the use case and limit the usage of your tool or feature to the desired purpose.
* Do NOT buy new GPUs / hardware
* Hardware manufacturing for data centers is around 50% of the impact.
* Use cloud instances that are located in low emissions / high energy efficiency data centers
(see [electricitymaps.com](https://app.electricitymaps.com/map))
* Optimize your models for production
* Quantize your models.
* Use inference optimization tricks.
* Prefer fine-tuning of small and existing models over generalist models.
**What is the difference between **EcoLogits** and [CodeCarbon](https://github.com/mlco2/codecarbon)?**
EcoLogits is focused on estimating the environmental impacts of generative AI (only LLMs for now) used **through API
providers (such as OpenAI, Anthropic, Cloud APIs...)** whereas CodeCarbon is more general tool to measure energy
consumption and estimate GHG emissions measurement. If you deploy LLMs locally we encourage you to use CodeCarbon to
get real numbers of your energy consumption.
## 🤗 Contributing
We are eager to get feedback from the community, don't hesitate to engage the discussion with us on this
[GitHub thread](https://github.com/genai-impact/ecologits/discussions/45) or message us on
[LinkedIn](https://www.linkedin.com/company/genai-impact/).
We also welcome any open-source contributions on 🌱 **[EcoLogits](https://github.com/genai-impact/ecologits)** or on
🧮 **EcoLogits Calculator**.
## ⚖️ License
<p xmlns:cc="http://creativecommons.org/ns#" >
This work is licensed under
<a href="https://creativecommons.org/licenses/by-sa/4.0/?ref=chooser-v1" target="_blank" rel="license noopener noreferrer" style="display:inline-block;">
CC BY-SA 4.0
</a>
<img style="display:inline-block;height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/cc.svg?ref=chooser-v1" alt="">
<img style="display:inline-block;height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/by.svg?ref=chooser-v1" alt="">
<img style="display:inline-block;height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/sa.svg?ref=chooser-v1" alt="">
</p>
## 🙌 Acknowledgement
We thank [Data For Good](https://dataforgood.fr/) and [Boavizta](https://boavizta.org/en) for supporting the
development of this project. Their contributions of tools, best practices, and expertise in environmental impact
assessment have been invaluable.
We also extend our gratitude to the open-source contributions of 🤗 [Hugging Face](huggingface.com) on the LLM-Perf
Leaderboard.
## 🤝 Contact
For general question on the project, please use the [GitHub thread](https://github.com/genai-impact/ecologits/discussions/45).
Otherwise use our contact form on [genai-impact.org/contact](https://genai-impact.org/contact/).
"""
METHODOLOGY_TEXT = r"""
## 📖 Methodology
We have developed a methodology to **estimate the energy consumption and environmental impacts for an LLM inference**
based on request parameters and hypotheses on the data center location, the hardware used, the model architecture and
more.
In this section we will only cover the principles of the methodology related to the 🧮 **EcoLogits Calculator**. If
you wish to learn more on the environmental impacts modeling of an LLM request checkout the
🌱 [EcoLogits documentation page](https://ecologits.ai/methodology/).
### Modeling impacts of an LLM request
The environmental impacts of an LLM inference are split into the **usage impacts** $I_{request}^u$ to account for
electricity consumption and the **embodied impacts** $I_{request}^e$ that relates to resource extraction, hardware
manufacturing and transportation. In general terms it can be expressed as follow:
$$ I_{request} = I_{request}^u + I_{request}^e $$
$$ I_{request} = E_{request}*F_{em}+\frac{\Delta T}{\Delta L}*I_{server}^e $$
With,
* $E_{request}$ the estimated energy consumption of the server and its cooling system.
* $F_{em}$ the electricity mix that depends on the country and time.
* $\frac{\Delta T}{\Delta L}$ the hardware usage ratio i.e. the computation time over the lifetime of the hardware.
* $I_{server}^e$ the embodied impacts of the server.
Additionally, to ⚡️ **direct energy consumption** the environmental impacts are expressed in **three dimensions
(multi-criteria impacts)** that are:
* 🌍 **Global Warming Potential** (GWP): Potential impact on global warming in kgCO2eq (commonly known as GHG/carbon
emissions).
* 🪨 **Abiotic Depletion Potential for Elements** (ADPe): Impact on the depletion of non-living resources such as
minerals or metals in kgSbeq.
* ⛽️ **Primary Energy** (PE): Total energy consumed from primary sources in MJ.
### Principles, Data and Hypotheses
We use a **bottom-up methodology** to model impacts, meaning that we will estimate the impacts of low-level physical
components to then estimate the impacts at software level (in that case an LLM inference). We also rely on **Life
Cycle Approach (LCA) proxies and approach** to model both usage and embodied phases with multi-criteria impacts.
If you are interested in this approach we recommend you to read the following [Boavizta](https://boavizta.org/)
resources.
* [Digital & environment: How to evaluate server manufacturing footprint, beyond greenhouse gas emissions?](https://boavizta.org/en/blog/empreinte-de-la-fabrication-d-un-serveur)
* [Boavizta API automated evaluation of environmental impacts of ICT services and equipments](https://boavizta.org/en/blog/boavizta-api-automated-evaluation-of-ict-impacts-on-the-environment)
* [Boavizta API documentation](https://doc.api.boavizta.org/)
We leverage **open data to estimate the environmental impacts**, here is an exhaustive list of our data providers.
* [LLM-Perf Leaderboard](https://huggingface.co/spaces/optimum/llm-perf-leaderboard) to estimate GPU energy consumption
and latency based on the model architecture and number of output tokens.
* [Boavizta API](https://github.com/Boavizta/boaviztapi) to estimate server embodied impacts and base energy
consumption.
* [ADEME Base Empreinte®](https://base-empreinte.ademe.fr/) for electricity mix impacts per country.
Finally here are the **main hypotheses** we have made to compute the impacts.
* ⚠️ **We *"guesstimate"* the model architecture of proprietary LLMs when not disclosed by the provider.**
* Production setup: quantized models running on data center grade servers and GPUs such as A100.
* Electricity mix does not depend on time (help us enhance EcoLogits and work on this [issue](https://github.com/genai-impact/ecologits/issues/42))
* Ignore the following impacts: unused cloud resources, data center building, network and end-user devices... (for now)
## Equivalents
We have integrated impact equivalents to help people better understand the impacts and have reference points for
standard use cases and everyday activities.
### Request impacts
These equivalents are computed based on the request impacts only.
#### 🚶♂️➡️ Walking or 🏃♂️➡️ running distance
We compare the ⚡️ direct energy consumption with the energy consumption of someone 🚶♂️➡️ walking or 🏃♂️➡️ running.
From [runningtools.com](https://www.runningtools.com/energyusage.htm) we consider the following energy values per
physical activity (for someone weighing 70kg):
* 🚶♂️➡️ walking: $ 196\ kJ/km $ (speed of $ 3\ km/h $)
* 🏃♂️➡️ running: $ 294\ kJ/km $ (speed of $ 10\ km/h $)
We divide the request energy consumption by these values to compute the distance traveled.
#### 🔋 Electric Vehicle distance
We compare the ⚡️ direct energy consumption with the energy consumer by a EV car. From
[selectra.info](https://selectra.info/energie/actualites/insolite/consommation-vehicules-electriques-france-2040) or
[tesla.com](https://www.tesla.com/fr_fr/support/power-consumption) we consider an average value of energy consumed per
kilometer of: $ 0.17\ kWh/km $.
We divide the request energy consumption by this value to compute the distance driven by an EV.
#### ⏯️ Streaming time
We compare the 🌍 GHG emissions of the request and of streaming a video. From
[impactco2.fr](https://impactco2.fr/outils/comparateur?value=1&comparisons=streamingvideo), we consider that
$ 1\ kgCO2eq $ is equivalent to $ 15.6\ h $ of streaming.
We multiply that value by the GHG emissions of the request to get an equivalent in hours of video streaming.
### Scaled impacts
These equivalents are computed based on the request impacts scaled to a worldwide adoption use case. We imply that the
same request is done 1% of the planet everyday for 1 year, and then compute impact equivalents.
$$
I_{scaled} = I_{request} * [1 \\% \ \text{of}\ 8B\ \text{people on earth}] * 365\ \text{days}
$$
#### Number of 💨 wind turbines or ☢️ nuclear plants
We compare the ⚡️ direct energy consumption (scaled) by the energy production of wind turbines and nuclear power
plants. From [ecologie.gouv.fr](https://www.ecologie.gouv.fr/eolien-terrestre) we consider that a $ 2\ MW $ wind
turbine produces $ 4.2\ GWh $ a year. And from [edf.fr](https://www.edf.fr/groupe-edf/espaces-dedies/jeunes-enseignants/pour-les-jeunes/lenergie-de-a-a-z/produire-de-lelectricite/le-nucleaire-en-chiffres)
we learn that a $ 900\ MW $ nuclear power plant produces $ 6\ TWh $ a year.
We divide the scaled energy consumption by these values to get the number of wind turbines or nuclear power plants
needed.
#### Multiplier of 🇮🇪 Ireland electricity consumption
We compare the ⚡️ direct energy consumption (scaled) by the electricity consumption of Ireland per year. From
[wikipedia.org](https://en.wikipedia.org/wiki/List_of_countries_by_electricity_consumption) we consider the Ireland
electricity consumption to be $ 33\ TWh $ a year for a population of 5M.
We divide the scaled energy consumption by this value to get the equivalent number of "Ireland countries".
#### Number of ✈️ Paris ↔ New York City flights
We compare the 🌍 GHG emissions (scaled) of the request and of a return flight Paris ↔ New York City. From
[impactco2.fr](https://impactco2.fr/outils/comparateur?value=1&comparisons=&equivalent=avion-pny) we consider that a
return flight Paris → New York City → Paris for one passenger emits $ 1,770\ kgCO2eq $ and we consider an overall
average load of 100 passengers per flight.
We divide the scaled GHG emissions by this value to get the equivalent number of return flights.
#### If you are motivated to help us test and enhance this methodology [contact us](https://genai-impact.org/contact/)! 💪
"""
CITATION_LABEL = "BibTeX citation for EcoLogits Calculator and the EcoLogits library:"
CITATION_TEXT = r"""@misc{ecologits-calculator,
author={Samuel Rincé, Adrien Banse and Valentin Defour},
title={EcoLogits Calculator},
year={2025},
howpublished= {\url{https://huggingface.co/spaces/genai-impact/ecologits-calculator}},
}
@software{ecologits,
author = {Samuel Rincé, Adrien Banse, Vinh Nguyen and Luc Berton},
publisher = {GenAI Impact},
title = {EcoLogits: track the energy consumption and environmental footprint of using generative AI models through APIs.},
}"""
LICENCE_TEXT = """<p xmlns:cc="http://creativecommons.org/ns#" >
This work is licensed under
<a href="https://creativecommons.org/licenses/by-sa/4.0/?ref=chooser-v1" target="_blank" rel="license noopener noreferrer" style="display:inline-block;">
CC BY-SA 4.0
</a>
<img style="display:inline-block;height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/cc.svg?ref=chooser-v1" alt="">
<img style="display:inline-block;height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/by.svg?ref=chooser-v1" alt="">
<img style="display:inline-block;height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/sa.svg?ref=chooser-v1" alt="">
</p>"""123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175from csv import DictReader
import pandas as pd
PATH = "src/data/electricity_mix.csv"
COUNTRY_CODES = [
("🌎 World", "WOR"),
("🇪🇺 Europe", "EEE"),
("🇿🇼 Zimbabwe", "ZWE"),
("🇿🇲 Zambia", "ZMB"),
("🇿🇦 South Africa", "ZAF"),
("🇾🇪 Yemen", "YEM"),
("🇻🇳 Vietnam", "VNM"),
("🇻🇪 Venezuela", "VEN"),
("🇺🇿 Uzbekistan", "UZB"),
("🇺🇾 Uruguay", "URY"),
("🇺🇸 United States", "USA"),
("🇺🇦 Ukraine", "UKR"),
("🇹🇿 Tanzania", "TZA"),
("🇹🇼 Taiwan", "TWN"),
("🇹🇹 Trinidad and Tobago", "TTO"),
("🇹🇷 Turkey", "TUR"),
("🇹🇳 Tunisia", "TUN"),
("🇹🇲 Turkmenistan", "TKM"),
("🇹🇯 Tajikistan", "TJK"),
("🇹🇭 Thailand", "THA"),
("🇹🇬 Togo", "TGO"),
("🇸🇾 Syrian Arab Republic", "SYR"),
("🇸🇻 El Salvador", "SLV"),
("🇸🇳 Senegal", "SEN"),
("🇸🇰 Slovak Republic", "SVK"),
("🇸🇮 Slovenia", "SVN"),
("🇸🇬 Singapore", "SGP"),
("🇸🇪 Sweden", "SWE"),
("🇸🇩 Sudan", "SDN"),
("🇸🇦 Saudi Arabia", "SAU"),
("🇷🇺 Russian Federation", "RUS"),
("🇷🇸 Serbia and Montenegro", "SCG"),
("🇷🇴 Romania", "ROU"),
("🇶🇦 Qatar", "QAT"),
("🇵🇾 Paraguay", "PRY"),
("🇵🇹 Portugal", "PRT"),
("🇵🇱 Poland", "POL"),
("🇵🇰 Pakistan", "PAK"),
("🇵🇭 Philippines", "PHL"),
("🇵🇪 Peru", "PER"),
("🇵🇦 Panama", "PAN"),
("🇴🇲 Oman", "OMN"),
("🇳🇿 New Zealand", "NZL"),
("🇳🇵 Nepal", "NPL"),
("🇳🇴 Norway", "NOR"),
("🇳🇱 Netherlands", "NLD"),
("🇳🇮 Nicaragua", "NIC"),
("🇳🇬 Nigeria", "NGA"),
("🇳🇦 Namibia", "NAM"),
("🇲🇿 Mozambique", "MOZ"),
("🇲🇾 Malaysia", "MYS"),
("🇲🇽 Mexico", "MEX"),
("🇲🇹 Malta", "MLT"),
("🇲🇳 Mongolia", "MNG"),
("🇲🇲 Myanmar", "MMR"),
("🇲🇰 North Macedonia", "MKD"),
("🇲🇩 Moldova", "MDA"),
("🇲🇦 Morocco", "MAR"),
("🇱🇾 Libya", "LBY"),
("🇱🇻 Latvia", "LVA"),
("🇱🇺 Luxembourg", "LUX"),
("🇱🇹 Lithuania", "LTU"),
("🇱🇰 Sri Lanka", "LKA"),
("🇱🇧 Lebanon", "LBN"),
("🇰🇿 Kazakhstan", "KAZ"),
("🇰🇼 Kuwait", "KWT"),
("🇰🇷 South Korea", "KOR"),
("🇰🇵 North Korea", "PRK"),
("🇰🇭 Cambodia", "KHM"),
("🇰🇬 Kyrgyz Republic", "KGZ"),
("🇰🇪 Kenya", "KEN"),
("🇯🇵 Japan", "JPN"),
("🇯🇴 Jordan", "JOR"),
("🇯🇲 Jamaica", "JAM"),
("🇮🇹 Italy", "ITA"),
("🇮🇸 Iceland", "ISL"),
("🇮🇷 Iran", "IRN"),
("🇮🇶 Iraq", "IRQ"),
("🇮🇳 India", "IND"),
("🇮🇱 Israel", "ISR"),
("🇮🇪 Ireland", "IRL"),
("🇮🇩 Indonesia", "IDN"),
("🇭🇺 Hungary", "HUN"),
("🇭🇹 Haiti", "HTI"),
("🇭🇷 Croatia", "HRV"),
("🇭🇳 Honduras", "HND"),
("🇭🇰 Hong Kong", "HKG"),
("🇬🇹 Guatemala", "GTM"),
("🇬🇷 Greece", "GRC"),
("🇬🇮 Gibraltar", "GIB"),
("🇬🇭 Ghana", "GHA"),
("🇬🇪 Georgia", "GEO"),
("🇬🇧 United Kingdom", "GBR"),
("🇬🇦 Gabon", "GAB"),
("🇫🇷 France", "FRA"),
("🇫🇮 Finland", "FIN"),
("🇪🇹 Ethiopia", "ETH"),
("🇪🇸 Spain", "ESP"),
("🇪🇷 Eritrea", "ERI"),
("🇪🇬 Egypt", "EGY"),
("🇪🇪 Estonia", "EST"),
("🇪🇨 Ecuador", "ECU"),
("🇩🇿 Algeria", "DZA"),
("🇩🇴 Dominican Republic", "DOM"),
("🇩🇰 Denmark", "DNK"),
("🇩🇪 Germany", "DEU"),
("🇨🇿 Czech Republic", "CZE"),
("🇨🇾 Cyprus", "CYP"),
("🇨🇺 Cuba", "CUB"),
("🇨🇷 Costa Rica", "CRI"),
("🇨🇴 Colombia", "COL"),
("🇨🇳 China", "CHN"),
("🇨🇲 Cameroon", "CMR"),
("🇨🇱 Chile", "CHL"),
("🇨🇮 Cote d'Ivoire", "CIV"),
("🇨🇭 Switzerland", "CHE"),
("🇨🇬 Congo", "COG"),
("🇨🇩 Democratic Republic of the Congo", "COD"),
("🇨🇦 Canada", "CAN"),
("🇧🇾 Belarus", "BLR"),
("🇧🇼 Botswana", "BWA"),
("🇧🇷 Brazil", "BRA"),
("🇧🇴 Bolivia", "BOL"),
("🇧🇳 Brunei", "BRN"),
("🇧🇯 Benin", "BEN"),
("🇧🇭 Bahrain", "BHR"),
("🇧🇬 Bulgaria", "BGR"),
("🇧🇪 Belgium", "BEL"),
("🇧🇩 Bangladesh", "BGD"),
("🇧🇦 Bosnia and Herzegovina", "BIH"),
("🇦🇿 Azerbaijan", "AZE"),
("🇦🇺 Australia", "AUS"),
("🇦🇹 Austria", "AUT"),
("🇦🇷 Argentina", "ARG"),
("🇦🇴 Angola", "AGO"),
("🇦 Netherlands Antilles", "ANT"),
("🇦🇲 Armenia", "ARM"),
("🇦🇱 Albania", "ALB"),
("🇦🇪 United Arab Emirates", "ARE")
]
def find_electricity_mix(code: str):
# TODO: Maybe more optimal to construct database at the beginning of the app
# in the same fashion as find_model
res = []
with open(PATH) as fd:
csv = DictReader(fd)
for row in csv:
res += [float(row[code])]
return res
def dataframe_electricity_mix(countries: list):
df = pd.read_csv('src/data/electricity_mix.csv')
df['name_unit'] = df['name'] + ' (' + df['unit'] + ')'
df = df[['name_unit'] + [x[1] for x in COUNTRY_CODES if x[0] in countries]]
df_melted = df.melt(
id_vars=['name_unit'],
value_vars=[x[1] for x in COUNTRY_CODES if x[0] in countries],
var_name='country',
value_name='value')
df = df_melted.pivot(columns='name_unit',
index='country',
values='value')
return df123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156import streamlit as st
import pandas as pd
from ecologits.impacts.llm import compute_llm_impacts
from src.utils import format_impacts, average_range_impacts, format_impacts_expert, model_active_params_fn, model_total_params_fn
from src.impacts import display_impacts
#from src.constants import PROVIDERS, MODELS
from src.electricity_mix import COUNTRY_CODES, find_electricity_mix, dataframe_electricity_mix
from ecologits.model_repository import models
import plotly.express as px
def reset_model():
model = 'CUSTOM'
def expert_mode():
st.markdown("### 🤓 Expert mode")
with st.container(border = True):
########## Model info ##########
# col1, col2, col3 = st.columns(3)
# with col1:
# provider = st.selectbox(label = 'Provider expert',
# options = [x[0] for x in PROVIDERS],
# index = 0)
# provider = [x[1] for x in PROVIDERS if x[0] == provider][0]
# if 'huggingface_hub' in provider:
# provider = 'huggingface_hub'
# with col2:
# model = st.selectbox('Model expert', [x[0] for x in MODELS if provider in x[1]])
# model = [x[1] for x in MODELS if x[0] == model][0].split('/', 1)[1]
########## Model parameters ##########
col11, col22, col33 = st.columns(3)
with col11:
# st.write(provider, model)
# st.write(models.find_model(provider, model))
# st.write(model_active_params_fn(provider, model, 45))
active_params = st.number_input('Active parameters (B)', 0, None, 45)
with col22:
total_params = st.number_input('Total parameters (B)', 0, None, 45)
with col33:
output_tokens = st.number_input('Output completion tokens', 100)
########## Electricity mix ##########
location = st.selectbox('Location', [x[0] for x in COUNTRY_CODES])
col4, col5, col6 = st.columns(3)
with col4:
mix_gwp = st.number_input('Electricity mix - GHG emissions [kgCO2eq / kWh]', find_electricity_mix([x[1] for x in COUNTRY_CODES if x[0] ==location][0])[2], format="%0.6f")
#disp_ranges = st.toggle('Display impact ranges', False)
with col5:
mix_adpe = st.number_input('Electricity mix - Abiotic resources [kgSbeq / kWh]', find_electricity_mix([x[1] for x in COUNTRY_CODES if x[0] ==location][0])[0], format="%0.13f")
with col6:
mix_pe = st.number_input('Electricity mix - Primary energy [MJ / kWh]', find_electricity_mix([x[1] for x in COUNTRY_CODES if x[0] ==location][0])[1], format="%0.3f")
impacts = compute_llm_impacts(model_active_parameter_count=active_params,
model_total_parameter_count=total_params,
output_token_count=output_tokens,
request_latency=100000,
if_electricity_mix_gwp=mix_gwp,
if_electricity_mix_adpe=mix_adpe,
if_electricity_mix_pe=mix_pe
)
impacts, usage, embodied = format_impacts(impacts)
with st.container(border = True):
st.markdown('<h3 align="center">Environmental Impacts</h2>', unsafe_allow_html = True)
display_impacts(impacts)
with st.expander('⚖️ Usage vs Embodied'):
st.markdown('<h3 align="center">Embodied vs Usage comparison</h2>', unsafe_allow_html = True)
st.markdown('The usage impacts account for the electricity consumption of the model while the embodied impacts account for resource extraction (e.g., minerals and metals), manufacturing, and transportation of the hardware.')
col_ghg_comparison, col_adpe_comparison, col_pe_comparison = st.columns(3)
with col_ghg_comparison:
fig_gwp = px.pie(
values = [average_range_impacts(usage.gwp.value), average_range_impacts(embodied.gwp.value)],
names = ['usage', 'embodied'],
title = 'GHG emissions',
color_discrete_sequence=["#636EFA", "#00CC96"],
width = 100
)
fig_gwp.update_layout(showlegend=False, title_x=0.5)
st.plotly_chart(fig_gwp)
with col_adpe_comparison:
fig_adpe = px.pie(
values = [average_range_impacts(usage.adpe.value), average_range_impacts(embodied.adpe.value)],
names = ['usage', 'embodied'],
title = 'Abiotic depletion',
color_discrete_sequence=["#00CC96","#636EFA"],
width = 100)
fig_adpe.update_layout(
showlegend=True,
legend=dict(yanchor="bottom", x = 0.35, y = -0.1),
title_x=0.5)
st.plotly_chart(fig_adpe)
with col_pe_comparison:
fig_pe = px.pie(
values = [average_range_impacts(usage.pe.value), average_range_impacts(embodied.pe.value)],
names = ['usage', 'embodied'],
title = 'Primary energy',
color_discrete_sequence=["#636EFA", "#00CC96"],
width = 100)
fig_pe.update_layout(showlegend=False, title_x=0.5)
st.plotly_chart(fig_pe)
with st.expander('🌍️ Location impact'):
st.markdown('<h4 align="center">How can location impact the footprint ?</h4>', unsafe_allow_html = True)
countries_to_compare = st.multiselect(
label = 'Countries to compare',
options = [x[0] for x in COUNTRY_CODES],
default = ["🇫🇷 France", "🇺🇸 United States", "🇨🇳 China"]
)
try:
df = dataframe_electricity_mix(countries_to_compare)
impact_type = st.selectbox(
label='Select an impact type to compare',
options=[x for x in df.columns if x!='country'],
index=1)
df.sort_values(by = impact_type, inplace = True)
fig_2 = px.bar(df, x = df.index, y = impact_type, text = impact_type, color = impact_type)
st.plotly_chart(fig_2)
except:
st.warning("Can't display chart with no values.")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116import streamlit as st
import ecologits
from src.utils import (
format_energy_eq_electric_vehicle,
format_energy_eq_electricity_consumption_ireland,
format_energy_eq_electricity_production,
format_energy_eq_physical_activity,
format_gwp_eq_airplane_paris_nyc,
format_gwp_eq_streaming,
PhysicalActivity,
EnergyProduction
)
############################################################################################################
def get_impacts(model, active_params, total_params, mix_ghg, mix_adpe, mix_pe):
return 1
############################################################################################################
def display_impacts(impacts):
st.divider()
col_energy, col_ghg, col_adpe, col_pe, col_water = st.columns(5)
with col_energy:
st.markdown('<h4 align="center">⚡️ Energy</h4>', unsafe_allow_html = True)
st.latex(f'\Large {impacts.energy.magnitude:.3g} \ \large {impacts.energy.units}')
st.markdown(f'<p align="center"><i>Evaluates the electricity consumption<i></p>', unsafe_allow_html = True)
with col_ghg:
st.markdown('<h4 align="center">🌍️ GHG Emissions</h4>', unsafe_allow_html = True)
st.latex(f'\Large {impacts.gwp.magnitude:.3g} \ \large {impacts.gwp.units}')
st.markdown(f'<p align="center"><i>Evaluates the effect on global warming<i></p>', unsafe_allow_html = True)
with col_adpe:
st.markdown('<h4 align="center">🪨 Abiotic Resources</h4>', unsafe_allow_html = True)
st.latex(f'\Large {impacts.adpe.magnitude:.3g} \ \large {impacts.adpe.units}')
st.markdown(f'<p align="center"><i>Evaluates the use of metals and minerals<i></p>', unsafe_allow_html = True)
with col_pe:
st.markdown('<h4 align="center">⛽️ Primary Energy</h4>', unsafe_allow_html = True)
st.latex(f'\Large {impacts.pe.magnitude:.3g} \ \large {impacts.pe.units}')
st.markdown(f'<p align="center"><i>Evaluates the use of energy resources<i></p>', unsafe_allow_html = True)
with col_water:
st.markdown('<h4 align="center">🚰 Water</h4>', unsafe_allow_html = True)
st.latex('\Large Upcoming...')
st.markdown(f'<p align="center"><i>Evaluates the use of water<i></p>', unsafe_allow_html = True)
############################################################################################################
def display_equivalent(impacts):
st.divider()
ev_eq = format_energy_eq_electric_vehicle(impacts.energy)
streaming_eq = format_gwp_eq_streaming(impacts.gwp)
col1, col2, col3 = st.columns(3)
with col1:
physical_activity, distance = format_energy_eq_physical_activity(impacts.energy)
if physical_activity == PhysicalActivity.WALKING:
physical_activity = "🚶 " + physical_activity.capitalize()
if physical_activity == PhysicalActivity.RUNNING:
physical_activity = "🏃 " + physical_activity.capitalize()
st.markdown(f'<h4 align="center">{physical_activity}</h4>', unsafe_allow_html = True)
st.latex(f'\Large {distance.magnitude:.3g} \ \large {distance.units}')
st.markdown(f'<p align="center"><i>Based on energy consumption<i></p>', unsafe_allow_html = True)
with col2:
ev_eq = format_energy_eq_electric_vehicle(impacts.energy)
st.markdown(f'<h4 align="center">🔋 Electric Vehicle</h4>', unsafe_allow_html = True)
st.latex(f'\Large {ev_eq.magnitude:.3g} \ \large {ev_eq.units}')
st.markdown(f'<p align="center"><i>Based on energy consumption<i></p>', unsafe_allow_html = True)
with col3:
streaming_eq = format_gwp_eq_streaming(impacts.gwp)
st.markdown(f'<h4 align="center">⏯️ Streaming</h4>', unsafe_allow_html = True)
st.latex(f'\Large {streaming_eq.magnitude:.3g} \ \large {streaming_eq.units}')
st.markdown(f'<p align="center"><i>Based on GHG emissions<i></p>', unsafe_allow_html = True)
st.divider()
st.markdown('<h3 align="center">What if 1% of the planet does this request everyday for 1 year ?</h3>', unsafe_allow_html = True)
st.markdown('<p align="center">If this use case is largely deployed around the world, the equivalent impacts would be the impacts of this request x 1% of 8 billion people x 365 days in a year.</p>', unsafe_allow_html = True)
col4, col5, col6 = st.columns(3)
with col4:
electricity_production, count = format_energy_eq_electricity_production(impacts.energy)
if electricity_production == EnergyProduction.NUCLEAR:
emoji = "☢️"
name = "Nuclear power plants"
if electricity_production == EnergyProduction.WIND:
emoji = "💨️ "
name = "Wind turbines"
st.markdown(f'<h4 align="center">{emoji} {count.magnitude:.0f} {name} (yearly)</h4>', unsafe_allow_html = True)
st.markdown(f'<p align="center"><i>Based on energy consumption<i></p>', unsafe_allow_html = True)
with col5:
ireland_count = format_energy_eq_electricity_consumption_ireland(impacts.energy)
st.markdown(f'<h4 align="center">🇮🇪 {ireland_count.magnitude:.3f} x Ireland <span style="font-size: 12px">(yearly ⚡️ cons.)</span></h2></h4>', unsafe_allow_html = True)
st.markdown(f'<p align="center"><i>Based on energy consumption<i></p>', unsafe_allow_html = True)
with col6:
paris_nyc_airplane = format_gwp_eq_airplane_paris_nyc(impacts.gwp)
st.markdown(f'<h4 align="center">✈️ {round(paris_nyc_airplane.magnitude):,} Paris ↔ NYC</h4>', unsafe_allow_html = True)
st.markdown(f'<p align="center"><i>Based on GHG emissions<i></p>', unsafe_allow_html = True)12345678910111213141516171819202122232425262728293031323334353637383940414243444546import requests
import json
import pandas as pd
from src.constants import MODEL_REPOSITORY_URL
def clean_models_data(df):
dict_providers = {
'google': 'Google',
'mistralai': 'MistralAI',
'meta-llama': 'Meta',
'openai': 'OpenAI',
'anthropic': 'Anthropic',
'cohere': 'Cohere',
'microsoft': 'Microsoft',
'mistral-community': 'Mistral Community',
'databricks': 'Databricks'
}
df.drop('type', axis=1, inplace=True)
df.loc[df['name'].str.contains('/'), 'name_clean'] = df.loc[df['name'].str.contains('/'), 'name'].str.split('/').str[1]
df['name_clean'] = df['name_clean'].fillna(df['name'])
df['name_clean'] = df['name_clean'].replace({'-': ' '}, regex = True)
df.loc[df['provider'] == 'huggingface_hub', 'provider_clean'] = df.loc[df['provider'] == 'huggingface_hub', 'name'].str.split('/').str[0]
df['provider_clean'] = df['provider_clean'].fillna(df['provider'])
df['provider_clean'] = df['provider_clean'].replace(dict_providers, regex = True)
df['architecture_type'] = df['architecture'].apply(lambda x: x['type'])
df['architecture_parameters'] = df['architecture'].apply(lambda x: x['parameters'])
df['warnings'] = df['warnings'].apply(lambda x: ', '.join(x) if x else None).fillna('none')
df['warning_arch'] = df['warnings'].apply(lambda x: 'model-arch-not-released' in x)
df['warning_multi_modal'] = df['warnings'].apply(lambda x: 'model-arch-multimodal' in x)
return df[['provider', 'provider_clean', 'name', 'name_clean', 'architecture_type', 'architecture_parameters', 'warning_arch', 'warning_multi_modal']]
def load_models():
resp = requests.get(MODEL_REPOSITORY_URL)
data = json.loads(resp.text)
df = pd.DataFrame(data['models'])
return clean_models_data(df)
5 unmodified lines678910111 unmodified line131415161718192021222324252627282930313233343536374 unmodified lines4243444546475 unmodified linesfrom src.utils import format_impactsfrom src.content import WARNING_CLOSED_SOURCE, WARNING_MULTI_MODAL, WARNING_BOTHfrom src.models import load_models, clean_models_datafrom src.constants import PROMPTS1 unmodified linewith st.container(border=True):df = load_models()col1, col2, col3 = st.columns(3)with col1:provider = st.selectbox(label = 'Provider',options = [x for x in df['provider_clean'].unique()],index = 9)provider_raw = df[df['provider_clean'] == provider]['provider'].values[0]with col2:model = st.selectbox('Model', [x for x in df['name_clean'].unique() if x in df[df['provider_clean'] == provider]['name_clean'].unique()])model_raw = df[(df['provider_clean'] == provider) & (df['name_clean'] == model)]['name'].values[0]with col3:output_tokens = st.selectbox('Example prompt', [x[0] for x in PROMPTS])# WARNING DISPLAYdf_filtered = df[(df['provider_clean'] == provider) & (df['name_clean'] == model)]if df_filtered['warning_arch'].values[0] and not df_filtered['warning_multi_modal'].values[0]:4 unmodified linesst.warning(WARNING_BOTH)try:impacts = llm_impacts(provider=provider_raw,model_name=model_raw,5 unmodified lines67891011121 unmodified line1415161718192021222324252627282930313233343536373839404142434 unmodified lines484950515253545 unmodified linesfrom src.utils import format_impactsfrom src.content import WARNING_CLOSED_SOURCE, WARNING_MULTI_MODAL, WARNING_BOTHfrom src.models import load_models, clean_models_datafrom src.constants import MAIN_MODELSfrom src.constants import PROMPTS1 unmodified linewith st.container(border=True):df = load_models(filter_main=True)col1, col2, col3 = st.columns(3)with col1:provider = st.selectbox(label = 'Provider',options = [x for x in df['provider_clean'].unique()],index = 7)with col2:model = st.selectbox(label = 'Model',options = [x for x in df['name_clean'].unique() if x in df[df['provider_clean'] == provider]['name_clean'].unique()])with col3:output_tokens = st.selectbox('Example prompt', [x[0] for x in PROMPTS])# WARNING DISPLAYprovider_raw = df[(df['provider_clean'] == provider) & (df['name_clean'] == model)]['provider'].values[0]model_raw = df[(df['provider_clean'] == provider) & (df['name_clean'] == model)]['name'].values[0]df_filtered = df[(df['provider_clean'] == provider) & (df['name_clean'] == model)]if df_filtered['warning_arch'].values[0] and not df_filtered['warning_multi_modal'].values[0]:4 unmodified linesst.warning(WARNING_BOTH)try:impacts = llm_impacts(provider=provider_raw,model_name=model_raw,
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106PROVIDERS = [("OpenAI", "openai"),("Anthropic", "anthropic"),("Cohere", "cohere"),("Meta", "huggingface_hub/meta"),("Mistral AI", "mistralai"),]OPENAI_MODELS = [("GPT-4o", "gpt-4o"),("GPT-4-Turbo", "gpt-4-turbo"),("GPT-4", "gpt-4"),("GPT-3.5-Turbo", "gpt-3.5-turbo"),]ANTHROPIC_MODELS = [("Claude 3 Opus", "claude-3-opus-20240229"),("Claude 3 Sonnet", "claude-3-sonnet-20240229"),("Claude 3 Haiku", "claude-3-haiku-20240307"),("Claude 2.1", "claude-2.1"),("Claude 2.0", "claude-2.0"),("Claude Instant 1.2", "claude-instant-1.2"),]COHERE_MODELS = [("Command Light", "command-light"),("Command", "command"),("Command R", "command-r"),("Command R+", "command-r-plus"),]META_MODELS = [("Llama 3 8B", "meta-llama/Meta-Llama-3-8B"),("Llama 3 70B", "meta-llama/Meta-Llama-3-70B"),("Llama 2 7B", "meta-llama/Llama-2-7b-hf"),("Llama 2 13B", "meta-llama/Llama-2-13b-hf"),("Llama 2 70B", "meta-llama/Llama-2-70b-hf"),]MISTRALAI_MODELS = [("Mistral 7B", "open-mistral-7b"),("Mixtral 8x7B", "open-mixtral-8x7b"),("Mixtral 8x22B", "open-mixtral-8x22b"),("Tiny", "mistral-tiny-2312"),("Small", "mistral-small-2402"),("Medium", "mistral-medium-2312"),("Large", "mistral-large-2402"),]PROMPTS = [("Write a Tweet", 50),("Write an email", 170),("Write an article summary", 250),("Small conversation with a chatbot", 400),("Write a report of 5 pages", 5000),("Write the code for this app", 10000)]PROMPTS = [(s + f" ({v} output tokens)", v) for (s, v) in PROMPTS]CLOSED_SOURCE_MODELS = {"openai/gpt-4o","openai/gpt-4-turbo","openai/gpt-4","openai/gpt-3.5-turbo","anthropic/claude-3-opus-20240229","anthropic/claude-3-sonnet-20240229","anthropic/claude-3-haiku-20240307","anthropic/claude-2.1","anthropic/claude-2.0","anthropic/claude-instant-1.2","mistralai/mistral-tiny-2312","mistralai/mistral-small-2402","mistralai/mistral-medium-2312","mistralai/mistral-large-2402",}MODELS = [("OpenAI / GPT-4o", "openai/gpt-4o"),("OpenAI / GPT-4-Turbo", "openai/gpt-4-turbo"),("OpenAI / GPT-4", "openai/gpt-4"),("OpenAI / GPT-3.5-Turbo", "openai/gpt-3.5-turbo"),("Anthropic / Claude 3 Opus", "anthropic/claude-3-opus-20240229"),("Anthropic / Claude 3 Sonnet", "anthropic/claude-3-sonnet-20240229"),("Anthropic / Claude 3 Haiku", "anthropic/claude-3-haiku-20240307"),("Anthropic / Claude 2.1", "anthropic/claude-2.1"),("Anthropic / Claude 2.0", "anthropic/claude-2.0"),("Anthropic / Claude Instant 1.2", "anthropic/claude-instant-1.2"),("Mistral AI / Mistral 7B", "mistralai/open-mistral-7b"),("Mistral AI / Mixtral 8x7B", "mistralai/open-mixtral-8x7b"),("Mistral AI / Mixtral 8x22B", "mistralai/open-mixtral-8x22b"),("Mistral AI / Tiny", "mistralai/mistral-tiny-2312"),("Mistral AI / Small", "mistralai/mistral-small-2402"),("Mistral AI / Medium", "mistralai/mistral-medium-2312"),("Mistral AI / Large", "mistralai/mistral-large-2402"),("Meta / Llama 3 8B", "huggingface_hub/meta-llama/Meta-Llama-3-8B"),("Meta / Llama 3 70B", "huggingface_hub/meta-llama/Meta-Llama-3-70B"),("Meta / Llama 2 7B", "huggingface_hub/meta-llama/Llama-2-7b-hf"),("Meta / Llama 2 13B", "huggingface_hub/meta-llama/Llama-2-13b-hf"),("Meta / Llama 2 70B", "huggingface_hub/meta-llama/Llama-2-70b-hf"),("Cohere / Command Light", "cohere/command-light"),("Cohere / Command", "cohere/command"),("Cohere / Command R", "cohere/command-r"),("Cohere / Command R+", "cohere/command-r-plus"),]MODEL_REPOSITORY_URL = "https://raw.githubusercontent.com/genai-impact/ecologits/refs/heads/main/ecologits/data/models.json"123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102PROMPTS = [("Write a Tweet", 50),("Write an email", 170),("Write an article summary", 250),("Small conversation with a chatbot", 400),("Write a report of 5 pages", 5000),("Write the code for this app", 15000)]PROMPTS = [(s + f" ({v} output tokens)", v) for (s, v) in PROMPTS]MODEL_REPOSITORY_URL = "https://raw.githubusercontent.com/genai-impact/ecologits/refs/heads/main/ecologits/data/models.json"main_models_openai = ['chatgpt-4o-latest','gpt-3.5-turbo','gpt-4','gpt-4-turbo','gpt-4o','gpt-4o-mini','o1','o1-mini']main_models_meta = ['meta-llama/Meta-Llama-3.1-8B','meta-llama/Meta-Llama-3.1-70B','meta-llama/Meta-Llama-3.1-405B','meta-llama/Meta-Llama-3-8B','meta-llama/Meta-Llama-3-70B','meta-llama/Meta-Llama-3-70B','meta-llama/Llama-2-7b','meta-llama/Llama-2-13b','meta-llama/Llama-2-70b','meta-llama/CodeLlama-7b-hf','meta-llama/CodeLlama-13b-hf','meta-llama/CodeLlama-34b-hf','meta-llama/CodeLlama-70b-hf']main_models_msft = ['microsoft/phi-1','microsoft/phi-1_5','microsoft/Phi-3-mini-128k-instruct','microsoft/Phi-3-small-128k-instruct','microsoft/Phi-3-medium-128k-instruct',]main_models_anthropic = ['claude-2.0','claude-2.1','claude-3-5-haiku-latest','claude-3-5-sonnet-latest','claude-3-7-sonnet-latest','claude-3-haiku-20240307','claude-3-opus-latest','claude-3-sonnet-20240229']main_models_cohere = ['c4ai-aya-expanse-8b','c4ai-aya-expanse-32b','command','command-light','command-r','command-r-plus',]main_models_google = ['google/gemma-2-2b','google/gemma-2-9b','google/gemma-2-27b','google/codegemma-2b','google/codegemma-7b','gemini-1.0-pro','gemini-1.5-pro','gemini-1.5-flash','gemini-2.0-flash']main_models_databricks = ['databricks/dolly-v1-6b','databricks/dolly-v2-12b','databricks/dolly-v2-7b','databricks/dolly-v2-3b','databricks/dbrx-base']main_models_mistral = ['mistralai/Mistral-7B-v0.3','mistralai/Mixtral-8x7B-v0.1','mistralai/Mixtral-8x22B-v0.1','mistralai/Codestral-22B-v0.1','mistralai/Mathstral-7B-v0.1','ministral-3b-latest','ministral-8b-latest','mistral-tiny','mistral-small','mistral-medium','mistral-large-latest']MAIN_MODELS = main_models_meta + main_models_openai + main_models_anthropic + main_models_cohere + main_models_msft + main_models_mistral + main_models_databricks + main_models_google
1234567896 unmodified lines1617181920212223242526275 unmodified lines3334353637383940414243444546import requestsimport jsonimport pandas as pdfrom src.constants import MODEL_REPOSITORY_URLdef clean_models_data(df):dict_providers = {'google': 'Google',6 unmodified lines'mistral-community': 'Mistral Community','databricks': 'Databricks'}df.drop('type', axis=1, inplace=True)df.loc[df['name'].str.contains('/'), 'name_clean'] = df.loc[df['name'].str.contains('/'), 'name'].str.split('/').str[1]df['name_clean'] = df['name_clean'].fillna(df['name'])df['name_clean'] = df['name_clean'].replace({'-': ' '}, regex = True)df.loc[df['provider'] == 'huggingface_hub', 'provider_clean'] = df.loc[df['provider'] == 'huggingface_hub', 'name'].str.split('/').str[0]df['provider_clean'] = df['provider_clean'].fillna(df['provider'])5 unmodified linesdf['warnings'] = df['warnings'].apply(lambda x: ', '.join(x) if x else None).fillna('none')df['warning_arch'] = df['warnings'].apply(lambda x: 'model-arch-not-released' in x)df['warning_multi_modal'] = df['warnings'].apply(lambda x: 'model-arch-multimodal' in x)return df[['provider', 'provider_clean', 'name', 'name_clean', 'architecture_type', 'architecture_parameters', 'warning_arch', 'warning_multi_modal']]def load_models():resp = requests.get(MODEL_REPOSITORY_URL)data = json.loads(resp.text)df = pd.DataFrame(data['models'])return clean_models_data(df)123456789106 unmodified lines17181920212223242526272829305 unmodified lines363738394041424344454647484950515253import requestsimport jsonimport pandas as pdfrom src.constants import MODEL_REPOSITORY_URL, MAIN_MODELSimport streamlit as stdef clean_models_data(df, with_filter = True):dict_providers = {'google': 'Google',6 unmodified lines'mistral-community': 'Mistral Community','databricks': 'Databricks'}models_to_keep = MAIN_MODELSdf.drop('type', axis=1, inplace=True)df.loc[df['name'].str.contains('/'), 'name_clean'] = df.loc[df['name'].str.contains('/'), 'name'].str.split('/').str[1]df['name_clean'] = df['name_clean'].fillna(df['name'])df['name_clean'] = df['name_clean'].replace({'-': ' ', 'latest': ''}, regex = True)df.loc[df['provider'] == 'huggingface_hub', 'provider_clean'] = df.loc[df['provider'] == 'huggingface_hub', 'name'].str.split('/').str[0]df['provider_clean'] = df['provider_clean'].fillna(df['provider'])5 unmodified linesdf['warnings'] = df['warnings'].apply(lambda x: ', '.join(x) if x else None).fillna('none')df['warning_arch'] = df['warnings'].apply(lambda x: 'model-arch-not-released' in x)df['warning_multi_modal'] = df['warnings'].apply(lambda x: 'model-arch-multimodal' in x)if with_filter == True:df = df[df['name'].isin(models_to_keep)]return df[['provider', 'provider_clean', 'name', 'name_clean', 'architecture_type', 'architecture_parameters', 'warning_arch', 'warning_multi_modal']]@st.cache_datadef load_models(filter_main = True):resp = requests.get(MODEL_REPOSITORY_URL)data = json.loads(resp.text)df = pd.DataFrame(data['models'])return clean_models_data(df, filter_main)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262from dataclasses import dataclass
from enum import Enum
import pandas as pd
from ecologits.model_repository import models
from ecologits.impacts.modeling import Impacts, Energy, GWP, ADPe, PE
#from ecologits.tracers.utils import llm_impacts
from pint import UnitRegistry, Quantity
#####################################################################################
### UNITS DEFINITION
#####################################################################################
u = UnitRegistry()
u.define('Wh = watt_hour')
u.define('kWh = kilowatt_hour')
u.define('MWh = megawatt_hour')
u.define('GWh = gigawatt_hour')
u.define('TWh = terawatt_hour')
u.define('gCO2eq = gram')
u.define('kgCO2eq = kilogram')
u.define('tCO2eq = metricton')
u.define('kgSbeq = kilogram')
u.define('kJ = kilojoule')
u.define('MJ = megajoule')
u.define('m = meter')
u.define('km = kilometer')
u.define('s = second')
u.define('min = minute')
u.define('h = hour')
q = u.Quantity
@dataclass
class QImpacts:
energy: Quantity
gwp: Quantity
adpe: Quantity
pe: Quantity
class PhysicalActivity(str, Enum):
RUNNING = "running"
WALKING = "walking"
class EnergyProduction(str, Enum):
NUCLEAR = "nuclear"
WIND = "wind"
COUNTRIES = [
("cook_islands", 38.81, 9_556),
("tonga", 51.15, 104_490),
("comoros", 100, 821_632),
("samoa", 100, 821_632),
]
#####################################################################################
### EQUIVALENT RAW DATA
#####################################################################################
# From https://www.runningtools.com/energyusage.htm
RUNNING_ENERGY_EQ = q("294 kJ / km") # running 1 km at 10 km/h with a weight of 70 kg
WALKING_ENERGY_EQ = q("196 kJ / km") # walking 1 km at 3 km/h with a weight of 70 kg
# From https://selectra.info/energie/actualites/insolite/consommation-vehicules-electriques-france-2040
# and https://www.tesla.com/fr_fr/support/power-consumption
EV_ENERGY_EQ = q("0.17 kWh / km")
# From https://impactco2.fr/outils/comparateur?value=1&comparisons=streamingvideo
STREAMING_GWP_EQ = q("15.6 h / kgCO2eq")
# From https://ourworldindata.org/population-growth
ONE_PERCENT_WORLD_POPULATION = 80_000_000
DAYS_IN_YEAR = 365
# For a 900 MW nuclear plant -> 500 000 MWh / month
# From https://www.edf.fr/groupe-edf/espaces-dedies/jeunes-enseignants/pour-les-jeunes/lenergie-de-a-a-z/produire-de-lelectricite/le-nucleaire-en-chiffres
YEARLY_NUCLEAR_ENERGY_EQ = q("6 TWh")
# For a 2MW wind turbine
# https://www.ecologie.gouv.fr/eolien-terrestre
YEARLY_WIND_ENERGY_EQ = q("4.2 GWh")
# Ireland yearly electricity consumption
# From https://en.wikipedia.org/wiki/List_of_countries_by_electricity_consumption
YEARLY_IRELAND_ELECTRICITY_CONSUMPTION = q("33 TWh")
IRELAND_POPULATION_MILLION = 5
# From https://impactco2.fr/outils/comparateur?value=1&comparisons=&equivalent=avion-pny
AIRPLANE_PARIS_NYC_GWP_EQ = q("177000 kgCO2eq")
def filter_models(provider, list_models):
model = 1
return model
#####################################################################################
### IMPACTS FORMATING
#####################################################################################
def format_energy(energy: Energy) -> Quantity:
val = q(energy.value, energy.unit)
if val < q("1 kWh"):
val = val.to("Wh")
return val
def format_gwp(gwp: GWP) -> Quantity:
val = q(gwp.value, gwp.unit)
if val < q("1 kgCO2eq"):
val = val.to("gCO2eq")
return val
def format_adpe(adpe: ADPe) -> Quantity:
return q(adpe.value, adpe.unit)
def format_pe(pe: PE) -> Quantity:
val = q(pe.value, pe.unit)
if val < q("1 MJ"):
val = val.to("kJ")
return val
def format_impacts(impacts: Impacts) -> QImpacts:
try:
impacts.energy.value = (impacts.energy.value.max + impacts.energy.value.min)/2
impacts.gwp.value = (impacts.gwp.value.max + impacts.gwp.value.min)/2
impacts.adpe.value = (impacts.adpe.value.max + impacts.adpe.value.min)/2
impacts.pe.value = (impacts.pe.value.max + impacts.pe.value.min)/2
return QImpacts(
energy=format_energy(impacts.energy),
gwp=format_gwp(impacts.gwp),
adpe=format_adpe(impacts.adpe),
pe=format_pe(impacts.pe)
), impacts.usage, impacts.embodied
except: #when no range
return QImpacts(
energy=format_energy(impacts.energy),
gwp=format_gwp(impacts.gwp),
adpe=format_adpe(impacts.adpe),
pe=format_pe(impacts.pe)
), impacts.usage, impacts.embodied
def split_impacts_u_e(impacts: Impacts) -> QImpacts:
return impacts.usage, impacts.embodied
def average_range_impacts(RangeValue):
return (RangeValue.max + RangeValue.min)/2
def format_impacts_expert(impacts: Impacts, display_range: bool) -> QImpacts:
if display_range:
return QImpacts(
energy=format_energy(impacts.energy),
gwp=format_gwp(impacts.gwp),
adpe=format_adpe(impacts.adpe),
pe=format_pe(impacts.pe)
), impacts.usage, impacts.embodied
else:
energy = {"value":(impacts.energy.value.max + impacts.energy.value.min)/2, "unit":impacts.energy.unit}
gwp = (impacts.gwp.value.max + impacts.gwp.value.min)/2
adpe = (impacts.adpe.value.max + impacts.adpe.value.min)/2
pe = (impacts.pe.value.max + impacts.pe.value.min)/2
return QImpacts(
energy=format_energy(energy),
gwp=format_gwp(gwp),
adpe=format_adpe(adpe),
pe=format_pe(pe)
), impacts.usage, impacts.embodied
#####################################################################################
### EQUIVALENT FORMATING
#####################################################################################
def format_energy_eq_physical_activity(energy: Quantity) -> tuple[PhysicalActivity, Quantity]:
energy = energy.to("kJ")
running_eq = energy / RUNNING_ENERGY_EQ
if running_eq > q("1 km"):
return PhysicalActivity.RUNNING, running_eq
walking_eq = energy / WALKING_ENERGY_EQ
if walking_eq < q("1 km"):
walking_eq = walking_eq.to("meter")
return PhysicalActivity.WALKING, walking_eq
def format_energy_eq_electric_vehicle(energy: Quantity) -> Quantity:
energy = energy.to("kWh")
ev_eq = energy / EV_ENERGY_EQ
if ev_eq < q("1 km"):
ev_eq = ev_eq.to("meter")
return ev_eq
def format_gwp_eq_streaming(gwp: Quantity) -> Quantity:
gwp = gwp.to("kgCO2eq")
streaming_eq = gwp * STREAMING_GWP_EQ
if streaming_eq < q("1 h"):
streaming_eq = streaming_eq.to("min")
if streaming_eq < q("1 min"):
streaming_eq = streaming_eq.to("s")
return streaming_eq
def format_energy_eq_electricity_production(energy: Quantity) -> tuple[EnergyProduction, Quantity]:
electricity_eq = energy * ONE_PERCENT_WORLD_POPULATION * DAYS_IN_YEAR
electricity_eq = electricity_eq.to("TWh")
if electricity_eq > YEARLY_NUCLEAR_ENERGY_EQ:
return EnergyProduction.NUCLEAR, electricity_eq / YEARLY_NUCLEAR_ENERGY_EQ
electricity_eq = electricity_eq.to("GWh")
return EnergyProduction.WIND, electricity_eq / YEARLY_WIND_ENERGY_EQ
def format_energy_eq_electricity_consumption_ireland(energy: Quantity) -> Quantity:
electricity_eq = energy * ONE_PERCENT_WORLD_POPULATION * DAYS_IN_YEAR
electricity_eq = electricity_eq.to("TWh")
return electricity_eq / YEARLY_IRELAND_ELECTRICITY_CONSUMPTION
def format_gwp_eq_airplane_paris_nyc(gwp: Quantity) -> Quantity:
gwp_eq = gwp * ONE_PERCENT_WORLD_POPULATION * DAYS_IN_YEAR
gwp_eq = gwp_eq.to("kgCO2eq")
return gwp_eq / AIRPLANE_PARIS_NYC_GWP_EQ
#####################################################################################
### MODELS PARAMETERS
#####################################################################################
def model_active_params_fn(provider_name: str, model_name: str, n_param: float):
if model_name == 'CUSTOM':
return n_param
else:
model = models.find_model(provider=provider_name, model_name=model_name)
if model.architecture == 'moe':
try:
return model.architecture.parameters.active.max
except:
try:
return model.architecture.parameters.active
except:
return model.architecture.parameters
elif model.architecture == 'dense':
try: #dense with range
return model.architecture.parameters.max
except: #dense without range
return model.architecture.parameters
def model_total_params_fn(provider_name: str, model_name: str, n_param: float):
if model_name == 'CUSTOM':
return n_param
provider, model_name = model_name.split('/', 1)
model = models.find_model(provider=provider, model_name=model_name)
try: #moe
return model.architecture.parameters.total.max
except:
try: #dense with range
return model.architecture.parameters.max
except: #dense without range
try:
return model.architecture.parameters.total
except:
return model.architecture.parameters12345version = 1revision = 1requires-python = ">=3.12"[[package]]1234version = 1requires-python = ">=3.12"[[package]]
__init__.pyDeleted-8
calculator.pyDeleted-67
constants.pyDeleted-106
content.pyDeleted-232
electricity_mix.pyDeleted-175
expert.pyDeleted-156
impacts.pyDeleted-116
models.pyDeleted-46
src/__pycache__/__init__.cpython-312.pyc
src/__pycache__/calculator.cpython-312.pyc
src/__pycache__/constants.cpython-312.pyc
src/__pycache__/content.cpython-312.pyc
src/__pycache__/electricity_mix.cpython-312.pyc
src/__pycache__/expert.cpython-312.pyc
src/__pycache__/impacts.cpython-312.pyc
src/__pycache__/models.cpython-312.pyc
src/__pycache__/utils.cpython-312.pyc
src/calculator.py+15-8
src/constants.py+85-89
src/models.py+12-5
utils.pyDeleted-262
uv.lock-1