bgamazay commited on
Commit
c3f4f1f
·
verified ·
1 Parent(s): f8f25eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -94
app.py CHANGED
@@ -24,103 +24,101 @@ tasks = [
24
  'summarization.csv'
25
  ]
26
 
27
- color_map = {"1": "red", "2": "orange", "3": "yellow", "4": "lightgreen", "5": "green"} # Keep color map
28
-
29
  def format_stars(score):
30
  try:
31
  score_int = int(score)
32
  except Exception:
33
  score_int = 0
34
  # Render stars in black with a slightly larger font
35
- return f'<span style="color: black !important; font-size:1.5em !important;">{"★" * score_int}</span>'
36
 
37
  def make_link(mname):
38
  parts = str(mname).split('/')
39
  display_name = parts[1] if len(parts) > 1 else mname
40
- return f'[{display_name}](https://huggingface.co/{mname})'
41
-
42
- # --- Leaderboard Table Functions (Back to datatype="markdown" approach) ---
43
-
44
- def create_minimal_bar_html(energy_value_wh, energy_score, max_energy_value):
45
- """Generates HTML for the minimal bar chart."""
46
- if max_energy_value <= 0: # Avoid division by zero if max energy is 0 or negative
47
- bar_percentage = 0
48
- else:
49
- bar_percentage = min(100, (energy_value_wh / max_energy_value) * 100) # Cap at 100%
50
- bar_color = color_map.get(str(energy_score), "gray") # Default color if score is unexpected
51
-
52
- html = f"""<div class='minimal-bar-container'>
53
- <div class='minimal-bar' style='width: {bar_percentage}%; background-color: {bar_color};'></div>
54
- <span style='margin-left: 5px;'>{energy_value_wh:.4f} Wh</span>
55
- </div>""" # Added classes and inline styles for better control
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  return html
57
 
58
-
59
- def get_model_names(task):
60
  df = pd.read_csv('data/energy/' + task)
61
  if df.columns[0].startswith("Unnamed:"):
62
  df = df.iloc[:, 1:]
63
- df['total_gpu_energy'] = pd.to_numeric(df['total_gpu_energy'], errors='raise') * 1000 # kWh to Wh conversion
64
  df['energy_score'] = df['energy_score'].astype(int)
65
-
66
- df = df.sort_values(by='total_gpu_energy') # Sort BEFORE creating HTML column
67
- max_energy_for_task = df['total_gpu_energy'].max() # Calculate max energy for this task
68
-
69
- # Create HTML bar chart for GPU Energy column, passing dynamic max_energy_for_task
70
- df['GPU Energy (Wh)'] = df.apply(lambda row: create_minimal_bar_html(row['total_gpu_energy'], row['energy_score'], max_energy_for_task), axis=1)
71
-
72
  df['Model'] = df['model'].apply(make_link)
73
  df['Score'] = df['energy_score'].apply(format_stars)
74
- df = df[['Model', 'GPU Energy (Wh)', 'Score']] # Keep only these columns
75
- return df
76
 
77
- def get_all_model_names():
78
  all_df = pd.DataFrame()
79
- max_energy_overall = 0 # Initialize overall max energy
80
  for task in tasks:
81
  df = pd.read_csv('data/energy/' + task)
82
- df['total_gpu_energy'] = pd.to_numeric(df['total_gpu_energy'], errors='raise') * 1000 # kWh to Wh conversion
 
83
  df['energy_score'] = df['energy_score'].astype(int)
 
 
 
84
  all_df = pd.concat([all_df, df], ignore_index=True)
85
-
86
  all_df = all_df.drop_duplicates(subset=['model'])
87
- all_df = all_df.sort_values(by='total_gpu_energy') # Sort ALL DATA before calculating max and creating HTML
88
-
89
- max_energy_overall = all_df['total_gpu_energy'].max() # Calculate overall max AFTER sorting
90
-
91
- # Create HTML bar chart for GPU Energy column, passing dynamic max_energy_overall
92
- all_df['GPU Energy (Wh)'] = all_df.apply(lambda row: create_minimal_bar_html(row['total_gpu_energy'], row['energy_score'], max_energy_overall), axis=1)
93
- all_df['Model'] = all_df['model'].apply(make_link)
94
- all_df['Score'] = all_df['energy_score'].apply(format_stars)
95
- all_df = all_df[['Model', 'GPU Energy (Wh)', 'Score']]
96
- return all_df[['Model', 'GPU Energy (Wh)', 'Score']]
97
 
98
-
99
- def get_text_generation_model_names(model_class):
100
  df = pd.read_csv('data/energy/text_generation.csv')
101
  if df.columns[0].startswith("Unnamed:"):
102
  df = df.iloc[:, 1:]
103
  if 'class' in df.columns:
104
  df = df[df['class'] == model_class]
105
- df['total_gpu_energy'] = pd.to_numeric(df['total_gpu_energy'], errors='raise') * 1000 # kWh to Wh conversion
106
  df['energy_score'] = df['energy_score'].astype(int)
107
-
108
- df = df.sort_values(by='total_gpu_energy') # Sort BEFORE creating HTML column
109
- max_energy_for_class = df['total_gpu_energy'].max() # Calculate max energy for this class
110
-
111
- # Create HTML bar chart for GPU Energy column, passing dynamic max_energy_for_class
112
- df['GPU Energy (Wh)'] = df.apply(lambda row: create_minimal_bar_html(row['total_gpu_energy'], row['energy_score'], max_energy_for_class), axis=1)
113
-
114
  df['Model'] = df['model'].apply(make_link)
115
  df['Score'] = df['energy_score'].apply(format_stars)
116
- df = df[['Model', 'GPU Energy (Wh)', 'Score']] # Keep only these columns
117
- return df
118
 
119
  def update_text_generation(model_class):
120
- table = get_text_generation_model_names(model_class)
121
- return table
122
 
123
- # --- Build the Gradio Interface (Plots Removed, Tables with Dynamic Bars using datatype="markdown") ---
124
 
125
  demo = gr.Blocks(css="""
126
  .gr-dataframe table {
@@ -133,25 +131,6 @@ demo = gr.Blocks(css="""
133
  overflow: hidden;
134
  text-overflow: ellipsis;
135
  }
136
- /* CSS for minimal bar chart inside table cell - more specific CSS */
137
- .gr-dataframe td > .minimal-bar-container { /* Target minimal-bar-container WITHIN dataframe cells */
138
- display: flex;
139
- align-items: center;
140
- gap: 5px;
141
- margin: 0; /* Reset margins */
142
- padding: 0; /* Reset paddings */
143
- line-height: normal; /* Reset line-height */
144
- }
145
- .gr-dataframe td > .minimal-bar-container > .minimal-bar { /* Target minimal-bar WITHIN container in dataframe cells */
146
- height: 10px;
147
- background-color: blue; /* default, will be overridden by dynamic color */
148
- border-radius: 2px;
149
- }
150
- .gr-dataframe td > .minimal-bar-container > span { /* Target span for text value in dataframe cells */
151
- font-size: 0.9em; /* Adjust text size if needed */
152
- color: #333; /* Adjust text color if needed */
153
- }
154
-
155
  """)
156
 
157
  with demo:
@@ -164,45 +143,43 @@ Select different tasks to see scored models. Submit open models for testing and
164
  with gr.Tabs():
165
  # --- Text Generation Tab with Dropdown for Model Class ---
166
  with gr.TabItem("Text Generation 💬"):
167
- # Dropdown moved above the leaderboard
168
  model_class_dropdown = gr.Dropdown(choices=["A", "B", "C"],
169
  label="Select Model Class",
170
  value="A")
171
- tg_table = gr.Dataframe(get_text_generation_model_names("A"), datatype="markdown") # IMPORTANT: datatype="markdown"
172
- # Update table when the dropdown value changes
173
  model_class_dropdown.change(fn=update_text_generation,
174
  inputs=model_class_dropdown,
175
- outputs=[tg_table])
176
 
177
  with gr.TabItem("Image Generation 📷"):
178
- table = gr.Dataframe(get_model_names('image_generation.csv'), datatype="markdown") # IMPORTANT: datatype="markdown"
179
 
180
  with gr.TabItem("Text Classification 🎭"):
181
- table = gr.Dataframe(get_model_names('text_classification.csv'), datatype="markdown") # IMPORTANT: datatype="markdown"
182
 
183
  with gr.TabItem("Image Classification 🖼️"):
184
- table = gr.Dataframe(get_model_names('image_classification.csv'), datatype="markdown") # IMPORTANT: datatype="markdown"
185
 
186
  with gr.TabItem("Image Captioning 📝"):
187
- table = gr.Dataframe(get_model_names('image_captioning.csv'), datatype="markdown") # IMPORTANT: datatype="markdown"
188
 
189
  with gr.TabItem("Summarization 📃"):
190
- table = gr.Dataframe(get_model_names('summarization.csv'), datatype="markdown") # IMPORTANT: datatype="markdown"
191
 
192
  with gr.TabItem("Automatic Speech Recognition 💬"):
193
- table = gr.Dataframe(get_model_names('asr.csv'), datatype="markdown") # IMPORTANT: datatype="markdown"
194
 
195
  with gr.TabItem("Object Detection 🚘"):
196
- table = gr.Dataframe(get_model_names('object_detection.csv'), datatype="markdown") # IMPORTANT: datatype="markdown"
197
 
198
  with gr.TabItem("Sentence Similarity 📚"):
199
- table = gr.Dataframe(get_model_names('sentence_similarity.csv'), datatype="markdown") # IMPORTANT: datatype="markdown"
200
 
201
  with gr.TabItem("Extractive QA ❔"):
202
- table = gr.Dataframe(get_model_names('question_answering.csv'), datatype="markdown") # IMPORTANT: datatype="markdown"
203
 
204
  with gr.TabItem("All Tasks 💡"):
205
- table = gr.Dataframe(get_all_model_names(), datatype="markdown") # IMPORTANT: datatype="markdown"
206
 
207
  with gr.Accordion("📙 Citation", open=False):
208
  citation_button = gr.Textbox(
@@ -216,4 +193,4 @@ Select different tasks to see scored models. Submit open models for testing and
216
  """Last updated: February 2025"""
217
  )
218
 
219
- demo.launch(share=True)
 
24
  'summarization.csv'
25
  ]
26
 
 
 
27
  def format_stars(score):
28
  try:
29
  score_int = int(score)
30
  except Exception:
31
  score_int = 0
32
  # Render stars in black with a slightly larger font
33
+ return f'<span style="color: black; font-size:1.5em;">{"★" * score_int}</span>'
34
 
35
  def make_link(mname):
36
  parts = str(mname).split('/')
37
  display_name = parts[1] if len(parts) > 1 else mname
38
+ return f'<a href="https://huggingface.co/{mname}" target="_blank">{display_name}</a>'
39
+
40
+ def generate_html_table_from_df(df):
41
+ """
42
+ Given a dataframe that already includes:
43
+ - 'gpu_energy_numeric': numeric energy (in Wh)
44
+ - 'Model': the model link HTML,
45
+ - 'Score': the HTML stars,
46
+ - and 'energy_score' as an integer,
47
+ generate an HTML table that shows the energy value plus a horizontal bar whose width is computed
48
+ relative to the maximum energy.
49
+ """
50
+ max_energy = df['gpu_energy_numeric'].max() if not df.empty else 1
51
+ color_map = {"1": "red", "2": "orange", "3": "yellow", "4": "lightgreen", "5": "green"}
52
+ html = '<table style="width:100%; border-collapse: collapse; font-family: Arial, sans-serif;">'
53
+ html += '<thead><tr style="background-color: #f2f2f2;">'
54
+ html += '<th style="text-align: left; padding: 8px;">Model</th>'
55
+ html += '<th style="text-align: left; padding: 8px;">GPU Energy (Wh)</th>'
56
+ html += '<th style="text-align: left; padding: 8px;">Score</th>'
57
+ html += '</tr></thead>'
58
+ html += '<tbody>'
59
+ for _, row in df.iterrows():
60
+ energy_numeric = row['gpu_energy_numeric']
61
+ energy_str = f"{energy_numeric:.4f}"
62
+ # Compute the relative width (as a percentage) for the horizontal bar
63
+ bar_width = (energy_numeric / max_energy) * 100
64
+ score_val = row['energy_score']
65
+ bar_color = color_map.get(str(score_val), "gray")
66
+ html += '<tr>'
67
+ html += f'<td style="padding: 8px;">{row["Model"]}</td>'
68
+ html += (
69
+ f'<td style="padding: 8px;">{energy_str}<br>'
70
+ f'<div style="background-color: {bar_color}; width: {bar_width:.1f}%; height: 10px;"></div></td>'
71
+ )
72
+ html += f'<td style="padding: 8px;">{row["Score"]}</td>'
73
+ html += '</tr>'
74
+ html += '</tbody></table>'
75
  return html
76
 
77
+ def get_model_names_html(task):
 
78
  df = pd.read_csv('data/energy/' + task)
79
  if df.columns[0].startswith("Unnamed:"):
80
  df = df.iloc[:, 1:]
81
+ # Convert energy_score to integer and total_gpu_energy from kWh to Wh
82
  df['energy_score'] = df['energy_score'].astype(int)
83
+ df['gpu_energy_numeric'] = pd.to_numeric(df['total_gpu_energy'], errors='raise') * 1000
 
 
 
 
 
 
84
  df['Model'] = df['model'].apply(make_link)
85
  df['Score'] = df['energy_score'].apply(format_stars)
86
+ df = df.sort_values(by='gpu_energy_numeric')
87
+ return generate_html_table_from_df(df)
88
 
89
+ def get_all_model_names_html():
90
  all_df = pd.DataFrame()
 
91
  for task in tasks:
92
  df = pd.read_csv('data/energy/' + task)
93
+ if df.columns[0].startswith("Unnamed:"):
94
+ df = df.iloc[:, 1:]
95
  df['energy_score'] = df['energy_score'].astype(int)
96
+ df['gpu_energy_numeric'] = pd.to_numeric(df['total_gpu_energy'], errors='raise') * 1000
97
+ df['Model'] = df['model'].apply(make_link)
98
+ df['Score'] = df['energy_score'].apply(format_stars)
99
  all_df = pd.concat([all_df, df], ignore_index=True)
 
100
  all_df = all_df.drop_duplicates(subset=['model'])
101
+ all_df = all_df.sort_values(by='gpu_energy_numeric')
102
+ return generate_html_table_from_df(all_df)
 
 
 
 
 
 
 
 
103
 
104
+ def get_text_generation_model_names_html(model_class):
 
105
  df = pd.read_csv('data/energy/text_generation.csv')
106
  if df.columns[0].startswith("Unnamed:"):
107
  df = df.iloc[:, 1:]
108
  if 'class' in df.columns:
109
  df = df[df['class'] == model_class]
 
110
  df['energy_score'] = df['energy_score'].astype(int)
111
+ df['gpu_energy_numeric'] = pd.to_numeric(df['total_gpu_energy'], errors='raise') * 1000
 
 
 
 
 
 
112
  df['Model'] = df['model'].apply(make_link)
113
  df['Score'] = df['energy_score'].apply(format_stars)
114
+ df = df.sort_values(by='gpu_energy_numeric')
115
+ return generate_html_table_from_df(df)
116
 
117
  def update_text_generation(model_class):
118
+ table_html = get_text_generation_model_names_html(model_class)
119
+ return table_html
120
 
121
+ # --- Build the Gradio Interface ---
122
 
123
  demo = gr.Blocks(css="""
124
  .gr-dataframe table {
 
131
  overflow: hidden;
132
  text-overflow: ellipsis;
133
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  """)
135
 
136
  with demo:
 
143
  with gr.Tabs():
144
  # --- Text Generation Tab with Dropdown for Model Class ---
145
  with gr.TabItem("Text Generation 💬"):
 
146
  model_class_dropdown = gr.Dropdown(choices=["A", "B", "C"],
147
  label="Select Model Class",
148
  value="A")
149
+ tg_table = gr.HTML(get_text_generation_model_names_html("A"))
 
150
  model_class_dropdown.change(fn=update_text_generation,
151
  inputs=model_class_dropdown,
152
+ outputs=tg_table)
153
 
154
  with gr.TabItem("Image Generation 📷"):
155
+ gr.HTML(get_model_names_html('image_generation.csv'))
156
 
157
  with gr.TabItem("Text Classification 🎭"):
158
+ gr.HTML(get_model_names_html('text_classification.csv'))
159
 
160
  with gr.TabItem("Image Classification 🖼️"):
161
+ gr.HTML(get_model_names_html('image_classification.csv'))
162
 
163
  with gr.TabItem("Image Captioning 📝"):
164
+ gr.HTML(get_model_names_html('image_captioning.csv'))
165
 
166
  with gr.TabItem("Summarization 📃"):
167
+ gr.HTML(get_model_names_html('summarization.csv'))
168
 
169
  with gr.TabItem("Automatic Speech Recognition 💬"):
170
+ gr.HTML(get_model_names_html('asr.csv'))
171
 
172
  with gr.TabItem("Object Detection 🚘"):
173
+ gr.HTML(get_model_names_html('object_detection.csv'))
174
 
175
  with gr.TabItem("Sentence Similarity 📚"):
176
+ gr.HTML(get_model_names_html('sentence_similarity.csv'))
177
 
178
  with gr.TabItem("Extractive QA ❔"):
179
+ gr.HTML(get_model_names_html('question_answering.csv'))
180
 
181
  with gr.TabItem("All Tasks 💡"):
182
+ gr.HTML(get_all_model_names_html())
183
 
184
  with gr.Accordion("📙 Citation", open=False):
185
  citation_button = gr.Textbox(
 
193
  """Last updated: February 2025"""
194
  )
195
 
196
+ demo.launch()