Spaces:
Running
Running
| # Copyright 2021 The HuggingFace Team. All rights reserved. | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| import streamlit as st | |
| import pandas as pd | |
| import plotly.express as px | |
| import numpy as np | |
| import os | |
| # --- Utility Functions --- | |
| def load_carbon_data(path): | |
| """Load carbon data from pickle file, handle errors gracefully.""" | |
| if not os.path.exists(path): | |
| st.error(f"Data file not found: {path}") | |
| return None | |
| try: | |
| df = pd.read_pickle(path) | |
| return df | |
| except Exception as e: | |
| st.error(f"Failed to load data: {e}") | |
| return None | |
| # --- Load Data --- | |
| carbon_df = load_carbon_data('./data/carbon_df.pkl') | |
| if carbon_df is not None: | |
| # Drop rows with missing task | |
| carbon_df = carbon_df[carbon_df['task'].astype(str).str.strip() != ''] | |
| st.set_page_config( | |
| page_title="Comparing the Carbon Footprint of Transformers", | |
| page_icon="./hf-earth.png", | |
| layout="wide", | |
| ) | |
| st.title("Hugging Face Carbon Compare Tool") | |
| # --- Sidebar: Model Selection --- | |
| with st.sidebar.expander("Models", expanded=True): | |
| st.image('./hf-earth.png') | |
| models = [] | |
| model_full_names = [] | |
| for m in carbon_df['name'].items(): | |
| try: | |
| modelname = m[1].split('/')[1] | |
| except Exception: | |
| modelname = m[1] | |
| models.append(modelname) | |
| model_full_names.append(m[1]) | |
| model_name = st.selectbox( | |
| "Choose model to explore:", | |
| models, | |
| help="Select a model to compare its carbon footprint." | |
| ) | |
| # --- Model Comparison --- | |
| with st.expander("Model Comparison", expanded=False): | |
| st.markdown(f"### How does **{model_name}** compare to other models?") | |
| # Highlight the selected model in red, others in blue | |
| color_map = {m: ('red' if m == model_name else '#1f77b4') for m in models} | |
| sorted_df = carbon_df.copy() | |
| sorted_df['short_name'] = models | |
| sorted_df = sorted_df.sort_values(by=['carbon']) | |
| fig_model = px.bar( | |
| sorted_df, | |
| x='short_name', | |
| y='carbon', | |
| hover_name='name', | |
| color='short_name', | |
| color_discrete_map=color_map, | |
| labels={'short_name': 'Model', 'carbon': 'Carbon Footprint (kg CO₂e)'}, | |
| ) | |
| fig_model.update_layout(showlegend=False) | |
| st.plotly_chart(fig_model, use_container_width=True) | |
| # --- Task Comparison --- | |
| with st.expander("Task Comparison", expanded=False): | |
| fig = px.box( | |
| carbon_df, | |
| x='task', | |
| y='carbon', | |
| color='task', | |
| hover_name='name', | |
| labels={'task': 'Task', 'carbon': 'Carbon Footprint (kg CO₂e)'}, | |
| ) | |
| fig.update_layout(showlegend=False) | |
| st.plotly_chart(fig, use_container_width=True) | |
| else: | |
| st.stop() | |