bgamazay commited on
Commit
8b7dfb4
·
verified ·
1 Parent(s): 8c74b2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -32
app.py CHANGED
@@ -39,22 +39,21 @@ def make_link(mname):
39
 
40
  def get_plots(task):
41
  df = pd.read_csv('data/energy/' + task)
42
- # Remove extra unnamed column if present
43
  if df.columns[0].startswith("Unnamed:"):
44
  df = df.iloc[:, 1:]
45
-
46
- # Convert energy_score to int and then to str so it's treated as categorical
 
47
  df['energy_score'] = df['energy_score'].astype(int).astype(str)
48
  df['Display Model'] = df['model'].apply(lambda m: m.split('/')[-1])
49
 
50
- # Update color_map keys to be strings
51
  color_map = {"1": "red", "2": "orange", "3": "yellow", "4": "lightgreen", "5": "green"}
52
 
53
  fig = px.scatter(
54
  df,
55
- x="total_gpu_energy", # x-axis: GPU energy consumption
56
- y="Display Model", # y-axis: Model name for display
57
- color="energy_score", # Discrete color based on energy score
58
  custom_data=['energy_score'],
59
  height=500,
60
  width=800,
@@ -76,7 +75,7 @@ def get_all_plots():
76
  df = pd.read_csv('data/energy/' + task)
77
  if df.columns[0].startswith("Unnamed:"):
78
  df = df.iloc[:, 1:]
79
- # Convert energy_score to categorical string
80
  df['energy_score'] = df['energy_score'].astype(int).astype(str)
81
  df['Display Model'] = df['model'].apply(lambda m: m.split('/')[-1])
82
  all_df = pd.concat([all_df, df], ignore_index=True)
@@ -86,9 +85,9 @@ def get_all_plots():
86
 
87
  fig = px.scatter(
88
  all_df,
89
- x="total_gpu_energy", # x-axis: GPU energy consumption
90
  y="Display Model",
91
- color="energy_score", # Discrete color mapping
92
  custom_data=['energy_score'],
93
  height=500,
94
  width=800,
@@ -105,20 +104,11 @@ def get_all_plots():
105
  return fig
106
 
107
  def get_model_names(task):
108
- """
109
- For a given task, load the energy CSV and return a dataframe with the following columns:
110
- - Model (a markdown link)
111
- - GPU Energy (Wh) formatted as a string with 4 decimal places
112
- - Score (a star rating based on energy_score)
113
- For text_generation.csv only, also add the "Class" column from the CSV.
114
- The final column order is: Model, GPU Energy (Wh), Score, [Class].
115
- """
116
  df = pd.read_csv('data/energy/' + task)
117
  if df.columns[0].startswith("Unnamed:"):
118
  df = df.iloc[:, 1:]
119
  df['energy_score'] = df['energy_score'].astype(int)
120
- # Format the energy as a string with 4 decimals
121
- df['GPU Energy (Wh)'] = df['total_gpu_energy'].apply(lambda x: f"{x:.4f}")
122
  df['Model'] = df['model'].apply(make_link)
123
  df['Score'] = df['energy_score'].apply(format_stars)
124
 
@@ -132,16 +122,11 @@ def get_model_names(task):
132
  return df
133
 
134
  def get_all_model_names():
135
- """
136
- Combine data from all tasks and return a leaderboard table with:
137
- - Model, GPU Energy (Wh), Score
138
- Duplicate models are dropped.
139
- """
140
  all_df = pd.DataFrame()
141
  for task in tasks:
142
  df = pd.read_csv('data/energy/' + task)
143
  df['energy_score'] = df['energy_score'].astype(int)
144
- df['GPU Energy (Wh)'] = df['total_gpu_energy'].apply(lambda x: f"{x:.4f}")
145
  df['Model'] = df['model'].apply(make_link)
146
  df['Score'] = df['energy_score'].apply(format_stars)
147
  all_df = pd.concat([all_df, df], ignore_index=True)
@@ -149,8 +134,64 @@ def get_all_model_names():
149
  all_df = all_df.sort_values(by='GPU Energy (Wh)')
150
  return all_df[['Model', 'GPU Energy (Wh)', 'Score']]
151
 
152
- # Build the Gradio interface.
153
- # The css argument below makes all tables (e.g. leaderboard) use a fixed layout with narrower columns.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
  demo = gr.Blocks(css="""
155
  .gr-dataframe table {
156
  table-layout: fixed;
@@ -175,13 +216,20 @@ Click through the tasks below to see how different models measure up in terms of
175
  )
176
 
177
  with gr.Tabs():
 
178
  with gr.TabItem("Text Generation 💬"):
179
  with gr.Row():
180
  with gr.Column(scale=1.3):
181
- plot = gr.Plot(get_plots('text_generation.csv'))
182
  with gr.Column(scale=1):
183
- table = gr.Dataframe(get_model_names('text_generation.csv'), datatype="markdown")
184
-
 
 
 
 
 
 
185
  with gr.TabItem("Image Generation 📷"):
186
  with gr.Row():
187
  with gr.Column():
@@ -248,7 +296,6 @@ Click through the tasks below to see how different models measure up in terms of
248
  with gr.TabItem("All Tasks 💡"):
249
  with gr.Row():
250
  with gr.Column():
251
- # Call the functions to generate the plot and table
252
  plot = gr.Plot(get_all_plots())
253
  with gr.Column():
254
  table = gr.Dataframe(get_all_model_names(), datatype="markdown")
 
39
 
40
  def get_plots(task):
41
  df = pd.read_csv('data/energy/' + task)
 
42
  if df.columns[0].startswith("Unnamed:"):
43
  df = df.iloc[:, 1:]
44
+ # Ensure total_gpu_energy is a float so that values are not misinterpreted
45
+ df['total_gpu_energy'] = df['total_gpu_energy'].astype(float)
46
+ # Convert energy_score to a categorical string for discrete coloring
47
  df['energy_score'] = df['energy_score'].astype(int).astype(str)
48
  df['Display Model'] = df['model'].apply(lambda m: m.split('/')[-1])
49
 
 
50
  color_map = {"1": "red", "2": "orange", "3": "yellow", "4": "lightgreen", "5": "green"}
51
 
52
  fig = px.scatter(
53
  df,
54
+ x="total_gpu_energy",
55
+ y="Display Model",
56
+ color="energy_score",
57
  custom_data=['energy_score'],
58
  height=500,
59
  width=800,
 
75
  df = pd.read_csv('data/energy/' + task)
76
  if df.columns[0].startswith("Unnamed:"):
77
  df = df.iloc[:, 1:]
78
+ df['total_gpu_energy'] = df['total_gpu_energy'].astype(float)
79
  df['energy_score'] = df['energy_score'].astype(int).astype(str)
80
  df['Display Model'] = df['model'].apply(lambda m: m.split('/')[-1])
81
  all_df = pd.concat([all_df, df], ignore_index=True)
 
85
 
86
  fig = px.scatter(
87
  all_df,
88
+ x="total_gpu_energy",
89
  y="Display Model",
90
+ color="energy_score",
91
  custom_data=['energy_score'],
92
  height=500,
93
  width=800,
 
104
  return fig
105
 
106
  def get_model_names(task):
 
 
 
 
 
 
 
 
107
  df = pd.read_csv('data/energy/' + task)
108
  if df.columns[0].startswith("Unnamed:"):
109
  df = df.iloc[:, 1:]
110
  df['energy_score'] = df['energy_score'].astype(int)
111
+ df['GPU Energy (Wh)'] = df['total_gpu_energy'].astype(float).apply(lambda x: f"{x:.4f}")
 
112
  df['Model'] = df['model'].apply(make_link)
113
  df['Score'] = df['energy_score'].apply(format_stars)
114
 
 
122
  return df
123
 
124
  def get_all_model_names():
 
 
 
 
 
125
  all_df = pd.DataFrame()
126
  for task in tasks:
127
  df = pd.read_csv('data/energy/' + task)
128
  df['energy_score'] = df['energy_score'].astype(int)
129
+ df['GPU Energy (Wh)'] = df['total_gpu_energy'].astype(float).apply(lambda x: f"{x:.4f}")
130
  df['Model'] = df['model'].apply(make_link)
131
  df['Score'] = df['energy_score'].apply(format_stars)
132
  all_df = pd.concat([all_df, df], ignore_index=True)
 
134
  all_df = all_df.sort_values(by='GPU Energy (Wh)')
135
  return all_df[['Model', 'GPU Energy (Wh)', 'Score']]
136
 
137
+ # === New functions for Text Generation filtering by model class ===
138
+ def get_text_generation_plots(model_class):
139
+ df = pd.read_csv('data/energy/text_generation.csv')
140
+ if df.columns[0].startswith("Unnamed:"):
141
+ df = df.iloc[:, 1:]
142
+ # Filter to the selected model class (if a "class" column exists)
143
+ if 'class' in df.columns:
144
+ df = df[df['class'] == model_class]
145
+ df['total_gpu_energy'] = df['total_gpu_energy'].astype(float)
146
+ df['energy_score'] = df['energy_score'].astype(int).astype(str)
147
+ df['Display Model'] = df['model'].apply(lambda m: m.split('/')[-1])
148
+
149
+ color_map = {"1": "red", "2": "orange", "3": "yellow", "4": "lightgreen", "5": "green"}
150
+
151
+ fig = px.scatter(
152
+ df,
153
+ x="total_gpu_energy",
154
+ y="Display Model",
155
+ color="energy_score",
156
+ custom_data=['energy_score'],
157
+ height=500,
158
+ width=800,
159
+ color_discrete_map=color_map
160
+ )
161
+ fig.update_traces(
162
+ hovertemplate="<br>".join([
163
+ "Model: %{y}",
164
+ "GPU Energy (Wh): %{x}",
165
+ "Energy Score: %{customdata[0]}"
166
+ ])
167
+ )
168
+ fig.update_layout(xaxis_title="GPU Energy (Wh)", yaxis_title="Model")
169
+ return fig
170
+
171
+ def get_text_generation_model_names(model_class):
172
+ df = pd.read_csv('data/energy/text_generation.csv')
173
+ if df.columns[0].startswith("Unnamed:"):
174
+ df = df.iloc[:, 1:]
175
+ if 'class' in df.columns:
176
+ df = df[df['class'] == model_class]
177
+ df['energy_score'] = df['energy_score'].astype(int)
178
+ df['GPU Energy (Wh)'] = df['total_gpu_energy'].astype(float).apply(lambda x: f"{x:.4f}")
179
+ df['Model'] = df['model'].apply(make_link)
180
+ df['Score'] = df['energy_score'].apply(format_stars)
181
+ if 'class' in df.columns:
182
+ df['Class'] = df['class']
183
+ df = df[['Model', 'GPU Energy (Wh)', 'Score', 'Class']]
184
+ else:
185
+ df = df[['Model', 'GPU Energy (Wh)', 'Score']]
186
+ df = df.sort_values(by='GPU Energy (Wh)')
187
+ return df
188
+
189
+ def update_text_generation(model_class):
190
+ plot = get_text_generation_plots(model_class)
191
+ table = get_text_generation_model_names(model_class)
192
+ return plot, table
193
+
194
+ # === Build the Gradio Interface ===
195
  demo = gr.Blocks(css="""
196
  .gr-dataframe table {
197
  table-layout: fixed;
 
216
  )
217
 
218
  with gr.Tabs():
219
+ # --- Text Generation Tab with Dropdown for Model Class ---
220
  with gr.TabItem("Text Generation 💬"):
221
  with gr.Row():
222
  with gr.Column(scale=1.3):
223
+ tg_plot = gr.Plot(get_text_generation_plots("A"))
224
  with gr.Column(scale=1):
225
+ tg_table = gr.Dataframe(get_text_generation_model_names("A"), datatype="markdown")
226
+ model_class_dropdown = gr.Dropdown(choices=["A", "B", "C"],
227
+ label="Select Model Class",
228
+ value="A")
229
+ model_class_dropdown.change(fn=update_text_generation,
230
+ inputs=model_class_dropdown,
231
+ outputs=[tg_plot, tg_table])
232
+
233
  with gr.TabItem("Image Generation 📷"):
234
  with gr.Row():
235
  with gr.Column():
 
296
  with gr.TabItem("All Tasks 💡"):
297
  with gr.Row():
298
  with gr.Column():
 
299
  plot = gr.Plot(get_all_plots())
300
  with gr.Column():
301
  table = gr.Dataframe(get_all_model_names(), datatype="markdown")