Datasets:
Download code/evaluation-code/ent400-a06a1576/apps/console/web/src/DistInput.tsx from VEHwang/cocogni4d: direct link, hf CLI and curl.
- Browser
- Download file 5.94 kB
-
https://huggingface.co/datasets/VEHwang/cocogni4d/resolve/main/code/evaluation-code/ent400-a06a1576/apps/console/web/src/DistInput.tsx
- Command line
-
hf download hf://datasets/VEHwang/cocogni4d/code/evaluation-code/ent400-a06a1576/apps/console/web/src/DistInput.tsx
-
curl -L -o DistInput.tsx https://huggingface.co/datasets/VEHwang/cocogni4d/resolve/main/code/evaluation-code/ent400-a06a1576/apps/console/web/src/DistInput.tsx
5.94 kB
| import {useState} from 'react'; | |
| import {distSummary, label} from './np-batch.ts'; | |
| import type {Dist} from './np-batch.ts'; | |
| export function DistInput({value, onChange, kind, options = [], disabledOptions = [], unit}: { | |
| value: Dist; onChange: (value: Dist) => void; kind: 'number' | 'enum'; options?: string[]; disabledOptions?: string[]; unit?: string; | |
| }) { | |
| const [json, setJson] = useState<string | null>(null), [error, setError] = useState(''), [open, setOpen] = useState(false); | |
| const summary = <small className="batch-muted">{distSummary(value, unit)}</small>; | |
| if (kind === 'enum') { | |
| const values = typeof value === 'string' ? [value] : typeof value === 'object' && value.dist === 'fixed' ? [value.value] : typeof value === 'object' && value.dist === 'choice' ? value.values : []; | |
| const weights = typeof value === 'object' && value.dist === 'choice' ? value.weights : undefined; | |
| const commit = (next: (string | number)[], nextWeights: number[]) => {if (next.length) onChange(next.length === 1 ? {dist: 'fixed', value: String(next[0])} : {dist: 'choice', values: next, weights: nextWeights});}; | |
| return <div className="dist-input"><div className="dist-options">{options.map(option => { | |
| const index = values.indexOf(option), disabled = disabledOptions.includes(option); | |
| return <div className="dist-option" key={option}><label><input type="checkbox" checked={index >= 0} disabled={disabled || index >= 0 && values.length === 1} onChange={e => { | |
| const currentWeights = values.map((_, i) => weights?.[i] ?? 1); | |
| commit(e.target.checked ? [...values, option] : values.filter(v => v !== option), e.target.checked ? [...currentWeights, 1] : currentWeights.filter((_, i) => i !== index)); | |
| }}/>{label(option)}</label>{index >= 0 && <label>权重 <input aria-label={label(option) + ' 权重'} type="number" min="0" step="any" disabled={disabled || values.length === 1} value={weights?.[index] ?? 1} onChange={e => commit(values, values.map((_, i) => i === index ? Number(e.target.value) : weights?.[i] ?? 1))}/></label>}{disabled && <small>生成器还不支持</small>}</div>; | |
| })}</div>{summary}</div>; | |
| } | |
| const dist = typeof value === 'object' ? value : {dist: 'fixed' as const, value}; | |
| // 数值分布默认只显示一行摘要,点“改”才展开编辑框,避免堆砌文本框 | |
| if (!open) return <div className="dist-input dist-closed"><span className="dist-summary">{distSummary(value, unit)}</span><button type="button" className="dist-edit" onClick={() => setOpen(true)}>改</button></div>; | |
| const numeric = (title: string, n: number | string | undefined, change: (n: number | undefined) => void, optional = false) => <label>{title}<input type="number" aria-label={title} step="any" value={n ?? ''} onChange={e => change(optional && e.target.value === '' ? undefined : Number(e.target.value))}/></label>; | |
| const defaults: Record<string, Dist> = {fixed: 1, uniform: {dist: 'uniform', lo: 0, hi: 1}, normal: {dist: 'normal', mean: 1, sd: .2}, lognormal: {dist: 'lognormal', median: 1, sigma: .2}, choice: {dist: 'choice', values: [1, 2]}}; | |
| return <div className="dist-input"><div className="dist-fields"><label>分布 <select aria-label="分布" value={dist.dist} onChange={e => {onChange(defaults[e.target.value]); setJson(null); setError('');}}> | |
| <option value="fixed">固定</option><option value="uniform">均匀</option><option value="normal">正态</option><option value="lognormal">对数正态</option><option value="choice">选一个</option>{dist.dist === 'mixture' && <option value="mixture">混合</option>}</select></label> | |
| {dist.dist === 'fixed' && numeric('数值', dist.value, n => onChange(n ?? 0))} | |
| {dist.dist === 'uniform' && <>{numeric('下限', dist.lo, n => onChange({...dist, lo: n ?? 0}))}{numeric('上限', dist.hi, n => onChange({...dist, hi: n ?? 0}))}</>} | |
| {dist.dist === 'normal' && <>{numeric('均值', dist.mean, n => onChange({...dist, mean: n ?? 0}))}{numeric('标准差', dist.sd, n => onChange({...dist, sd: n ?? 0}))}</>} | |
| {dist.dist === 'lognormal' && <>{numeric('中位数', dist.median, n => onChange({...dist, median: n ?? 0}))}{numeric('sigma', dist.sigma, n => onChange({...dist, sigma: n ?? 0}))}</>} | |
| {(dist.dist === 'normal' || dist.dist === 'lognormal') && <>{numeric('下限(可空)', dist.lo, n => onChange({...dist, lo: n}), true)}{numeric('上限(可空)', dist.hi, n => onChange({...dist, hi: n}), true)}</>} | |
| {dist.dist === 'choice' && <div>{dist.values.map((v, i) => <div className="dist-fields" key={i}>{numeric(`选项 ${i + 1}`, v, n => onChange({...dist, values: dist.values.map((x, j) => i === j ? n ?? 0 : x)}))}{numeric(`权重 ${i + 1}`, dist.weights?.[i] ?? 1, n => onChange({...dist, weights: dist.values.map((_, j) => j === i ? n ?? 0 : dist.weights?.[j] ?? 1)}))}<button type="button" disabled={dist.values.length === 1} onClick={() => onChange({...dist, values: dist.values.filter((_, j) => i !== j), ...(dist.weights ? {weights: dist.weights.filter((_, j) => i !== j)} : {})})}>删除选项</button></div>)}<button type="button" onClick={() => onChange({...dist, values: [...dist.values, 0], ...(dist.weights ? {weights: [...dist.weights, 1]} : {})})}>+ 加选项</button></div>} | |
| <button type="button" onClick={() => setOpen(false)}>收起</button></div>{summary}{dist.dist === 'mixture' && <label>编辑 JSON<textarea rows={5} value={json ?? JSON.stringify(value, null, 2)} onChange={e => {setJson(e.target.value); try { | |
| const next = JSON.parse(e.target.value); | |
| if (!next || next.dist !== 'mixture' || !Array.isArray(next.of) || !next.of.length) throw Error('需要非空的 mixture.of 数组'); | |
| // The generator validates the distributions when expanding. | |
| distSummary(next); onChange(next); setError(''); | |
| } catch (e) {setError(e instanceof Error ? e.message : String(e));}}}/>{error && <small role="alert">{error}</small>}</label>}</div>; | |
| } | |