fix: correctly name arguments in token adjustement in expert mode
Browse files- src/expert.py +186 -182
src/expert.py
CHANGED
@@ -1,183 +1,187 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from ecologits.impacts.llm import compute_llm_impacts
|
3 |
-
|
4 |
-
from src.utils import format_impacts, average_range_impacts
|
5 |
-
from src.impacts import display_impacts
|
6 |
-
from src.electricity_mix import COUNTRY_CODES, find_electricity_mix, dataframe_electricity_mix
|
7 |
-
from src.models import load_models
|
8 |
-
from src.constants import PROMPTS
|
9 |
-
|
10 |
-
import plotly.express as px
|
11 |
-
|
12 |
-
def reset_model():
|
13 |
-
model = 'CUSTOM'
|
14 |
-
|
15 |
-
def expert_mode():
|
16 |
-
|
17 |
-
st.markdown("### 🤓 Expert mode")
|
18 |
-
|
19 |
-
with st.container(border = True):
|
20 |
-
|
21 |
-
########## Model info ##########
|
22 |
-
|
23 |
-
col1, col2, col3 = st.columns(3)
|
24 |
-
|
25 |
-
df = load_models(filter_main=True)
|
26 |
-
|
27 |
-
with col1:
|
28 |
-
provider_exp = st.selectbox(
|
29 |
-
label = 'Provider',
|
30 |
-
options = [x for x in df['provider_clean'].unique()],
|
31 |
-
index = 7,
|
32 |
-
key = 1
|
33 |
-
)
|
34 |
-
|
35 |
-
with col2:
|
36 |
-
model_exp = st.selectbox(
|
37 |
-
label = 'Model',
|
38 |
-
options = [x for x in df['name_clean'].unique() if x in df[df['provider_clean'] == provider_exp]['name_clean'].unique()],
|
39 |
-
key = 2
|
40 |
-
)
|
41 |
-
|
42 |
-
with col3:
|
43 |
-
output_tokens_exp = st.selectbox(
|
44 |
-
label = 'Example prompt',
|
45 |
-
options = [x[0] for x in PROMPTS],
|
46 |
-
key = 3
|
47 |
-
)
|
48 |
-
|
49 |
-
df_filtered = df[(df['provider_clean'] == provider_exp) & (df['name_clean'] == model_exp)]
|
50 |
-
|
51 |
-
try:
|
52 |
-
total_params = int(df_filtered['total_parameters'].iloc[0])
|
53 |
-
except:
|
54 |
-
total_params = int((df_filtered['total_parameters'].values[0]['min'] + df_filtered['total_parameters'].values[0]['max'])/2)
|
55 |
-
|
56 |
-
try:
|
57 |
-
active_params = int(df_filtered['active_parameters'].iloc[0])
|
58 |
-
except:
|
59 |
-
active_params = int((df_filtered['active_parameters'].values[0]['min'] + df_filtered['active_parameters'].values[0]['max'])/2)
|
60 |
-
|
61 |
-
########## Model parameters ##########
|
62 |
-
|
63 |
-
col11, col22, col33 = st.columns(3)
|
64 |
-
|
65 |
-
with col11:
|
66 |
-
active_params = st.number_input('Active parameters (B)', 0, None, active_params)
|
67 |
-
|
68 |
-
with col22:
|
69 |
-
total_params = st.number_input('Total parameters (B)', 0, None, total_params)
|
70 |
-
|
71 |
-
with col33:
|
72 |
-
output_tokens = st.number_input(
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
|
|
|
|
|
|
|
|
183 |
st.warning("Can't display chart with no values.")
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from ecologits.impacts.llm import compute_llm_impacts
|
3 |
+
|
4 |
+
from src.utils import format_impacts, average_range_impacts
|
5 |
+
from src.impacts import display_impacts
|
6 |
+
from src.electricity_mix import COUNTRY_CODES, find_electricity_mix, dataframe_electricity_mix
|
7 |
+
from src.models import load_models
|
8 |
+
from src.constants import PROMPTS
|
9 |
+
|
10 |
+
import plotly.express as px
|
11 |
+
|
12 |
+
def reset_model():
|
13 |
+
model = 'CUSTOM'
|
14 |
+
|
15 |
+
def expert_mode():
|
16 |
+
|
17 |
+
st.markdown("### 🤓 Expert mode")
|
18 |
+
|
19 |
+
with st.container(border = True):
|
20 |
+
|
21 |
+
########## Model info ##########
|
22 |
+
|
23 |
+
col1, col2, col3 = st.columns(3)
|
24 |
+
|
25 |
+
df = load_models(filter_main=True)
|
26 |
+
|
27 |
+
with col1:
|
28 |
+
provider_exp = st.selectbox(
|
29 |
+
label = 'Provider',
|
30 |
+
options = [x for x in df['provider_clean'].unique()],
|
31 |
+
index = 7,
|
32 |
+
key = 1
|
33 |
+
)
|
34 |
+
|
35 |
+
with col2:
|
36 |
+
model_exp = st.selectbox(
|
37 |
+
label = 'Model',
|
38 |
+
options = [x for x in df['name_clean'].unique() if x in df[df['provider_clean'] == provider_exp]['name_clean'].unique()],
|
39 |
+
key = 2
|
40 |
+
)
|
41 |
+
|
42 |
+
with col3:
|
43 |
+
output_tokens_exp = st.selectbox(
|
44 |
+
label = 'Example prompt',
|
45 |
+
options = [x[0] for x in PROMPTS],
|
46 |
+
key = 3
|
47 |
+
)
|
48 |
+
|
49 |
+
df_filtered = df[(df['provider_clean'] == provider_exp) & (df['name_clean'] == model_exp)]
|
50 |
+
|
51 |
+
try:
|
52 |
+
total_params = int(df_filtered['total_parameters'].iloc[0])
|
53 |
+
except:
|
54 |
+
total_params = int((df_filtered['total_parameters'].values[0]['min'] + df_filtered['total_parameters'].values[0]['max'])/2)
|
55 |
+
|
56 |
+
try:
|
57 |
+
active_params = int(df_filtered['active_parameters'].iloc[0])
|
58 |
+
except:
|
59 |
+
active_params = int((df_filtered['active_parameters'].values[0]['min'] + df_filtered['active_parameters'].values[0]['max'])/2)
|
60 |
+
|
61 |
+
########## Model parameters ##########
|
62 |
+
|
63 |
+
col11, col22, col33 = st.columns(3)
|
64 |
+
|
65 |
+
with col11:
|
66 |
+
active_params = st.number_input('Active parameters (B)', 0, None, active_params)
|
67 |
+
|
68 |
+
with col22:
|
69 |
+
total_params = st.number_input('Total parameters (B)', 0, None, total_params)
|
70 |
+
|
71 |
+
with col33:
|
72 |
+
output_tokens = st.number_input(
|
73 |
+
label = 'Output completion tokens',
|
74 |
+
min_value = 0,
|
75 |
+
value = [x[1] for x in PROMPTS if x[0] == output_tokens_exp][0]
|
76 |
+
)
|
77 |
+
|
78 |
+
########## Electricity mix ##########
|
79 |
+
|
80 |
+
location = st.selectbox('Location', [x[0] for x in COUNTRY_CODES])
|
81 |
+
|
82 |
+
col4, col5, col6 = st.columns(3)
|
83 |
+
|
84 |
+
with col4:
|
85 |
+
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")
|
86 |
+
#disp_ranges = st.toggle('Display impact ranges', False)
|
87 |
+
with col5:
|
88 |
+
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")
|
89 |
+
with col6:
|
90 |
+
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")
|
91 |
+
|
92 |
+
impacts = compute_llm_impacts(model_active_parameter_count=active_params,
|
93 |
+
model_total_parameter_count=total_params,
|
94 |
+
output_token_count=output_tokens,
|
95 |
+
request_latency=100000,
|
96 |
+
if_electricity_mix_gwp=mix_gwp,
|
97 |
+
if_electricity_mix_adpe=mix_adpe,
|
98 |
+
if_electricity_mix_pe=mix_pe
|
99 |
+
)
|
100 |
+
|
101 |
+
impacts, usage, embodied = format_impacts(impacts)
|
102 |
+
|
103 |
+
with st.container(border = True):
|
104 |
+
|
105 |
+
st.markdown('<h3 align="center">Environmental Impacts</h2>', unsafe_allow_html = True)
|
106 |
+
|
107 |
+
display_impacts(impacts)
|
108 |
+
|
109 |
+
with st.expander('⚖️ Usage vs Embodied'):
|
110 |
+
|
111 |
+
st.markdown('<h3 align="center">Embodied vs Usage comparison</h2>', unsafe_allow_html = True)
|
112 |
+
|
113 |
+
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.')
|
114 |
+
|
115 |
+
col_ghg_comparison, col_adpe_comparison, col_pe_comparison = st.columns(3)
|
116 |
+
|
117 |
+
with col_ghg_comparison:
|
118 |
+
|
119 |
+
fig_gwp = px.pie(
|
120 |
+
values = [average_range_impacts(usage.gwp.value), average_range_impacts(embodied.gwp.value)],
|
121 |
+
names = ['usage', 'embodied'],
|
122 |
+
title = 'GHG emissions',
|
123 |
+
color_discrete_sequence=["#00BF63", "#0B3B36"],
|
124 |
+
width = 100
|
125 |
+
)
|
126 |
+
fig_gwp.update_layout(showlegend=False, title_x=0.5)
|
127 |
+
|
128 |
+
st.plotly_chart(fig_gwp)
|
129 |
+
|
130 |
+
with col_adpe_comparison:
|
131 |
+
fig_adpe = px.pie(
|
132 |
+
values = [average_range_impacts(usage.adpe.value), average_range_impacts(embodied.adpe.value)],
|
133 |
+
names = ['usage', 'embodied'],
|
134 |
+
title = 'Abiotic depletion',
|
135 |
+
color_discrete_sequence=["#0B3B36","#00BF63"],
|
136 |
+
width = 100)
|
137 |
+
fig_adpe.update_layout(
|
138 |
+
showlegend=False,
|
139 |
+
title_x=0.5)
|
140 |
+
|
141 |
+
st.plotly_chart(fig_adpe)
|
142 |
+
|
143 |
+
with col_pe_comparison:
|
144 |
+
fig_pe = px.pie(
|
145 |
+
values = [average_range_impacts(usage.pe.value), average_range_impacts(embodied.pe.value)],
|
146 |
+
names = ['usage', 'embodied'],
|
147 |
+
title = 'Primary energy',
|
148 |
+
color_discrete_sequence=["#00BF63", "#0B3B36"],
|
149 |
+
width = 100)
|
150 |
+
fig_pe.update_layout(showlegend=False, title_x=0.5)
|
151 |
+
|
152 |
+
st.plotly_chart(fig_pe)
|
153 |
+
|
154 |
+
with st.expander('🌍️ Location impact'):
|
155 |
+
|
156 |
+
st.markdown('<h4 align="center">How can location impact the footprint ?</h4>', unsafe_allow_html = True)
|
157 |
+
|
158 |
+
countries_to_compare = st.multiselect(
|
159 |
+
label = 'Countries to compare',
|
160 |
+
options = [x[0] for x in COUNTRY_CODES],
|
161 |
+
default = ["🇫🇷 France", "🇺🇸 United States", "🇨🇳 China"]
|
162 |
+
)
|
163 |
+
|
164 |
+
try:
|
165 |
+
|
166 |
+
df_comp = dataframe_electricity_mix(countries_to_compare)
|
167 |
+
|
168 |
+
impact_type = st.selectbox(
|
169 |
+
label='Select an impact type to compare',
|
170 |
+
options=[x for x in df_comp.columns if x!='country'],
|
171 |
+
index=1)
|
172 |
+
|
173 |
+
df_comp.sort_values(by = impact_type, inplace = True)
|
174 |
+
|
175 |
+
fig_2 = px.bar(
|
176 |
+
df_comp,
|
177 |
+
x = df_comp.index,
|
178 |
+
y = impact_type,
|
179 |
+
text = impact_type,
|
180 |
+
color = impact_type
|
181 |
+
)
|
182 |
+
|
183 |
+
st.plotly_chart(fig_2)
|
184 |
+
|
185 |
+
except:
|
186 |
+
|
187 |
st.warning("Can't display chart with no values.")
|