Download server/static/common.js from raianand/SCQC: direct link, hf CLI and curl.
- Browser
- Download file 5.84 kB
-
https://huggingface.co/spaces/raianand/SCQC/resolve/main/server/static/common.js
- Command line
-
hf download hf://spaces/raianand/SCQC/server/static/common.js
-
curl -L -o common.js https://huggingface.co/spaces/raianand/SCQC/resolve/main/server/static/common.js
5.84 kB
| // Shared helpers for the SCQC pages. No build step, no framework. | |
| export const LANG_NAMES = { | |
| as: "Assamese (অসমীয়া)", bn: "Bengali (বাংলা)", brx: "Bodo (बड़ो)", doi: "Dogri (डोगरी)", | |
| gu: "Gujarati (ગુજરાતી)", hi: "Hindi (हिन्दी)", kn: "Kannada (ಕನ್ನಡ)", ks: "Kashmiri (کٲشُر)", | |
| kok: "Konkani (कोंकणी)", mai: "Maithili (मैथिली)", ml: "Malayalam (മലയാളം)", mni: "Manipuri (ꯃꯩꯇꯩꯂꯣꯟ)", | |
| mr: "Marathi (मराठी)", ne: "Nepali (नेपाली)", or: "Odia (ଓଡ଼ିଆ)", pa: "Punjabi (ਪੰਜਾਬੀ)", | |
| sa: "Sanskrit (संस्कृतम्)", sat: "Santali (ᱥᱟᱱᱛᱟᱲᱤ)", sd: "Sindhi (سنڌي)", ta: "Tamil (தமிழ்)", | |
| te: "Telugu (తెలుగు)", ur: "Urdu (اردو)", en: "English", | |
| }; | |
| export const $ = (sel, root = document) => root.querySelector(sel); | |
| export const el = (tag, attrs = {}, ...kids) => { | |
| const n = document.createElement(tag); | |
| for (const [k, v] of Object.entries(attrs)) { | |
| if (v == null || v === false) continue; | |
| if (k === "class") n.className = v; | |
| else if (k.startsWith("on")) n.addEventListener(k.slice(2), v); | |
| else if (k === "text") n.textContent = v; | |
| else n.setAttribute(k, v === true ? "" : v); | |
| } | |
| for (const kid of kids.flat()) if (kid != null) n.append(kid.nodeType ? kid : document.createTextNode(kid)); | |
| return n; | |
| }; | |
| export class ApiError extends Error { | |
| constructor(status, body) { | |
| super(typeof body?.detail === "string" ? body.detail : `HTTP ${status}`); | |
| this.status = status; | |
| this.body = body; | |
| } | |
| } | |
| export async function api(path, { method = "GET", body, token, form, raw } = {}) { | |
| const headers = {}; | |
| if (token) headers["X-SCQC-Token"] = token; | |
| let payload; | |
| if (form) payload = form; | |
| else if (body !== undefined) { headers["Content-Type"] = "application/json"; payload = JSON.stringify(body); } | |
| const res = await fetch(path, { method, headers, body: payload }); | |
| if (raw) { | |
| if (!res.ok) throw new ApiError(res.status, await res.json().catch(() => ({}))); | |
| return res; | |
| } | |
| const data = await res.json().catch(() => ({})); | |
| if (!res.ok) throw new ApiError(res.status, data); | |
| return data; | |
| } | |
| let toastTimer; | |
| export function toast(msg, ms = 3200) { | |
| let t = $("#toast"); | |
| if (!t) { t = el("div", { id: "toast", class: "toast", role: "status", "aria-live": "polite" }); document.body.append(t); } | |
| t.textContent = msg; | |
| t.classList.remove("hidden"); | |
| clearTimeout(toastTimer); | |
| toastTimer = setTimeout(() => t.classList.add("hidden"), ms); | |
| } | |
| export const store = { | |
| get(k, d = null) { try { const v = localStorage.getItem("scqc." + k); return v == null ? d : JSON.parse(v); } catch { return d; } }, | |
| set(k, v) { try { localStorage.setItem("scqc." + k, JSON.stringify(v)); } catch { /* storage full or blocked */ } }, | |
| del(k) { try { localStorage.removeItem("scqc." + k); } catch { /* ignore */ } }, | |
| }; | |
| // ---------------------------------------------------------------- offline upload queue (IndexedDB) | |
| // Recordings are written here first and uploaded when the network allows. | |
| // The server deduplicates by client_upload_id, so re-sending after a dropped | |
| // connection is safe. | |
| const DB_NAME = "scqc-queue", STORE = "uploads"; | |
| function openDb() { | |
| return new Promise((resolve, reject) => { | |
| const req = indexedDB.open(DB_NAME, 1); | |
| req.onupgradeneeded = () => req.result.createObjectStore(STORE, { keyPath: "client_upload_id" }); | |
| req.onsuccess = () => resolve(req.result); | |
| req.onerror = () => reject(req.error); | |
| }); | |
| } | |
| async function withStore(mode, fn) { | |
| const db = await openDb(); | |
| return new Promise((resolve, reject) => { | |
| const tx = db.transaction(STORE, mode); | |
| const out = fn(tx.objectStore(STORE)); | |
| tx.oncomplete = () => resolve(out?.result ?? out); | |
| tx.onerror = () => reject(tx.error); | |
| }); | |
| } | |
| export const queue = { | |
| add: (item) => withStore("readwrite", (s) => s.put(item)), | |
| remove: (id) => withStore("readwrite", (s) => s.delete(id)), | |
| all: () => withStore("readonly", (s) => s.getAll()), | |
| count: () => withStore("readonly", (s) => s.count()), | |
| }; | |
| let syncing = false; | |
| /** Upload everything waiting. Calls onResult(item, response) per success. Stops at the first network error. */ | |
| export async function syncQueue(onResult) { | |
| if (syncing) return { sent: 0, stopped: false }; | |
| syncing = true; | |
| let sent = 0; | |
| try { | |
| const items = (await queue.all()).sort((a, b) => a.created - b.created); | |
| for (const it of items) { | |
| const form = new FormData(); | |
| for (const k of ["project_id", "task_id", "speaker_id", "client_upload_id"]) form.append(k, it[k]); | |
| form.append("client_meta", JSON.stringify(it.client_meta || {})); | |
| form.append("file", it.blob, `${it.client_upload_id}.wav`); | |
| let res; | |
| try { | |
| res = await api("/api/recordings", { method: "POST", form }); | |
| } catch (e) { | |
| if (e instanceof ApiError && e.status >= 400 && e.status < 500) { | |
| // The server refused it (e.g. consent withdrawn); keeping it would block the queue forever. | |
| await queue.remove(it.client_upload_id); | |
| onResult?.(it, null, e); | |
| continue; | |
| } | |
| return { sent, stopped: true }; | |
| } | |
| await queue.remove(it.client_upload_id); | |
| sent++; | |
| onResult?.(it, res, null); | |
| } | |
| return { sent, stopped: false }; | |
| } finally { | |
| syncing = false; | |
| } | |
| } | |
| export function uuid() { | |
| if (crypto.randomUUID) return crypto.randomUUID(); | |
| const b = crypto.getRandomValues(new Uint8Array(16)); | |
| return [...b].map((x) => x.toString(16).padStart(2, "0")).join(""); | |
| } | |
| export function fmtTime(s) { | |
| const m = Math.floor(s / 60), r = Math.floor(s % 60); | |
| return `${m}:${String(r).padStart(2, "0")}`; | |
| } | |