Spaces:
Runtime error
Runtime error
File size: 14,656 Bytes
5752a28 | 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 340 341 342 343 344 345 346 347 | import { useState, useEffect } from 'react';
import { Gauge, FlaskConical, Loader2, AlertTriangle, Sparkles, ShieldAlert, ShieldCheck } from 'lucide-react';
import { Card } from './ui/card';
import { Button } from './ui/button';
import { API_BASE_URL } from '../config';
interface ClassStats {
precision: number;
recall: number;
f1: number;
support: number;
}
interface Metrics {
modelName: string;
datasetSize: number;
numClasses: number;
testSize: number;
testFraction: number;
trainGroupCount: number;
testGroupCount: number;
groupOverlap: number;
splitMethod: string;
accuracy: number;
macroF1: number;
weightedF1: number;
logLoss: number;
calibrated: boolean;
calibrationMethod: string;
perClass: Record<string, ClassStats>;
classDistribution: Record<string, number>;
rareClasses: Record<string, number>;
minimumRecommendedClassSize: number;
confusionMatrix: { labels: string[]; matrix: number[][] };
}
interface TextResult {
text: string;
prediction: string;
confidence: number;
isDarkPattern: boolean;
topClasses: { label: string; probability: number }[];
explanation: { phrase: string; weight: number }[];
calibrated: boolean;
confidenceBand: string;
cfpbViolation?: string;
recommendation?: string;
}
const EXAMPLE_TEXTS = [
'Hurry! Only 3 left in stock β sale ends in 10 minutes!',
'2,847 customers bought this in the last 24 hours',
'No thanks, I hate saving money',
'Your monthly statement is available in the documents section.',
];
export function ModelPerformance() {
const [metrics, setMetrics] = useState<Metrics | null>(null);
const [metricsError, setMetricsError] = useState<string | null>(null);
const [text, setText] = useState('');
const [testing, setTesting] = useState(false);
const [result, setResult] = useState<TextResult | null>(null);
const [testError, setTestError] = useState<string | null>(null);
useEffect(() => {
fetch(`${API_BASE_URL}/api/metrics`)
.then((r) => {
if (!r.ok) throw new Error(`Server returned ${r.status}`);
return r.json();
})
.then(setMetrics)
.catch((e) => setMetricsError(e.message));
}, []);
const runTest = async (input?: string) => {
const value = (input ?? text).trim();
if (value.length < 3) return;
if (input) setText(input);
setTesting(true);
setTestError(null);
setResult(null);
try {
const r = await fetch(`${API_BASE_URL}/api/analyze-text`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: value }),
});
const data = await r.json();
if (!r.ok || data.error) throw new Error(data.error || `Server returned ${r.status}`);
setResult(data);
} catch (e: any) {
setTestError(e.message);
} finally {
setTesting(false);
}
};
const f1Color = (f1: number) =>
f1 >= 0.9 ? 'bg-emerald-500' : f1 >= 0.7 ? 'bg-amber-500' : 'bg-rose-500';
return (
<div className="space-y-8">
{/* ββ Live Text Lab βββββββββββββββββββββββββββββββββββββββββββ */}
<Card className="p-8 bg-card/40 backdrop-blur-md border border-border shadow-xl rounded-2xl">
<h2 className="text-xl font-bold text-foreground mb-1 flex items-center gap-2">
<FlaskConical className="w-5 h-5 text-indigo-500 dark:text-indigo-400" />
Live Text Lab
</h2>
<p className="text-xs text-muted-foreground mb-6">
Paste any fintech copywriting and see what the model thinks β and which phrases drove the decision.
</p>
<div className="flex flex-col md:flex-row gap-3">
<input
value={text}
onChange={(e) => setText(e.target.value)}
onKeyDown={(e) => e.key === 'Enter' && runTest()}
placeholder='e.g. "Hurry! Only 2 left in stock!"'
className="flex-1 px-4 py-3 rounded-xl bg-muted/20 border border-border text-sm text-foreground placeholder:text-muted-foreground focus:outline-none focus:border-indigo-500/60"
/>
<Button
onClick={() => runTest()}
disabled={testing || text.trim().length < 3}
className="h-12 px-8 rounded-xl text-xs font-bold uppercase tracking-wider bg-gradient-to-r from-indigo-600 to-purple-600 hover:from-indigo-500 hover:to-purple-500 text-white"
>
{testing ? <Loader2 className="w-4 h-4 animate-spin" /> : 'Classify'}
</Button>
</div>
<div className="flex flex-wrap gap-2 mt-3">
{EXAMPLE_TEXTS.map((t) => (
<button
key={t}
onClick={() => runTest(t)}
className="text-[11px] px-3 py-1.5 rounded-full border border-border bg-muted/10 text-muted-foreground hover:text-foreground hover:border-indigo-500/40 transition-colors"
>
{t}
</button>
))}
</div>
{testError && (
<div className="mt-4 p-3 rounded-xl text-rose-500 bg-rose-500/10 border border-rose-500/25 text-xs font-semibold">
{testError}
</div>
)}
{result && (
<div className="mt-6 p-5 rounded-xl border border-border bg-muted/10 space-y-4">
<div className="flex items-center gap-3 flex-wrap">
{result.isDarkPattern ? (
<ShieldAlert className="w-6 h-6 text-rose-500" />
) : (
<ShieldCheck className="w-6 h-6 text-emerald-500" />
)}
<span
className={`text-sm font-extrabold uppercase tracking-wide ${
result.isDarkPattern ? 'text-rose-500' : 'text-emerald-500'
}`}
>
{result.prediction}
</span>
<span className="text-xs font-mono text-muted-foreground">
{result.confidence}% calibrated confidence ({result.confidenceBand})
</span>
</div>
{result.explanation.length > 0 && (
<div>
<div className="text-[10px] font-bold uppercase tracking-widest text-muted-foreground mb-2 flex items-center gap-1.5">
<Sparkles className="w-3 h-3" /> Phrases that triggered this verdict
</div>
<div className="flex flex-wrap gap-2">
{result.explanation.map((e) => (
<span
key={e.phrase}
className="text-xs font-mono px-2.5 py-1 rounded-md bg-indigo-500/10 text-indigo-500 dark:text-indigo-300 border border-indigo-500/25"
title={`weight ${e.weight}`}
>
{e.phrase}
</span>
))}
</div>
</div>
)}
<div className="grid grid-cols-3 gap-2">
{result.topClasses.map((c) => (
<div key={c.label} className="p-2.5 rounded-lg bg-muted/20 border border-border">
<div className="text-[10px] text-muted-foreground truncate">{c.label}</div>
<div className="text-sm font-bold text-foreground">{c.probability}%</div>
</div>
))}
</div>
{result.cfpbViolation && (
<div className="p-3 rounded-lg bg-amber-500/10 border border-amber-500/25 text-xs text-amber-600 dark:text-amber-400">
<strong>Compliance:</strong> {result.cfpbViolation}
</div>
)}
</div>
)}
</Card>
{/* ββ Evaluation metrics ββββββββββββββββββββββββββββββββββββββ */}
<Card className="p-8 bg-card/40 backdrop-blur-md border border-border shadow-xl rounded-2xl">
<h2 className="text-xl font-bold text-foreground mb-1 flex items-center gap-2">
<Gauge className="w-5 h-5 text-indigo-500 dark:text-indigo-400" />
Model Performance
</h2>
<p className="text-xs text-muted-foreground mb-6">
Evaluated on a grouped held-out fold. Text from the same source page never appears in both training and testing.
</p>
{metricsError && (
<div className="p-4 rounded-xl text-rose-500 bg-rose-500/10 border border-rose-500/25 text-xs font-semibold">
Failed to load metrics: {metricsError}
</div>
)}
{!metrics && !metricsError && (
<div className="flex items-center justify-center py-16">
<Loader2 className="w-6 h-6 animate-spin text-indigo-500" />
</div>
)}
{metrics && (
<div className="space-y-8">
<div className="text-xs font-mono text-muted-foreground">{metrics.modelName}</div>
<div className="text-[11px] text-muted-foreground">
{metrics.splitMethod} Β· {metrics.testGroupCount} test pages Β· {metrics.groupOverlap} page overlap Β· {metrics.calibrationMethod}
</div>
<div className="grid grid-cols-2 md:grid-cols-4 gap-4">
{[
{ label: 'Accuracy', value: `${(metrics.accuracy * 100).toFixed(1)}%` },
{ label: 'Macro F1', value: metrics.macroF1.toFixed(3) },
{ label: 'Weighted F1', value: metrics.weightedF1.toFixed(3) },
{ label: 'Training samples', value: metrics.datasetSize.toLocaleString() },
].map((s) => (
<div key={s.label} className="p-4 rounded-xl bg-muted/10 border border-border">
<div className="text-[10px] font-bold uppercase tracking-widest text-muted-foreground">
{s.label}
</div>
<div className="text-2xl font-extrabold text-foreground mt-1">{s.value}</div>
</div>
))}
</div>
{/* Per-class table */}
<div>
<h3 className="text-sm font-bold text-foreground mb-3">F1 score per pattern class</h3>
<div className="space-y-2">
{Object.entries(metrics.perClass)
.sort((a, b) => b[1].f1 - a[1].f1)
.map(([cls, s]) => (
<div key={cls} className="flex items-center gap-3">
<div className="w-36 text-xs font-semibold text-foreground truncate">{cls}</div>
<div className="flex-1 h-3 rounded-full bg-muted/30 overflow-hidden">
<div
className={`h-full rounded-full ${f1Color(s.f1)}`}
style={{ width: `${Math.max(s.f1 * 100, 2)}%` }}
/>
</div>
<div className="w-12 text-xs font-mono text-foreground text-right">
{s.f1.toFixed(2)}
</div>
<div className="w-20 text-[10px] font-mono text-muted-foreground text-right">
n={s.support}
</div>
</div>
))}
</div>
</div>
{/* Low-support warning β honest about dataset limitations */}
{Object.keys(metrics.rareClasses).length > 0 && (
<div className="flex items-start gap-2.5 p-3.5 rounded-xl bg-amber-500/10 border border-amber-500/25">
<AlertTriangle className="w-4 h-4 text-amber-500 mt-0.5 shrink-0" />
<p className="text-xs text-amber-600 dark:text-amber-400 leading-relaxed">
Classes below {metrics.minimumRecommendedClassSize} total samples remain unreliable:{' '}
{Object.entries(metrics.rareClasses)
.map(([label, count]) => `${label} (${count})`)
.join(', ')}. No synthetic records were added to inflate these scores.
</p>
</div>
)}
{/* Confusion matrix */}
<div>
<h3 className="text-sm font-bold text-foreground mb-3">Confusion matrix (rows = truth, columns = prediction)</h3>
<div className="overflow-x-auto">
<table className="text-[10px] font-mono border-collapse">
<thead>
<tr>
<th className="p-1.5" />
{metrics.confusionMatrix.labels.map((l) => (
<th key={l} className="p-1.5 text-muted-foreground font-semibold max-w-16 truncate" title={l}>
{l.split(' ')[0]}
</th>
))}
</tr>
</thead>
<tbody>
{metrics.confusionMatrix.matrix.map((row, i) => {
const rowMax = Math.max(...row, 1);
return (
<tr key={metrics.confusionMatrix.labels[i]}>
<td className="p-1.5 text-muted-foreground font-semibold text-right pr-3 whitespace-nowrap">
{metrics.confusionMatrix.labels[i]}
</td>
{row.map((v, j) => (
<td
key={j}
className={`p-1.5 text-center min-w-12 rounded ${
v === 0
? 'text-muted-foreground/40'
: i === j
? 'text-emerald-500 font-bold'
: 'text-rose-500 font-bold'
}`}
style={{
backgroundColor:
v > 0
? i === j
? `rgba(16,185,129,${0.08 + 0.3 * (v / rowMax)})`
: `rgba(244,63,94,${0.08 + 0.3 * (v / rowMax)})`
: undefined,
}}
>
{v}
</td>
))}
</tr>
);
})}
</tbody>
</table>
</div>
</div>
</div>
)}
</Card>
</div>
);
}
|