TeleAI-AI-Flow's picture
Upload 32 files
309320b verified
// 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
}