Download web-src/src/standard.ts from sasakiy84/bibform: direct link, hf CLI and curl.
- Browser
- Download file 3.72 kB
-
https://huggingface.co/sasakiy84/bibform/resolve/main/web-src/src/standard.ts
- Command line
-
hf download hf://sasakiy84/bibform/web-src/src/standard.ts
-
curl -L -o standard.ts https://huggingface.co/sasakiy84/bibform/resolve/main/web-src/src/standard.ts
3.72 kB
| import type { Prediction } from './lite'; | |
| interface Span { | |
| start: number; | |
| end: number; | |
| value: string; | |
| } | |
| function isRecord(value: unknown): value is Record<string, unknown> { | |
| return typeof value === 'object' && value !== null && !Array.isArray(value); | |
| } | |
| function span(value: unknown): Span { | |
| if (!isRecord(value) || typeof value.start !== 'number' || typeof value.end !== 'number' || | |
| typeof value.value !== 'string' || !Number.isInteger(value.start) || !Number.isInteger(value.end) || | |
| value.start < 0 || value.end <= value.start) throw new Error('通常版の文字範囲が不正です'); | |
| return { start: value.start, end: value.end, value: value.value }; | |
| } | |
| function parsePrediction(json: string): Prediction { | |
| const raw: unknown = JSON.parse(json); | |
| if (!isRecord(raw) || !Array.isArray(raw.fields) || !Array.isArray(raw.contributors) || | |
| !Array.isArray(raw.candidates) || !raw.candidates.length) throw new Error('通常版の出力が不正です'); | |
| return { | |
| fields: raw.fields.map((field: unknown) => { | |
| if (!isRecord(field) || typeof field.label !== 'string') throw new Error('通常版の項目が不正です'); | |
| return { ...span(field), label: field.label }; | |
| }), | |
| contributors: raw.contributors.map((person: unknown) => { | |
| if (!isRecord(person) || (person.kind !== 'person' && person.kind !== 'organization') || | |
| !isRecord(person.name_parts) || !Array.isArray(person.roles)) throw new Error('通常版の責任表示が不正です'); | |
| const roles = person.roles.map((role: unknown) => { | |
| if (typeof role !== 'string') throw new Error('通常版の役割が不正です'); | |
| return role; | |
| }); | |
| const nameParts = new Map<string, Span>(); | |
| for (const [name, value] of Object.entries(person.name_parts)) nameParts.set(name, span(value)); | |
| return { ...span(person), kind: person.kind, roles, nameParts }; | |
| }), | |
| candidates: raw.candidates.map((entry: unknown) => { | |
| if (!isRecord(entry) || typeof entry.type !== 'string' || typeof entry.score !== 'number' || | |
| !Number.isFinite(entry.score) || entry.score < 0 || entry.score > 1.00001) { | |
| throw new Error('通常版の資料種別が不正です'); | |
| } | |
| return { type: entry.type, score: Math.min(entry.score, 1) }; | |
| }), | |
| }; | |
| } | |
| export class WorkerModel { | |
| private worker: Worker | undefined; | |
| constructor(private readonly script: URL, private readonly profile?: 'standard' | 'lite') {} | |
| predict(text: string): Promise<Prediction> { | |
| const worker = this.worker ??= new Worker(this.script, { type: 'module' }); | |
| return new Promise((resolve, reject) => { | |
| const listeners = new AbortController(); | |
| const finish = (): void => { | |
| listeners.abort(); | |
| clearTimeout(timer); | |
| }; | |
| const failed = (): void => { | |
| finish(); | |
| worker.terminate(); | |
| this.worker = undefined; | |
| reject(new Error('通常版の実行環境を起動できませんでした')); | |
| }; | |
| const timer = setTimeout(failed, 120_000); | |
| worker.addEventListener('error', failed, { signal: listeners.signal }); | |
| worker.addEventListener('message', (event: MessageEvent<unknown>) => { | |
| finish(); | |
| try { | |
| const payload = event.data; | |
| if (!isRecord(payload) || typeof payload.result !== 'string') throw new Error('通常版で解析できませんでした'); | |
| resolve(parsePrediction(payload.result)); | |
| } catch (error) { | |
| reject(error); | |
| } | |
| }, { signal: listeners.signal }); | |
| worker.postMessage(this.profile ? { profile: this.profile, text } : text); | |
| }); | |
| } | |
| } | |
| export { WorkerModel as StandardModel }; | |