File size: 14,007 Bytes
444d92d ad9612f 444d92d ad9612f 444d92d ad9612f 444d92d ad9612f 444d92d ad9612f 444d92d ad9612f 444d92d ad9612f 444d92d ad9612f 444d92d ad9612f 444d92d ad9612f 444d92d ad9612f 444d92d adde4cd 444d92d ad9612f 444d92d ad9612f 444d92d ad9612f 444d92d ad9612f 444d92d ad9612f 444d92d ad9612f 444d92d ad9612f 444d92d ad9612f 444d92d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
import { useEffect, useState } from 'react';
import { Bot, Settings2, Lock } from 'lucide-react';
import ModelValidator from '../components/validators/ModelValidator';
import { LM_MODELS } from '../constants/models';
import type { JobConfig } from '../types';
type Extras = {
datasetLimit: number;
};
type Props = {
onRun: (cfg: JobConfig, extras: Extras) => void;
};
export default function ModelConfigPage({ onRun }: Props) {
// 從 localStorage 載入 Dataset 頁的草稿
const [cfg, setCfg] = useState<JobConfig>(() => {
try {
const draft = localStorage.getItem('cfgDraft');
if (draft) return JSON.parse(draft);
} catch {}
// fallback 預設
return {
dataset: '',
languageModel: '',
scorerModel: '',
k: 5,
numCounterfactuals: 3,
metrictarget: 0.5,
tau: 0.1,
iterations: 1000,
seed: 42,
enableFineTuning: false,
counterfactual: false,
};
});
const [datasetLimit, setDatasetLimit] = useState<number>(() => {
try {
const extrasDraft = JSON.parse(localStorage.getItem('extrasDraft') || '{}');
return typeof extrasDraft.datasetLimit === 'number' ? extrasDraft.datasetLimit : 10;
} catch {
return 10;
}
});
const [customLM, setCustomLM] = useState('');
const [showCustomLanguageInput, setShowCustomLanguageInput] = useState(false);
const [classificationTask, setClassificationTask] = useState<
'sentiment' | 'regard' | 'stereotype' | 'personality' | 'toxicity'
>('sentiment');
const [toxicityModelChoice, setToxicityModelChoice] = useState<'detoxify' | 'junglelee'>('detoxify');
const setField = <K extends keyof JobConfig>(k: K, v: JobConfig[K]) =>
setCfg((prev) => ({ ...prev, [k]: v }));
// 若有草稿中的 model 設定也載回
useEffect(() => {
try {
const draft = localStorage.getItem('cfgDraft');
if (!draft) return;
const parsed = JSON.parse(draft);
setClassificationTask(parsed.classificationTask ?? 'sentiment');
setToxicityModelChoice(parsed.toxicityModelChoice ?? 'detoxify');
if (parsed.languageModel) setField('languageModel', parsed.languageModel);
if (parsed.k) setField('k', parsed.k);
if (typeof parsed.metrictarget === 'number') setField('metrictarget', parsed.metrictarget);
} catch {}
}, []);
// 🔒 Example 鎖死規則:偵測到 example 就強制設定並關閉自定義輸入
const isExample = cfg.dataset === 'example';
useEffect(() => {
if (!isExample) return;
setField('languageModel', 'openai-community/gpt2'); // 後端預設也用這個
setShowCustomLanguageInput(false);
setCustomLM('');
setField('k', 10);
setClassificationTask('sentiment');
setField('metrictarget', 0.5);
}, [isExample]); // 當 dataset 改為/離開 example 時觸發
const card =
'group relative rounded-2xl p-8 border border-white/30 bg-white/60 backdrop-blur-xl ' +
'shadow-[0_15px_40px_-20px_rgba(30,41,59,0.35)] transition-all duration-300 ' +
'hover:shadow-[0_20px_50px_-20px_rgba(79,70,229,0.45)] hover:-translate-y-0.5';
const sectionTitle = 'text-xl font-bold tracking-tight text-slate-900';
const fieldInput =
'w-full rounded-xl border-2 border-slate-200/70 bg-white/70 px-4 py-3 ' +
'focus:outline-none focus:border-indigo-500 focus:ring-4 focus:ring-indigo-500/20 transition-all';
const selectInput =
'w-full rounded-xl border-2 border-slate-200/70 bg-white/70 px-3 py-2.5 ' +
'focus:outline-none focus:border-indigo-500 focus:ring-4 focus:ring-indigo-500/20 transition-all';
const canRun = !!(cfg.dataset && (cfg.languageModel || customLM));
return (
<div className="space-y-10">
<div className="grid grid-cols-1 lg:grid-cols-6 gap-8">
{/* 卡片 1:Language Generation Model */}
<div className={`${card} lg:col-span-3`}>
<div className="flex items-center gap-3 mb-8">
<div className="p-3 rounded-xl bg-gradient-to-br from-emerald-600 to-teal-600 shadow-md shadow-emerald-600/30">
<Bot className="w-6 h-6 text-white" />
</div>
<h3 className={sectionTitle}>Language Generation Model</h3>
{isExample && (
<span className="ml-2 inline-flex items-center gap-1 text-[11px] font-semibold px-2 py-0.5 rounded-full bg-slate-900 text-white">
<Lock className="w-3 h-3" /> Locked by Example
</span>
)}
</div>
<div className="space-y-8">
<div>
<label className="block text-sm font-semibold text-slate-800 mb-2">Model</label>
<select
value={isExample ? 'openai-community/gpt2' : cfg.languageModel}
onChange={(e) => {
if (isExample) return;
setField('languageModel', e.target.value);
setShowCustomLanguageInput(e.target.value === 'custom');
}}
disabled={isExample}
className={selectInput + (isExample ? ' cursor-not-allowed opacity-80' : '')}
>
<option value="">Select a Language Model</option>
{LM_MODELS.map((m) => (
<option key={m.id} value={m.id}>
{m.name}({m.provider})
</option>
))}
<option value="custom">🔧 Custom Model Upload from Hugging Face</option>
</select>
{!isExample && showCustomLanguageInput && (
<input
type="text"
placeholder="Input Hugging Face Model ID (e.g.: microsoft/DialoGPT-medium)"
value={customLM}
onChange={(e) => {
setCustomLM(e.target.value);
setField('languageModel', e.target.value);
}}
className={`${fieldInput} mt-3`}
/>
)}
{(isExample || customLM || cfg.languageModel) && (
<div className="mt-3">
<ModelValidator modelId={isExample ? 'openai-community/gpt2' : (customLM || cfg.languageModel)} type="language" />
</div>
)}
</div>
{/* K 與 datasetLimit */}
<div className="grid grid-cols-1 md:grid-cols-2 gap-5">
<div>
<label className="block text-sm font-semibold text-slate-800 mb-1">
Number of Candidates
<span className="ml-2 text-xs font-normal text-slate-500">
The number of candidates generated for each entity
</span>
</label>
<input
type="number"
min={1}
max={20}
value={isExample ? 10 : cfg.k}
onChange={(e) => {
if (isExample) return;
setField('k', parseInt(e.target.value || '0', 10));
}}
disabled={isExample}
className={fieldInput + (isExample ? ' cursor-not-allowed opacity-80' : '')}
/>
{isExample && (
<div className="text-[11px] mt-1 text-slate-500 flex items-center gap-1">
<Lock className="w-3 h-3" /> Locked to 10 for the Example preset.
</div>
)}
</div>
<div>
<label className="block text-sm font-semibold text-slate-800 mb-1">
Number of Data
</label>
<input
type="number"
min={1}
max={10000}
value={datasetLimit}
onChange={(e) => setDatasetLimit(parseInt(e.target.value || '0', 10))}
className={fieldInput}
/>
</div>
</div>
</div>
</div>
{/* 卡片 2:Feature Extraction / Classification */}
<div className={`${card} lg:col-span-3`}>
<div className="flex items-center gap-3 mb-8">
<div className="p-3 rounded-xl bg-gradient-to-br from-indigo-600 to-fuchsia-600 shadow-md shadow-indigo-600/30">
<Settings2 className="w-6 h-6 text-white" />
</div>
<h3 className={sectionTitle}>Feature Extraction Model</h3>
{isExample && (
<span className="ml-2 inline-flex items-center gap-1 text-[11px] font-semibold px-2 py-0.5 rounded-full bg-slate-900 text-white">
<Lock className="w-3 h-3" /> Locked by Example
</span>
)}
</div>
<div className="space-y-8">
<div>
<label className="block text-sm font-semibold text-slate-800 mb-1">
Task
</label>
<select
value={isExample ? 'sentiment' : classificationTask}
onChange={(e) => {
if (isExample) return;
setClassificationTask(e.target.value as any);
}}
disabled={isExample}
className={selectInput + (isExample ? ' cursor-not-allowed opacity-80' : '')}
>
<option value="sentiment">Sentiment (0–1, Neutral ≈ 0.5)</option>
<option value="regard">Regard (0–2, Neutral ≈ 1.0)</option>
<option value="stereotype">Stereotype (0–1, Neutral ≈ 0.0)</option>
<option value="personality">Personality (0–1, Neutral ≈ 0.2)</option>
<option value="toxicity">Toxicity (0–1, Neutral ≈ 0.0)</option>
</select>
</div>
{/* 只有 toxicity 才需要,example 強制 sentiment 就不顯示這塊 */}
{classificationTask === 'toxicity' && !isExample && (
<div>
<label className="block text-sm font-semibold text-slate-800 mb-1">
Toxicity Model
</label>
<select
value={toxicityModelChoice}
onChange={(e) => setToxicityModelChoice(e.target.value as any)}
className={selectInput}
>
<option value="detoxify">unitary/toxic-bert(detoxify)</option>
<option value="junglelee">JungleLee/bert-toxic-comment-classification</option>
</select>
</div>
)}
<div>
<label className="block text-sm font-semibold text-slate-800 mb-1">
Metric Target Value
<span className="ml-2 text-xs font-normal text-slate-500">
Indicator thresholds used to determine compliance
</span>
</label>
<input
type="number"
min={0}
max={2}
step={0.01}
value={isExample ? 0.5 : cfg.metrictarget}
onChange={(e) => {
if (isExample) return;
setField('metrictarget', parseFloat(e.target.value || '0'));
}}
disabled={isExample}
className={fieldInput + (isExample ? ' cursor-not-allowed opacity-80' : '')}
/>
{isExample && (
<div className="text-[11px] mt-1 text-slate-500 flex items-center gap-1">
<Lock className="w-3 h-3" /> Locked to 0.5 for the Example preset.
</div>
)}
</div>
</div>
</div>
</div>
{/* Run */}
<div className="flex">
<button
onClick={() => {
// 讀回 Dataset 頁草稿並合併
let mergedCfg: JobConfig = { ...cfg };
try {
const draft = localStorage.getItem('cfgDraft');
if (draft) {
const parsed = JSON.parse(draft);
mergedCfg = { ...parsed, ...cfg, languageModel: cfg.languageModel || parsed.languageModel };
}
} catch {}
// 🔒 若為 example,再次保險強制設定
if (isExample) {
mergedCfg = {
...mergedCfg,
languageModel: 'openai-community/gpt2',
k: 10,
metrictarget: 0.5,
} as JobConfig;
}
// 將目前 model 相關設定也寫回草稿,之後回到本頁仍可帶入
const persist = {
...mergedCfg,
classificationTask: isExample ? 'sentiment' : classificationTask,
toxicityModelChoice, // 不會用到,保留存檔
} as any;
localStorage.setItem('cfgDraft', JSON.stringify(persist));
localStorage.setItem('extrasDraft', JSON.stringify({ datasetLimit }));
onRun(
{
...mergedCfg,
classificationTask: isExample ? 'sentiment' : classificationTask,
toxicityModelChoice,
} as any,
{
datasetLimit,
}
);
}}
disabled={!canRun}
className="relative w-full group overflow-hidden rounded-2xl px-6 py-4 text-white font-semibold bg-gradient-to-r from-indigo-600 via-violet-600 to-fuchsia-600 shadow-lg shadow-indigo-600/20 enabled:hover:shadow-indigo-600/40 transition-all enabled:hover:translate-y-[-1px] enabled:active:translate-y-0 disabled:opacity-60 disabled:cursor-not-allowed"
>
<span className="relative z-10">🚀 Start</span>
<span className="absolute inset-0 opacity-0 group-hover:opacity-100 transition-opacity bg-[radial-gradient(1200px_200px_at_50%_-40%,rgba(255,255,255,0.35),transparent_60%)]" />
</button>
</div>
</div>
);
}
|