import { useState } from "react"; import { api, getErrorMessage } from "../api"; import type { TrainResponse } from "../types"; import { useCorpusLoader } from "../hooks/useCorpusLoader"; import StatusMessage from "./StatusMessage"; import MetricCard from "./MetricCard"; import Toggle from "./Toggle"; import Select from "./Select"; import LogViewer from "./LogViewer"; type Strategy = "unsupervised" | "contrastive" | "keywords"; const STRATEGIES: { id: Strategy; label: string; desc: string }[] = [ { id: "unsupervised", label: "Unsupervised", desc: "Soft-label domain adaptation. Samples random pairs and fine-tunes using the model's own similarity scores." }, { id: "contrastive", label: "Contrastive", desc: "Adjacent sentences = positive pairs. Learns document structure with in-batch negatives and validation." }, { id: "keywords", label: "Keyword-supervised", desc: "You provide keyword→meaning map. Best if you know the code words." }, ]; const MODELS = [ { value: "all-MiniLM-L6-v2", label: "all-MiniLM-L6-v2 (fast)" }, { value: "all-mpnet-base-v2", label: "all-mpnet-base-v2 (best quality)" }, ]; export default function TrainingPanel() { // Training const [strategy, setStrategy] = useState("contrastive"); const [baseModel, setBaseModel] = useState("all-MiniLM-L6-v2"); const [outputPath, setOutputPath] = useState("./trained_model"); const [epochs, setEpochs] = useState(5); const [batchSize, setBatchSize] = useState(16); const [keywordMapText, setKeywordMapText] = useState('{\n "pizza": "school",\n "pepperoni": "math class"\n}'); const [showAdvanced, setShowAdvanced] = useState(false); const [training, setTraining] = useState(false); const [result, setResult] = useState(null); const { corpusText, setCorpusText, loading: corpusLoading, error, setError, parseCorpus, loadFromEngine } = useCorpusLoader(); async function handleTrain() { setTraining(true); setError(""); setResult(null); try { const corpus = parseCorpus(); if (!corpus.length) { setError("Corpus is empty."); setTraining(false); return; } const base = { corpus_texts: corpus, base_model: baseModel, output_path: outputPath, epochs, batch_size: batchSize }; let res: TrainResponse; if (strategy === "unsupervised") { res = await api.trainUnsupervised(base); } else if (strategy === "contrastive") { res = await api.trainContrastive(base); } else { const kw = JSON.parse(keywordMapText); res = await api.trainKeywords({ ...base, keyword_meanings: kw }); } setResult(res); } catch (e) { setError(e instanceof SyntaxError ? "Invalid JSON in keyword map." : getErrorMessage(e)); } finally { setTraining(false); } } return (
{/* 1. Training (strategy + config + corpus merged) */}

1. Fine-tune Transformer

Fine-tune a pre-trained sentence transformer on your corpus to improve contextual understanding.

{corpusText && ( )}