File size: 1,938 Bytes
309320b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Lightweight CSV parsing helpers used by the app.
// Exports:
// - parseCSVLine(line): parse a single CSV line into array of values (handles quoted fields)
// - parseCsvContent(text): parse CSV text into { headers: string[], rows: Array<object> }
// - processModelNames(rows): modify model_name and model_series in rows

export function parseCSVLine(line) {
  const res = []
  let cur = ''
  let inQuotes = false
  for (let i = 0; i < line.length; i++) {
    const ch = line[i]
    if (inQuotes) {
      if (ch === '"') {
        if (i + 1 < line.length && line[i + 1] === '"') { cur += '"'; i++ } else { inQuotes = false }
      } else { cur += ch }
    } else {
      if (ch === ',') { res.push(cur); cur = '' }
      else if (ch === '"') { inQuotes = true }
      else { cur += ch }
    }
  }
  res.push(cur)
  return res
}

export function parseCsvContent(txt) {
  const lines = txt.split(/\r?\n/)
  let headerIdx = null
  for (let i = 0; i < lines.length; i++) if (lines[i].trim().length > 0) { headerIdx = i; break }
  if (headerIdx === null) return { headers: [], rows: [] }
  const headers = parseCSVLine(lines[headerIdx])
  const rows = []
  for (let i = headerIdx + 1; i < lines.length; i++) {
    const l = lines[i]
    if (!l || l.trim() === '') continue
    const vals = parseCSVLine(l)
    const obj = {}
    for (let j = 0; j < headers.length; j++) obj[headers[j]] = vals[j] ?? ''
    rows.push(obj)
  }
  return { headers, rows }
}

export function processModelNames(rows) {
  for (const row of rows) {
    if (row.model_name) {
      row.model_name = modifyString(row.model_name)
    }
    if (row.model_series) {
      row.model_series = modifyString(row.model_series)
    }
  }
}

function modifyString(str) {
  str = str.replace(/glm/gi, 'GLM')
  str = str.replace(/gemma/gi, 'Gemma')
  str = str.replace(/Internlm/gi, 'InternLM')
  str = str.replace(/b/gi, 'B')
  str = str.replace(/-hf/gi, '-Base')
  return str
}