-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.tsx
More file actions
256 lines (240 loc) · 9.39 KB
/
admin.tsx
File metadata and controls
256 lines (240 loc) · 9.39 KB
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
import { useState, useEffect } from "react";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { Separator } from "@/components/ui/separator";
import { TrendingUp, Brain, Target, AlertCircle, RefreshCw } from "lucide-react";
import { useToast } from "@/hooks/use-toast";
interface PerformanceMetrics {
totalPredictions: number;
correctPredictions: number;
accuracy: number;
commonMistakes: Array<{
incorrectBrand: string;
incorrectModel: string;
correctBrand: string;
correctModel: string;
frequency: number;
}>;
brandAccuracy: Record<string, number>;
confidenceDistribution: Record<string, number>;
}
interface TrainingData {
performance: PerformanceMetrics | null;
recommendations: string[];
}
export default function AdminPage() {
const [trainingData, setTrainingData] = useState<TrainingData | null>(null);
const [isRetraining, setIsRetraining] = useState(false);
const [loading, setLoading] = useState(true);
const { toast } = useToast();
useEffect(() => {
loadTrainingData();
}, []);
const loadTrainingData = async () => {
try {
const response = await fetch('/api/training/performance');
if (response.ok) {
const data = await response.json();
setTrainingData(data);
}
} catch (error) {
console.error('Error loading training data:', error);
} finally {
setLoading(false);
}
};
const handleRetrain = async () => {
setIsRetraining(true);
try {
const response = await fetch('/api/training/retrain', { method: 'POST' });
if (response.ok) {
const result = await response.json();
toast({
title: "Retraining Complete",
description: result.message,
});
await loadTrainingData(); // Reload metrics
} else {
throw new Error('Retrain failed');
}
} catch (error) {
toast({
title: "Retraining Failed",
description: "Please try again or check the logs.",
variant: "destructive",
});
} finally {
setIsRetraining(false);
}
};
if (loading) {
return (
<div className="min-h-screen bg-gray-50 p-8">
<div className="max-w-6xl mx-auto">
<div className="text-center">Loading training data...</div>
</div>
</div>
);
}
const performance = trainingData?.performance;
return (
<div className="min-h-screen bg-gray-50 p-8">
<div className="max-w-6xl mx-auto">
<div className="mb-8">
<h1 className="text-3xl font-bold text-gray-900 mb-2">AI Training Dashboard</h1>
<p className="text-gray-600">Monitor and improve lacrosse head identification accuracy</p>
</div>
{!performance ? (
<Card>
<CardContent className="p-8 text-center">
<AlertCircle className="h-12 w-12 text-gray-400 mx-auto mb-4" />
<h3 className="text-lg font-semibold text-gray-900 mb-2">No Training Data Yet</h3>
<p className="text-gray-600 mb-4">
Start collecting training data by having users report incorrect identifications.
</p>
<Button onClick={loadTrainingData}>
<RefreshCw className="mr-2 h-4 w-4" />
Refresh Data
</Button>
</CardContent>
</Card>
) : (
<div className="space-y-6">
{/* Overall Performance */}
<div className="grid grid-cols-1 md:grid-cols-4 gap-6">
<Card>
<CardHeader className="pb-3">
<CardTitle className="text-sm font-medium text-gray-600">Total Predictions</CardTitle>
</CardHeader>
<CardContent>
<div className="text-2xl font-bold text-gray-900">{performance.totalPredictions}</div>
</CardContent>
</Card>
<Card>
<CardHeader className="pb-3">
<CardTitle className="text-sm font-medium text-gray-600">Correct Predictions</CardTitle>
</CardHeader>
<CardContent>
<div className="text-2xl font-bold text-green-600">{performance.correctPredictions}</div>
</CardContent>
</Card>
<Card>
<CardHeader className="pb-3">
<CardTitle className="text-sm font-medium text-gray-600">Accuracy</CardTitle>
</CardHeader>
<CardContent>
<div className="text-2xl font-bold text-blue-600">{performance.accuracy.toFixed(1)}%</div>
<Badge className={performance.accuracy >= 80 ? "bg-green-100 text-green-800" : "bg-yellow-100 text-yellow-800"}>
{performance.accuracy >= 80 ? "Good" : "Needs Improvement"}
</Badge>
</CardContent>
</Card>
<Card>
<CardHeader className="pb-3">
<CardTitle className="text-sm font-medium text-gray-600">Actions</CardTitle>
</CardHeader>
<CardContent>
<Button
onClick={handleRetrain}
disabled={isRetraining || performance.totalPredictions < 5}
className="w-full bg-blue-600 hover:bg-blue-700 text-white"
>
{isRetraining ? (
<>
<RefreshCw className="mr-2 h-4 w-4 animate-spin" />
Retraining...
</>
) : (
<>
<Brain className="mr-2 h-4 w-4" />
Retrain AI
</>
)}
</Button>
</CardContent>
</Card>
</div>
{/* Common Mistakes */}
{performance.commonMistakes.length > 0 && (
<Card>
<CardHeader>
<CardTitle className="flex items-center">
<Target className="mr-2 h-5 w-5" />
Common Mistakes
</CardTitle>
</CardHeader>
<CardContent>
<div className="space-y-3">
{performance.commonMistakes.slice(0, 5).map((mistake, index) => (
<div key={index} className="flex items-center justify-between p-3 bg-red-50 rounded-lg">
<div>
<p className="font-medium text-red-900">
Confused {mistake.correctBrand} {mistake.correctModel}
</p>
<p className="text-sm text-red-600">
with {mistake.incorrectBrand} {mistake.incorrectModel}
</p>
</div>
<Badge className="bg-red-100 text-red-800">
{mistake.frequency} times
</Badge>
</div>
))}
</div>
</CardContent>
</Card>
)}
{/* Brand Performance */}
<Card>
<CardHeader>
<CardTitle className="flex items-center">
<TrendingUp className="mr-2 h-5 w-5" />
Brand Accuracy
</CardTitle>
</CardHeader>
<CardContent>
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
{Object.entries(performance.brandAccuracy).map(([brand, accuracy]) => (
<div key={brand} className="p-3 bg-gray-50 rounded-lg">
<div className="flex items-center justify-between">
<span className="font-medium text-gray-900">{brand}</span>
<span className="text-lg font-bold text-blue-600">{accuracy.toFixed(1)}%</span>
</div>
<div className="mt-2 bg-gray-200 rounded-full h-2">
<div
className={`h-2 rounded-full ${accuracy >= 80 ? 'bg-green-500' : accuracy >= 60 ? 'bg-yellow-500' : 'bg-red-500'}`}
style={{ width: `${Math.min(100, accuracy)}%` }}
/>
</div>
</div>
))}
</div>
</CardContent>
</Card>
{/* Recommendations */}
{trainingData.recommendations.length > 0 && (
<Card>
<CardHeader>
<CardTitle className="flex items-center">
<AlertCircle className="mr-2 h-5 w-5" />
Training Recommendations
</CardTitle>
</CardHeader>
<CardContent>
<div className="space-y-2">
{trainingData.recommendations.map((recommendation, index) => (
<div key={index} className="p-3 bg-blue-50 rounded-lg">
<p className="text-blue-900">{recommendation}</p>
</div>
))}
</div>
</CardContent>
</Card>
)}
</div>
)}
</div>
</div>
);
}