Datasets:
Download code/evaluation-code/v0.2-0c2690ae/apps/console/web/src/BatchDesigner.tsx from VEHwang/cocogni4d: direct link, hf CLI and curl.
- Browser
- Download file 16.2 kB
-
https://huggingface.co/datasets/VEHwang/cocogni4d/resolve/main/code/evaluation-code/v0.2-0c2690ae/apps/console/web/src/BatchDesigner.tsx
- Command line
-
hf download hf://datasets/VEHwang/cocogni4d/code/evaluation-code/v0.2-0c2690ae/apps/console/web/src/BatchDesigner.tsx
-
curl -L -o BatchDesigner.tsx https://huggingface.co/datasets/VEHwang/cocogni4d/resolve/main/code/evaluation-code/v0.2-0c2690ae/apps/console/web/src/BatchDesigner.tsx
16.2 kB
| import {useEffect, useRef, useState} from 'react'; | |
| import {postExpand} from './api.ts'; | |
| import {DistInput} from './DistInput.tsx'; | |
| import {observerBodies, unimplementedMotion, QUOTA_FIELDS, changeQuotaType, setOptional, batchTotal, castKinds, configRow, label, unfilledHints, setSceneCount} from './np-batch.ts'; | |
| import type {BatchSettings, Dist, ExpandedConfig, ExpandResult, GenSchema, Group, Quota} from './np-batch.ts'; | |
| import './batch.css'; | |
| // 不填就按生成器默认;填了才写进配置 | |
| function OptionalDist({name, value, onChange, kind = 'number', options, initial = 0.5}: {name: string; value: Dist | undefined; onChange: (value: Dist | undefined) => void; kind?: 'number' | 'enum'; options?: string[]; initial?: Dist}) { | |
| return <fieldset><legend>{name}</legend>{value === undefined | |
| ? <><span className="hint">按生成器默认</span> <button type="button" disabled={kind === 'enum' && !options?.length} onClick={() => onChange(initial)}>自定</button></> | |
| : <><DistInput kind={kind} options={options} value={value} onChange={onChange}/> <button type="button" onClick={() => onChange(undefined)}>用默认</button></>}</fieldset>; | |
| } | |
| function QuotaDetails({schema, quota, onChange}: {schema: GenSchema; quota: Quota; onChange: (quota: Quota) => void}) { | |
| const num = (key: 'dwell_s', step: string) => <label key={key}>{label(key)} <input type="number" min="0" step={step} | |
| placeholder="默认" value={quota[key] ?? ''} onChange={e => onChange(setOptional(quota, key, e.target.value === '' ? undefined : Number(e.target.value)))}/></label>; | |
| const fields = schema.arrangement_fields[quota.type] || {}; | |
| const invalid = Object.entries(quota).filter(([key, value]) => { | |
| if (['type', 'count', 'strength', 'when', 'decoy'].includes(key)) return false; | |
| const field = fields[key]; | |
| return !field || (field.type === 'enum' && !field.values!.includes(String(value))) || (field.type === 'fixed' && value !== field.value); | |
| }); | |
| const when = Array.isArray(quota.when) ? quota.when : null; | |
| const setWhen = (i: 0 | 1, value: number) => onChange({...quota, when: (i ? [when![0], value] : [value, when![1]]) as [number, number]}); | |
| return <details><summary>{Object.keys(quota).filter(k => !['type', 'count', 'strength'].includes(k)).map(label).join('、') || '按默认'}</summary> | |
| {!schema.arrangements[quota.type] && <p role="alert">type: {quota.type} 不可用</p>} | |
| {invalid.map(([key, value]) => <p role="alert" key={key}>{key}: {JSON.stringify(value)} 不符合当前安排约束</p>)} | |
| {(QUOTA_FIELDS[quota.type] || []).map(key => {const field = fields[key]; return key === 'where' | |
| ? <label key={key}>{label(key)} <input placeholder="auto" value={quota.where ?? ''} onChange={e => onChange(setOptional(quota, 'where', | |
| e.target.value === '' ? undefined : /^\d+$/.test(e.target.value) ? Number(e.target.value) : e.target.value))}/></label> | |
| : field?.type === 'enum' ? <label key={key}>{label(key)} <select value={quota.event ?? ''} onChange={e => onChange(setOptional(quota, 'event', e.target.value === '' ? undefined : 'dynamic'))}><option value="">默认</option>{field.values!.map(v => <option key={v} value={v}>{v}</option>)}</select></label> | |
| : field?.type === 'fixed' ? <span key={key}>{label(key)}:{field.value}</span> : num(key as 'dwell_s', 'any');})} | |
| <label><input type="checkbox" checked={!!when} onChange={e => onChange(setOptional(quota, 'when', e.target.checked ? [0, 30] as [number, number] : undefined))}/>{label('when')}</label> | |
| {when && <>从 <input aria-label="时段起点" type="number" min="0" step="any" value={when[0]} onChange={e => setWhen(0, Number(e.target.value))}/> 到 <input aria-label="时段终点" type="number" min="0" step="any" value={when[1]} onChange={e => setWhen(1, Number(e.target.value))}/> s</>} | |
| <label><input type="checkbox" checked={!!quota.decoy} onChange={e => onChange(setOptional(quota, 'decoy', e.target.checked || undefined))}/>{label('decoy')}</label> | |
| </details>; | |
| } | |
| export function BatchDesigner({schema, batch, onChange, segmentsNeeded, onSelect}: { | |
| schema: GenSchema; batch: BatchSettings; onChange: (batch: BatchSettings) => void; segmentsNeeded: number | null; | |
| onSelect?: (row: ExpandedConfig | null) => void; | |
| }) { | |
| const [result, setResult] = useState<{value: ExpandResult; settings: string} | null>(null); | |
| const [selected, setSelected] = useState<number | null>(null), [busy, setBusy] = useState(false), [error, setError] = useState(''); | |
| const request = useRef<AbortController | null>(null); | |
| useEffect(() => () => request.current?.abort(), []); | |
| const total = batchTotal(batch), fingerprint = JSON.stringify(batch), stale = !!result && result.settings !== fingerprint; | |
| function groupChange(index: number, group: Group) { | |
| onChange({...batch, template: {groups: batch.template.groups.map((old, i) => i === index ? group : old)}}); | |
| } | |
| async function expand() { | |
| if (request.current) return; | |
| const controller = new AbortController(); request.current = controller; setBusy(true); setError(''); setSelected(null); onSelect?.(null); | |
| try { | |
| const value = await postExpand(batch, controller.signal); | |
| if (!controller.signal.aborted) setResult({value, settings: fingerprint}); | |
| } catch (e) {if (!controller.signal.aborted) setError(e instanceof Error ? e.message : String(e));} | |
| finally {if (!controller.signal.aborted) {setBusy(false); request.current = null;}} | |
| } | |
| function download() { | |
| const url = URL.createObjectURL(new Blob([JSON.stringify(batch, null, 2) + '\n'], {type: 'application/json'})); | |
| const link = document.createElement('a'); link.href = url; link.download = 'batch-settings.json'; link.click(); URL.revokeObjectURL(url); | |
| } | |
| const kinds = castKinds(schema); | |
| return <div className="batch-designer"> | |
| <p className={segmentsNeeded !== null && total < segmentsNeeded ? 'batch-short' : ''}>这批共 {total} 段(各场景段数 × 组数){segmentsNeeded !== null && <> · 题目配额估算需 {segmentsNeeded} 段{total < segmentsNeeded && <strong> · 段数不够</strong>}</>}</p> | |
| <small className="batch-muted">生成器版本 {schema.generator_revision}</small> | |
| <label>随机种子(同设定同种子,展开结果相同) <input type="number" min="0" step="1" value={batch.seed} onChange={e => onChange({...batch, seed: Number(e.target.value)})}/></label> | |
| <div className="batch-table"><table><caption>场景段数</caption><thead><tr><th>场景</th><th>每组段数</th></tr></thead><tbody>{batch.scenes.map(scene => <tr key={scene.scene}><td>{scene.scene}</td><td><input aria-label={scene.scene + ' 段数'} type="number" min="1" step="1" value={scene.count} onChange={e => onChange(setSceneCount(batch, scene.scene, Number(e.target.value)))}/></td></tr>)}</tbody></table></div> | |
| {!batch.scenes.length && <p>请在上面的场景卡中选择场景。</p>} | |
| {batch.template.groups.map((group, index) => <fieldset className="batch-group" key={index}><legend>组 {index + 1}</legend> | |
| <button type="button" disabled={batch.template.groups.length <= 1} onClick={() => onChange({...batch, template: {groups: batch.template.groups.filter((_, i) => i !== index)}})}>删除这组</button> | |
| <fieldset><legend>{label('duration')}</legend><DistInput kind="number" unit="s" value={group.duration} onChange={duration => groupChange(index, {...group, duration})}/></fieldset> | |
| <fieldset><legend>{label('cast_mix')}</legend>{kinds.map(kind => <fieldset key={kind}><legend>{label(kind)}</legend><DistInput kind="number" value={group.cast_mix[kind] ?? 0} onChange={value => groupChange(index, {...group, cast_mix: {...group.cast_mix, [kind]: value}})}/></fieldset>)}</fieldset> | |
| <fieldset><legend>{label('profile_mix')}</legend>{kinds.map(kind => { | |
| const scriptKind = String(schema.bodies[kind]?.script_kind ?? kind); | |
| const value = group.profile_mix?.[kind] ?? schema.default_profile[scriptKind]; | |
| return <fieldset key={kind}><legend>{label(kind)}</legend>{value === undefined && <p>按生成器默认</p>}<DistInput kind="enum" options={Object.keys(schema.profiles)} value={value ?? {dist: 'choice', values: []}} onChange={profile => groupChange(index, {...group, profile_mix: {...group.profile_mix, [kind]: profile}})}/><button type="button" disabled={group.profile_mix?.[kind] === undefined} onClick={() => {const profile_mix = {...group.profile_mix}; delete profile_mix[kind]; groupChange(index, {...group, profile_mix});}}>按生成器默认</button></fieldset>; | |
| })}</fieldset> | |
| <fieldset><legend>{label('observer_mix')}</legend><fieldset><legend>数量</legend><DistInput kind="number" value={group.observer_mix.count} onChange={count => groupChange(index, {...group, observer_mix: {...group.observer_mix, count}})}/></fieldset> | |
| <fieldset><legend>种类</legend><DistInput kind="enum" options={Object.keys(schema.observer_kinds)} value={group.observer_mix.kind} onChange={kind => groupChange(index, {...group, observer_mix: setOptional({...group.observer_mix, kind}, 'body', undefined)})}/></fieldset> | |
| <fieldset><legend>移动方式</legend><DistInput kind="enum" options={schema.motion_modes} disabledOptions={unimplementedMotion(schema)} value={group.observer_mix.motion_mode} onChange={motion_mode => groupChange(index, {...group, observer_mix: {...group.observer_mix, motion_mode}})}/></fieldset> | |
| <fieldset><legend>视角</legend>{group.observer_mix.view === undefined && <p>按种类默认(实体朝前进方向,虚拟自动)</p>}<DistInput kind="enum" options={schema.view_modes} value={group.observer_mix.view ?? {dist: 'choice', values: []}} onChange={view => groupChange(index, {...group, observer_mix: {...group.observer_mix, view}})}/><button type="button" disabled={group.observer_mix.view === undefined} onClick={() => {const observer_mix = {...group.observer_mix}; delete observer_mix.view; groupChange(index, {...group, observer_mix});}}>按种类默认</button></fieldset> | |
| {(() => {const om = group.observer_mix, bodies = observerBodies(schema, om.kind), set = <K extends keyof typeof om>(key: K, value: (typeof om)[K] | undefined) => groupChange(index, {...group, observer_mix: setOptional(om, key, value)}); | |
| return <> | |
| <OptionalDist name={label('body') + (bodies.length ? '' : '(种类混抽时按种类默认)')} kind="enum" options={bodies} initial={bodies[0]} value={bodies.length ? om.body : undefined} onChange={v => set('body', v)}/> | |
| <OptionalDist name={label('profile')} kind="enum" options={Object.keys(schema.profiles)} initial={schema.default_profile.observer ?? Object.keys(schema.profiles)[0]} value={om.profile} onChange={v => set('profile', v)}/> | |
| <OptionalDist name={label('eye_height_m') + (bodies.length ? '' : '(种类混抽时各种类用同一个值)')} initial={Number(schema.bodies[bodies[0]]?.eye_height_m ?? 1.5)} value={om.eye_height_m} onChange={v => set('eye_height_m', v)}/> | |
| <fieldset><legend>{label('calibration')}</legend><select aria-label={label('calibration')} value={om.calibration === undefined ? '' : String(om.calibration)} | |
| onChange={e => set('calibration', e.target.value === '' ? undefined : e.target.value === 'true')}><option value="">按生成器默认(停)</option><option value="true">停</option><option value="false">不停</option></select></fieldset> | |
| </>;})()} | |
| </fieldset> | |
| <fieldset><legend>{label('flavor_mix')}</legend><DistInput kind="enum" options={schema.flavors} disabledOptions={schema.flavors.filter(f => !schema.implemented_flavors.includes(f))} value={group.flavor_mix} onChange={flavor_mix => groupChange(index, {...group, flavor_mix})}/></fieldset> | |
| <fieldset><legend>{label('recording')}</legend>{schema.recording_keys.map(key => <span key={key}><OptionalDist name={label(key)} value={group.recording?.[key]} | |
| onChange={value => {const recording = setOptional(group.recording || {}, key, value); | |
| groupChange(index, setOptional(group, 'recording', Object.keys(recording).length ? recording : undefined));}}/></span>)}</fieldset> | |
| <fieldset><legend>{label('arrangement_quota')}</legend><p className="hint" data-testid="unique-appearance">同类外观可区分:生成器保证,不用安排——同类主体的公开外观各不相同(gen/appearance.py require_unique_bodies)。</p><div className="batch-table"><table><thead><tr><th>类型</th><th>次数</th><th>强度</th><th>细节</th><th>操作</th></tr></thead><tbody>{group.arrangement_quota.map((arr, ai) => { | |
| const update = (next: typeof arr) => groupChange(index, {...group, arrangement_quota: group.arrangement_quota.map((old, i) => ai === i ? next : old)}); | |
| return <tr key={ai}><td><select aria-label="安排类型" value={arr.type} onChange={e => update(changeQuotaType(arr, e.target.value))}>{!(arr.type in schema.arrangements) && <option value={arr.type}>{arr.type}(不可用)</option>}{Object.keys(schema.arrangements).map(type => <option key={type} value={type}>{label(type)}</option>)}</select></td><td><DistInput kind="number" value={arr.count} onChange={count => update({...arr, count})}/></td><td><select aria-label="安排强度" value={arr.strength} onChange={e => update({...arr, strength: e.target.value as 'hard' | 'soft'})}><option value="hard">必须</option><option value="soft">尽量</option></select></td><td><QuotaDetails schema={schema} quota={arr} onChange={update}/></td><td><button type="button" onClick={() => groupChange(index, {...group, arrangement_quota: group.arrangement_quota.filter((_, i) => ai !== i)})}>删除安排</button></td></tr>; | |
| })}</tbody></table></div><button type="button" disabled={!Object.keys(schema.arrangements).length} onClick={() => groupChange(index, {...group, arrangement_quota: [...group.arrangement_quota, {type: Object.keys(schema.arrangements)[0], count: 1, strength: 'hard'}]})}>+ 加安排</button></fieldset> | |
| <label>{label('decoy_rate')} <input type="number" min="0" max="1" step="any" value={group.decoy_rate} onChange={e => groupChange(index, {...group, decoy_rate: Number(e.target.value)})}/></label> | |
| </fieldset>)} | |
| <div className="btns"><button type="button" onClick={() => onChange({...batch, template: {groups: [...batch.template.groups, structuredClone(batch.template.groups[batch.template.groups.length - 1])]}})}>+ 加一组(复制上一组)</button></div> | |
| <div className="btns"><button type="button" disabled={busy || !total || total > 500} onClick={() => void expand()}>{busy ? '正在展开…' : '展开成单段配置'}</button><button type="button" onClick={download}>保存批量设定 JSON</button></div> | |
| {total > 500 && <p role="alert" className="batch-short">每批最多展开 500 段,请减少段数。</p>} | |
| {error && <p role="alert" className="batch-error">{error}</p>} | |
| {result && <><p role="status">{stale ? '设定已改,重新展开' : `已展开 ${result.value.count} 段`} · 生成器版本 {result.value.generator_revision}</p><div className={'batch-table' + (stale ? ' batch-stale' : '')}><table><thead><tr>{['#', '场景', '时长', '主体', '观察者', '情形安排', '问题'].map(title => <th key={title}>{title}</th>)}</tr></thead><tbody>{result.value.configs.map((entry, i) => { | |
| const row = configRow(entry.config); | |
| return <tr key={i} className={[entry.errors.length ? 'batch-invalid' : '', selected === i ? 'batch-selected' : ''].join(' ')} onClick={() => {if (!stale) {setSelected(i); onSelect?.(entry);}}}><td><button type="button" disabled={stale} aria-label={`查看第 ${i + 1} 段配置`} onClick={e => {e.stopPropagation(); setSelected(i); onSelect?.(entry);}}>{i + 1}</button></td><td>{row.scene}</td><td>{row.duration} s</td><td>{row.cast}</td><td>{row.observers}</td><td>{row.arrangements}</td><td>{entry.errors.join(';') || (unfilledHints(entry.config).length ? '' : '无')}{unfilledHints(entry.config).map((h, j) => <small key={j} className="batch-muted" title={h.reason}>{h.text}</small>)}</td></tr>; | |
| })}</tbody></table></div></>} | |
| </div>; | |
| } | |