bgamazay commited on
Commit
83432dd
·
verified ·
1 Parent(s): 446b9c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -86
app.py CHANGED
@@ -7,12 +7,12 @@ def main():
7
  # Sidebar logo and title
8
  with st.sidebar:
9
  col1, col2 = st.columns([1, 5]) # Shrink the logo column and expand the text column
10
-
11
  with col1:
12
  logo = Image.open("logo.png")
13
  resized_logo = logo.resize((40, 40)) # Resize the logo
14
  st.image(resized_logo)
15
-
16
  with col2:
17
  st.markdown(
18
  """
@@ -31,24 +31,13 @@ def main():
31
  unsafe_allow_html=True,
32
  )
33
 
34
- # Sidebar instructions and link
35
- st.sidebar.markdown(
36
- """
37
- <h1 style="text-align: center; font-size: 24px; font-weight: bold;">
38
- Generate a Label to Display your
39
- <a href="https://huggingface.co/spaces/AIEnergyScore/Leaderboard" target="_blank" style="text-decoration: none; color: inherit;">
40
- AI Energy Score
41
- </a>
42
- </h1>
43
- """,
44
- unsafe_allow_html=True,
45
- )
46
 
47
  st.sidebar.markdown("<hr style='border: 1px solid gray; margin: 15px 0;'>", unsafe_allow_html=True)
48
 
49
- st.sidebar.write("### Instructions:")
50
- st.sidebar.write("#### 1. Select task(s)")
51
-
52
  # Define the ordered list of tasks.
53
  task_order = [
54
  "Text Generation",
@@ -62,15 +51,11 @@ def main():
62
  "Question Answering",
63
  "Sentence Similarity"
64
  ]
65
-
66
- # Multi-select dropdown for tasks.
67
- selected_tasks = st.sidebar.multiselect("Select Task(s)", options=task_order, default=task_order)
68
- if not selected_tasks:
69
- st.sidebar.error("Please select at least one task.")
70
- st.stop()
71
-
72
- st.sidebar.write("#### 2. Select a model below")
73
-
74
  # Mapping from task to CSV file name.
75
  task_to_file = {
76
  "Text Generation": "text_gen_energyscore.csv",
@@ -84,57 +69,71 @@ def main():
84
  "Question Answering": "question_answering_energyscore.csv",
85
  "Sentence Similarity": "sentence_similarity_energyscore.csv"
86
  }
87
-
88
- dfs = []
89
- # Load and process each CSV corresponding to the selected tasks.
90
- for task in selected_tasks:
91
- file_name = task_to_file[task]
92
- try:
93
- df = pd.read_csv(file_name)
94
- except FileNotFoundError:
95
- st.sidebar.error(f"Could not find '{file_name}' for task {task}!")
96
- continue
97
- except Exception as e:
98
- st.sidebar.error(f"Error reading '{file_name}' for task {task}: {e}")
99
- continue
100
-
101
- # Split the "model" column into 'provider' (before the "/") and 'model' (after the "/")
102
- df[['provider', 'model']] = df['model'].str.split(pat='/', n=1, expand=True)
103
- # Round total_gpu_energy to 3 decimal places and assign to 'energy'
104
- df['energy'] = df['total_gpu_energy'].round(3)
105
- # Use the energy_score column as 'score' (fill missing values with 1 to avoid casting errors)
106
- df['score'] = df['energy_score'].fillna(1).astype(int)
107
- # Hardcode date and hardware
108
- df['date'] = "February 2025"
109
- df['hardware'] = "NVIDIA H100-80GB"
110
- # Set the task from the file name mapping
111
- df['task'] = task
112
-
113
- dfs.append(df)
114
-
115
- if not dfs:
116
- st.sidebar.error("No data available for the selected task(s).")
117
- return
118
-
119
- data_df = pd.concat(dfs, ignore_index=True)
120
-
121
- # Check required columns
122
- required_columns = ["model", "provider", "date", "task", "hardware", "energy", "score"]
123
- for col in required_columns:
124
- if col not in data_df.columns:
125
- st.sidebar.error(f"The CSV file must contain a column named '{col}'.")
126
- return
127
-
128
- model_options = data_df["model"].unique().tolist()
129
- selected_model = st.sidebar.selectbox(
130
- "Scored Models",
131
- model_options,
132
- help="Start typing to search for a model"
133
- )
134
-
135
- model_data = data_df[data_df["model"] == selected_model].iloc[0]
136
 
137
- # Select background by score
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  try:
139
  score = int(model_data["score"])
140
  background_path = f"{score}.png"
@@ -163,14 +162,14 @@ def main():
163
  mime="image/png"
164
  )
165
 
166
- st.sidebar.write("#### 3. Share your label! [Guidelines](https://huggingface.github.io/AIEnergyScore/#labelusage)")
167
  st.sidebar.markdown("<hr style='border: 1px solid gray; margin: 15px 0;'>", unsafe_allow_html=True)
168
  st.sidebar.write("### Key Links")
169
  st.sidebar.write("- [Leaderboard](https://huggingface.co/spaces/AIEnergyScore/Leaderboard)")
170
- st.sidebar.write("- [Submission Portal](https://huggingface.co/spaces/AIEnergyScore/submission_portal)")
171
  st.sidebar.write("- [FAQ](https://huggingface.github.io/AIEnergyScore/#faq)")
172
  st.sidebar.write("- [Documentation](https://huggingface.github.io/AIEnergyScore/#documentation)")
173
-
174
 
175
  def create_label_single_pass(background_image, model_data, final_size=(520, 728)):
176
  """
@@ -189,16 +188,15 @@ def create_label_single_pass(background_image, model_data, final_size=(520, 728)
189
  st.error(f"Font loading failed: {e}")
190
  return bg_resized
191
 
192
- # 3. Place your text.
193
- # You may need to experiment with x/y coordinates or font sizes
194
- # to make it look right in 520×728.
195
  title_x, title_y = 33, 150
196
  details_x, details_y = 480, 256
197
  energy_x, energy_y = 480, 472
198
 
199
- # Text 1 (title) - model and provider separated on different lines
200
- draw.text((title_x, title_y), str(model_data['model']), font=title_font, fill="black")
201
- draw.text((title_x, title_y + 38), str(model_data['provider']), font=title_font, fill="black")
202
 
203
  # Text 2 (details)
204
  details_lines = [
@@ -210,7 +208,7 @@ def create_label_single_pass(background_image, model_data, final_size=(520, 728)
210
  bbox = draw.textbbox((0, 0), line, font=details_font)
211
  text_width = bbox[2] - bbox[0]
212
  # Right-justify the details text at details_x
213
- draw.text((details_x - text_width, details_y + i*47), line, font=details_font, fill="black")
214
 
215
  # Text 3 (energy)
216
  energy_text = str(model_data['energy'])
 
7
  # Sidebar logo and title
8
  with st.sidebar:
9
  col1, col2 = st.columns([1, 5]) # Shrink the logo column and expand the text column
10
+
11
  with col1:
12
  logo = Image.open("logo.png")
13
  resized_logo = logo.resize((40, 40)) # Resize the logo
14
  st.image(resized_logo)
15
+
16
  with col2:
17
  st.markdown(
18
  """
 
31
  unsafe_allow_html=True,
32
  )
33
 
34
+ # (Removed the "Generate a Label to Display your AI Energy Score" section)
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  st.sidebar.markdown("<hr style='border: 1px solid gray; margin: 15px 0;'>", unsafe_allow_html=True)
37
 
38
+ # Update instructions header
39
+ st.sidebar.write("### Generate Label:")
40
+
41
  # Define the ordered list of tasks.
42
  task_order = [
43
  "Text Generation",
 
51
  "Question Answering",
52
  "Sentence Similarity"
53
  ]
54
+
55
+ # Make the task selection label green and remove redundant text.
56
+ st.sidebar.markdown('<p style="color: green; font-size: 16px;">Task(s):</p>', unsafe_allow_html=True)
57
+ selected_tasks = st.sidebar.multiselect("", options=task_order, default=task_order)
58
+
 
 
 
 
59
  # Mapping from task to CSV file name.
60
  task_to_file = {
61
  "Text Generation": "text_gen_energyscore.csv",
 
69
  "Question Answering": "question_answering_energyscore.csv",
70
  "Sentence Similarity": "sentence_similarity_energyscore.csv"
71
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
+ # Default placeholder model data.
74
+ default_model_data = {
75
+ 'provider': "AI Provider",
76
+ 'model': "Model Name",
77
+ 'full_model': "AI Provider/Model Name",
78
+ 'date': "",
79
+ 'task': "",
80
+ 'hardware': "",
81
+ 'energy': "?",
82
+ 'score': 5
83
+ }
84
+
85
+ if not selected_tasks:
86
+ # If no tasks are selected, use generic placeholder.
87
+ model_data = default_model_data
88
+ else:
89
+ dfs = []
90
+ # Load and process each CSV corresponding to the selected tasks.
91
+ for task in selected_tasks:
92
+ file_name = task_to_file[task]
93
+ try:
94
+ df = pd.read_csv(file_name)
95
+ except FileNotFoundError:
96
+ st.sidebar.error(f"Could not find '{file_name}' for task {task}!")
97
+ continue
98
+ except Exception as e:
99
+ st.sidebar.error(f"Error reading '{file_name}' for task {task}: {e}")
100
+ continue
101
+
102
+ # Save the original full model string and then split the "model" column
103
+ df['full_model'] = df['model']
104
+ df[['provider', 'model']] = df['model'].str.split(pat='/', n=1, expand=True)
105
+ # Round total_gpu_energy to 3 decimal places and assign to 'energy'
106
+ df['energy'] = df['total_gpu_energy'].round(3)
107
+ # Use the energy_score column as 'score' (fill missing values with 1 to avoid casting errors)
108
+ df['score'] = df['energy_score'].fillna(1).astype(int)
109
+ # Hardcode date and hardware
110
+ df['date'] = "February 2025"
111
+ df['hardware'] = "NVIDIA H100-80GB"
112
+ # Set the task from the file name mapping
113
+ df['task'] = task
114
+
115
+ dfs.append(df)
116
+
117
+ if not dfs:
118
+ model_data = default_model_data
119
+ else:
120
+ data_df = pd.concat(dfs, ignore_index=True)
121
+ if data_df.empty:
122
+ model_data = default_model_data
123
+ else:
124
+ # In the scored model dropdown show the full model string.
125
+ model_options = data_df["full_model"].unique().tolist()
126
+ selected_model = st.sidebar.selectbox(
127
+ "Scored Models",
128
+ model_options,
129
+ help="Start typing to search for a model"
130
+ )
131
+ model_data = data_df[data_df["full_model"] == selected_model].iloc[0]
132
+
133
+ st.sidebar.write("#### 2. Select a model below")
134
+ st.sidebar.write("#### 3. Download the label")
135
+
136
+ # Select background by score (using generic placeholder score=5 if applicable)
137
  try:
138
  score = int(model_data["score"])
139
  background_path = f"{score}.png"
 
162
  mime="image/png"
163
  )
164
 
165
+ st.sidebar.write("#### 4. Share your label! [Guidelines](https://huggingface.github.io/AIEnergyScore/#labelusage)")
166
  st.sidebar.markdown("<hr style='border: 1px solid gray; margin: 15px 0;'>", unsafe_allow_html=True)
167
  st.sidebar.write("### Key Links")
168
  st.sidebar.write("- [Leaderboard](https://huggingface.co/spaces/AIEnergyScore/Leaderboard)")
169
+ st.sidebar.write("- [Submission Portal](https://huggingface.co/spaces/AIEnergyScore/submission_portal)")
170
  st.sidebar.write("- [FAQ](https://huggingface.github.io/AIEnergyScore/#faq)")
171
  st.sidebar.write("- [Documentation](https://huggingface.github.io/AIEnergyScore/#documentation)")
172
+
173
 
174
  def create_label_single_pass(background_image, model_data, final_size=(520, 728)):
175
  """
 
188
  st.error(f"Font loading failed: {e}")
189
  return bg_resized
190
 
191
+ # 3. Place your text.
192
+ # Flip the order so that the provider (AI Developer) is shown first and the model name second.
 
193
  title_x, title_y = 33, 150
194
  details_x, details_y = 480, 256
195
  energy_x, energy_y = 480, 472
196
 
197
+ # Text 1 (title) show provider first then model name
198
+ draw.text((title_x, title_y), str(model_data['provider']), font=title_font, fill="black")
199
+ draw.text((title_x, title_y + 38), str(model_data['model']), font=title_font, fill="black")
200
 
201
  # Text 2 (details)
202
  details_lines = [
 
208
  bbox = draw.textbbox((0, 0), line, font=details_font)
209
  text_width = bbox[2] - bbox[0]
210
  # Right-justify the details text at details_x
211
+ draw.text((details_x - text_width, details_y + i * 47), line, font=details_font, fill="black")
212
 
213
  # Text 3 (energy)
214
  energy_text = str(model_data['energy'])