File size: 9,317 Bytes
e4890d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import Papa from 'papaparse';
import { DataTable } from 'simple-datatables';

const languageMap = {
  'All Languages': 'final_rankings.csv',
  'Arabic': 'results_ar.csv',
  'Turkish': 'results_tr.csv',
  'Swahili': 'results_sw.csv',
  'Russian': 'results_ru.csv',
  'Telugu': 'results_te.csv',
  'Thai': 'results_th.csv',
  'Chinese': 'results_zh.csv',
  'French': 'results_fr.csv',
  'Hindi': 'results_hi.csv',
};

const columnNameMap = {
  'runname': 'Model',
  'agg_score_macro': 'Macro Score',
  'agg_score_RES': 'RES Score',
  'agg_score_RC': 'RC Score',
  'agg_score_GK': 'GK Score',
  'agg_score_NLU': 'NLU Score',
  'avg_rank_macro': 'Average Rank',
  'rank': 'Rank'
};

function createDropdown(options, onChange) {
  const select = document.createElement('select');
  options.forEach(option => {
    const optionElement = document.createElement('option');
    optionElement.value = option;
    optionElement.textContent = option;
    select.appendChild(optionElement);
  });
  select.addEventListener('change', onChange);
  return select;
}

function processTaskName(taskName) {
  const parts = taskName.split('|');
  let processedName = parts.length > 1 ? parts[1] : taskName;
  processedName = processedName.split('_mcf')[0].split('_cf')[0];
  return processedName;
}

function sanitizeColumnName(name) {
  return name.replace(/[^a-zA-Z0-9-_]/g, '_');
}

function createResultsTable(data, extraColumn) {
  const tableWrapper = document.createElement('div');
  tableWrapper.className = 'table-wrapper leaderboard-table-wrapper';

  const table = document.createElement('table');
  table.className = 'results-table leaderboard-results-table';

  const columns = extraColumn === 'All Languages' 
    ? ['rank', 'runname', 'avg_rank_macro']
    : ['rank', 'runname', 'agg_score_macro', extraColumn].filter(Boolean);

  const header = table.createTHead();
  const headerRow = header.insertRow();
  columns.forEach(column => {
    const th = document.createElement('th');
    th.textContent = columnNameMap[column] || processTaskName(column);
    th.className = `column-${sanitizeColumnName(column)}`; // Sanitize the column name
    headerRow.appendChild(th);
  });

  const body = table.createTBody();
  data.forEach((row, index) => {
    if (!row.runname) return; // Skip rows without a model name
    const tr = body.insertRow();
    columns.forEach(column => {
      const td = tr.insertCell();
      td.className = `column-${sanitizeColumnName(column)}`; // Sanitize the column name
      if (column === 'rank') {
        td.textContent = index + 1;
      } else if (column === 'runname') {
        const modelName = row[column];
        let displayName;
        
        // Check if it's a chat model
        const chatModels = [
          'CohereForAI/c4ai-command-r-plus-08-2024',
          'openai/gpt-4o-mini',
          'silma-ai/SILMA-9B-Instruct-v1.0',
          'microsoft/Phi-3.5-mini-instruct',
          'TURKCELL/Turkcell-LLM-7b-v1'
        ];
        
        if (chatModels.some(chatModel => modelName.includes(chatModel))) {
          displayName = `💬 ${modelName}`;
        } else {
          displayName = `🟢 ${modelName}`;
        }

        if (modelName.split("/")[0] !== "openai")
          displayName = `<a href="https://huggingface.co/${modelName}">${displayName}</a>`;
        td.innerHTML = displayName;
        td.title = modelName; // Add full model name as tooltip
        td.style.cursor = 'help'; // Change cursor to indicate hover functionality
      } else {
        const value = row[column];
        td.textContent = typeof value === 'number' ? value.toFixed(2) : value;
      }
    });
  });

  tableWrapper.appendChild(table);
  return tableWrapper;
}

export function initLeaderboardResults(containerId) {
  const container = document.getElementById(containerId);
  if (!container) return;

  const titleElement = document.createElement('h3');
  titleElement.textContent = 'FineTasks Leaderboard';
  titleElement.className = 'leaderboard-title';

  const tableContainer = document.createElement('div');
  tableContainer.className = 'table-container';
  
  const languageLabel = document.createElement('label');
  languageLabel.textContent = 'Language: ';
  const languageDropdown = createDropdown(Object.keys(languageMap), updateLanguageTable);

  const extraColumnLabel = document.createElement('label');
  extraColumnLabel.textContent = 'Task: ';
  const extraColumnDropdown = createDropdown(['None'], updateTable);

  let leaderboardDataTable;
  let currentData = [];

  // Create caption element
  const captionElement = document.createElement('figcaption');
  captionElement.className = 'table-caption';
  captionElement.textContent = container.dataset.caption || '';

  async function updateLanguageTable() {
    const selectedLanguage = languageDropdown.value;
    const csvFile = languageMap[selectedLanguage];

    try {
      const response = await fetch(`data/os_models/${csvFile}`);
      if (!response.ok) {
        throw new Error(`HTTP error! status: ${response.status}`);
      }
      const csvText = await response.text();
      const results = Papa.parse(csvText, { header: true, dynamicTyping: true }).data;
      currentData = selectedLanguage === 'All Languages'
        ? results.sort((a, b) => a.avg_rank_macro - b.avg_rank_macro)
        : results.sort((a, b) => b.agg_score_macro - a.agg_score_macro);

      // Update extra column dropdown options
      if (selectedLanguage !== 'All Languages') {
        const columnOptions = ['None'].concat(Object.keys(currentData[0]).filter(key => 
          !['runname', 'seed', 'steps', 'agg_score_micro', 'rank', 'avg_rank_macro', ''].includes(key)
        ));
        extraColumnDropdown.innerHTML = '';
        columnOptions.forEach(option => {
          const optionElement = document.createElement('option');
          optionElement.value = option;
          optionElement.textContent = option === 'None' ? 'None' : processTaskName(option);
          extraColumnDropdown.appendChild(optionElement);
        });
        
        extraColumnDropdown.value = 'None';
        extraColumnLabel.style.display = 'inline';
        extraColumnDropdown.style.display = 'inline';
      } else {
        extraColumnLabel.style.display = 'none';
        extraColumnDropdown.style.display = 'none';
      }

      updateTable();
    } catch (error) {
      console.error('Error fetching CSV:', error);
      tableContainer.innerHTML = `<p>Error loading data: ${error.message}</p>`;
    }
  }

  function updateTable() {
    const extraColumn = languageDropdown.value === 'All Languages' ? 'All Languages' : 
                        (extraColumnDropdown.value === 'None' ? null : extraColumnDropdown.value);
    
    tableContainer.innerHTML = '';
    const tableWrapper = createResultsTable(currentData, extraColumn);
    tableContainer.appendChild(tableWrapper);

    if (leaderboardDataTable) {
      leaderboardDataTable.destroy();
    }

    leaderboardDataTable = new DataTable('.leaderboard-results-table', {
      perPage: 10,
      perPageSelect: false,
      searchable: false,
      sortable: true,
      fixedHeight: true,
      labels: {
        info: ''  // This removes the "Showing 1 to X of Y entries" text
      }
    });

    // Adjust column widths after the table is created
    setTimeout(adjustColumnWidths, 0);
  }

  const controls = document.createElement('div');
  controls.className = 'controls leaderboard-controls fine-tasks-controls';

  const languageControlGroup = document.createElement('div');
  languageControlGroup.className = 'control-group';
  languageControlGroup.appendChild(languageLabel);
  languageControlGroup.appendChild(languageDropdown);

  const extraColumnControlGroup = document.createElement('div');
  extraColumnControlGroup.className = 'control-group';
  extraColumnControlGroup.appendChild(extraColumnLabel);
  extraColumnControlGroup.appendChild(extraColumnDropdown);

  controls.appendChild(languageControlGroup);
  controls.appendChild(extraColumnControlGroup);

  container.appendChild(titleElement);
  container.appendChild(tableContainer);
  container.appendChild(captionElement); // Add caption below the table
  container.appendChild(controls);

  // Initialize with All Languages data
  languageDropdown.value = 'All Languages';
  updateLanguageTable();
}

function adjustColumnWidths() {
  const table = document.querySelector('.leaderboard-results-table');
  if (!table) return;

  const columns = table.querySelectorAll('th');
  columns.forEach((column, index) => {
    const columnClass = column.className;
    const cells = table.querySelectorAll(`td.${columnClass}`);
    let maxWidth = column.offsetWidth;
    cells.forEach(cell => {
      maxWidth = Math.max(maxWidth, cell.offsetWidth);
    });

    let adjustedWidth;
    if (index === 0) { // Rank column
      adjustedWidth = 50;
    } else if (index === 1) { // Model name column
      adjustedWidth = 200;
    } else if (index === 2) { // Macro score column
      adjustedWidth = 100;
    } else { // Extra column or any other column
      adjustedWidth = Math.min(maxWidth, 150); // Set a maximum width of 150px for other columns
    }

    column.style.width = `${adjustedWidth}px`;
    cells.forEach(cell => {
      cell.style.width = `${adjustedWidth}px`;
    });
  });
}