Create JobForm.tsx
Browse files- frontend/components/JobForm.tsx +107 -0
frontend/components/JobForm.tsx
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useState } from 'react';
|
| 2 |
+
import { useTranslation } from 'next-i18next';
|
| 3 |
+
import ApiKeyInput from './ApiKeyInput';
|
| 4 |
+
import EvolutionaryParams from './EvolutionaryParams';
|
| 5 |
+
|
| 6 |
+
export default function JobForm() {
|
| 7 |
+
const { t } = useTranslation('common');
|
| 8 |
+
const [method, setMethod] = useState('linear');
|
| 9 |
+
const [modelASource, setModelASource] = useState('hf');
|
| 10 |
+
const [modelBSource, setModelBSource] = useState('hf');
|
| 11 |
+
const [modelAId, setModelAId] = useState('');
|
| 12 |
+
const [modelBId, setModelBId] = useState('');
|
| 13 |
+
const [alpha, setAlpha] = useState(0.5);
|
| 14 |
+
const [outputRepo, setOutputRepo] = useState('');
|
| 15 |
+
const [datasetFile, setDatasetFile] = useState<File | null>(null);
|
| 16 |
+
const [evoParams, setEvoParams] = useState({});
|
| 17 |
+
const [submitting, setSubmitting] = useState(false);
|
| 18 |
+
|
| 19 |
+
const handleSubmit = async (e: React.FormEvent) => {
|
| 20 |
+
e.preventDefault();
|
| 21 |
+
setSubmitting(true);
|
| 22 |
+
let datasetPath = '';
|
| 23 |
+
if (datasetFile) {
|
| 24 |
+
const formData = new FormData();
|
| 25 |
+
formData.append('file', datasetFile);
|
| 26 |
+
const res = await fetch('/api/backend/upload-dataset', { method: 'POST', body: formData });
|
| 27 |
+
const json = await res.json();
|
| 28 |
+
datasetPath = json.path;
|
| 29 |
+
}
|
| 30 |
+
const payload = {
|
| 31 |
+
model_a_source: modelASource,
|
| 32 |
+
model_a_id: modelAId,
|
| 33 |
+
model_b_source: modelBSource,
|
| 34 |
+
model_b_id: modelBId,
|
| 35 |
+
method,
|
| 36 |
+
linear_alpha: alpha,
|
| 37 |
+
output_repo_name: outputRepo,
|
| 38 |
+
dataset: datasetPath,
|
| 39 |
+
evo_params: method === 'evolutionary' ? evoParams : null,
|
| 40 |
+
// api keys passed via ApiKeyInput context, but we'll get from sessionStorage for simplicity
|
| 41 |
+
civitai_key: sessionStorage.getItem('civitai_key') || '',
|
| 42 |
+
};
|
| 43 |
+
const res = await fetch('/api/backend/submit-job', {
|
| 44 |
+
method: 'POST',
|
| 45 |
+
headers: { 'Content-Type': 'application/json' },
|
| 46 |
+
body: JSON.stringify(payload),
|
| 47 |
+
});
|
| 48 |
+
if (res.ok) {
|
| 49 |
+
alert(t('job_submitted'));
|
| 50 |
+
// clear
|
| 51 |
+
} else {
|
| 52 |
+
alert('Error submitting job');
|
| 53 |
+
}
|
| 54 |
+
setSubmitting(false);
|
| 55 |
+
};
|
| 56 |
+
|
| 57 |
+
return (
|
| 58 |
+
<form onSubmit={handleSubmit} className="space-y-4 bg-white p-6 shadow rounded-lg">
|
| 59 |
+
<ApiKeyInput />
|
| 60 |
+
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
| 61 |
+
<div>
|
| 62 |
+
<label className="block text-sm font-medium">{t('model_a')}</label>
|
| 63 |
+
<select value={modelASource} onChange={e => setModelASource(e.target.value)} className="mt-1 block w-full border rounded p-2">
|
| 64 |
+
<option value="hf">HuggingFace</option>
|
| 65 |
+
<option value="civitai">Civitai</option>
|
| 66 |
+
</select>
|
| 67 |
+
<input type="text" placeholder={t('repo_id_or_url')} value={modelAId} onChange={e => setModelAId(e.target.value)} className="mt-1 block w-full border rounded p-2" required />
|
| 68 |
+
</div>
|
| 69 |
+
<div>
|
| 70 |
+
<label className="block text-sm font-medium">{t('model_b')}</label>
|
| 71 |
+
<select value={modelBSource} onChange={e => setModelBSource(e.target.value)} className="mt-1 block w-full border rounded p-2">
|
| 72 |
+
<option value="hf">HuggingFace</option>
|
| 73 |
+
<option value="civitai">Civitai</option>
|
| 74 |
+
</select>
|
| 75 |
+
<input type="text" placeholder={t('repo_id_or_url')} value={modelBId} onChange={e => setModelBId(e.target.value)} className="mt-1 block w-full border rounded p-2" required />
|
| 76 |
+
</div>
|
| 77 |
+
</div>
|
| 78 |
+
<div>
|
| 79 |
+
<label className="block text-sm font-medium">{t('method')}</label>
|
| 80 |
+
<select value={method} onChange={e => setMethod(e.target.value)} className="mt-1 block w-full border rounded p-2">
|
| 81 |
+
<option value="linear">{t('linear')}</option>
|
| 82 |
+
<option value="evolutionary">{t('evolutionary')}</option>
|
| 83 |
+
</select>
|
| 84 |
+
</div>
|
| 85 |
+
{method === 'linear' && (
|
| 86 |
+
<div>
|
| 87 |
+
<label className="block text-sm font-medium">{t('alpha')}: {alpha}</label>
|
| 88 |
+
<input type="range" min="0" max="1" step="0.01" value={alpha} onChange={e => setAlpha(parseFloat(e.target.value))} className="w-full" />
|
| 89 |
+
</div>
|
| 90 |
+
)}
|
| 91 |
+
{method === 'evolutionary' && (
|
| 92 |
+
<EvolutionaryParams onChange={setEvoParams} />
|
| 93 |
+
)}
|
| 94 |
+
<div>
|
| 95 |
+
<label className="block text-sm font-medium">{t('calibration_dataset')} ({t('optional')})</label>
|
| 96 |
+
<input type="file" onChange={e => setDatasetFile(e.target.files?.[0] || null)} />
|
| 97 |
+
</div>
|
| 98 |
+
<div>
|
| 99 |
+
<label className="block text-sm font-medium">{t('output_repo_name')}</label>
|
| 100 |
+
<input type="text" value={outputRepo} onChange={e => setOutputRepo(e.target.value)} placeholder="my-merged-model" className="mt-1 block w-full border rounded p-2" />
|
| 101 |
+
</div>
|
| 102 |
+
<button type="submit" disabled={submitting} className="bg-blue-600 text-white px-4 py-2 rounded hover:bg-blue-700 disabled:opacity-50">
|
| 103 |
+
{submitting ? t('submitting') : t('start_merge')}
|
| 104 |
+
</button>
|
| 105 |
+
</form>
|
| 106 |
+
);
|
| 107 |
+
}
|