-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfront.js
More file actions
711 lines (659 loc) · 25.9 KB
/
Copy pathfront.js
File metadata and controls
711 lines (659 loc) · 25.9 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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
import React, { useState } from "react";
import {
AlertCircle,
CheckCircle,
TrendingUp,
Users,
Activity,
Upload,
} from "lucide-react";
const StudentAnomalyDetector = () => {
const [formData, setFormData] = useState({
code_module: "AAA",
code_presentation: "2013J",
gender: "M",
region: "East Anglian Region",
highest_education: "HE Qualification",
imd_band: "20-30%",
age_band: "35-55",
disability: "N",
studied_credits: 60,
num_of_prev_attempts: 0,
avg_score: 50,
std_score: 15,
min_score: 20,
max_score: 85,
num_assessments: 5,
avg_submission_date: 100,
std_submission_date: 30,
score_range: 65,
total_clicks: 500,
avg_clicks: 50,
std_clicks: 20,
max_clicks: 150,
num_interactions: 10,
first_access: 10,
last_access: 200,
access_duration: 190,
avg_registration_date: -15,
num_unregistrations: 0,
});
const [prediction, setPrediction] = useState(null);
const [loading, setLoading] = useState(false);
const [activeTab, setActiveTab] = useState("basic");
// Call the real API
const predictAnomaly = async () => {
setLoading(true);
try {
// Change this URL to your deployed API URL
const API_URL = "http://localhost:5000";
const response = await fetch(`${API_URL}/predict`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(formData),
});
if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}
const data = await response.json();
setPrediction({
isAnomaly: data.isAnomaly,
riskScore: data.riskScore,
confidence: data.confidence,
factors: data.factors || [],
});
} catch (error) {
console.error("Prediction error:", error);
alert(
"Failed to get prediction. Make sure the API is running on http://localhost:5000"
);
// Fallback to mock prediction if API fails
const riskScore = calculateRiskScore(formData);
const isAnomaly = riskScore > 0.5;
setPrediction({
isAnomaly,
riskScore,
confidence: 0.85,
factors: getTopFactors(formData, isAnomaly),
});
} finally {
setLoading(false);
}
};
const calculateRiskScore = (data) => {
let score = 0;
// Low assessment scores
if (data.avg_score < 40) score += 0.3;
else if (data.avg_score < 50) score += 0.15;
// Low engagement
if (data.total_clicks < 300) score += 0.2;
if (data.num_interactions < 8) score += 0.15;
// Few assessments
if (data.num_assessments < 4) score += 0.15;
// Previous attempts
if (data.num_of_prev_attempts > 0) score += 0.1 * data.num_of_prev_attempts;
// Late submissions
if (data.avg_submission_date > 150) score += 0.15;
return Math.min(score, 1);
};
const getTopFactors = (data, isAnomaly) => {
const factors = [];
if (data.avg_score < 50) {
factors.push({
name: "Low Average Score",
impact: "high",
value: data.avg_score,
});
}
if (data.total_clicks < 400) {
factors.push({
name: "Low Engagement",
impact: "high",
value: data.total_clicks,
});
}
if (data.num_assessments < 5) {
factors.push({
name: "Few Assessments",
impact: "medium",
value: data.num_assessments,
});
}
if (data.num_of_prev_attempts > 0) {
factors.push({
name: "Previous Attempts",
impact: "medium",
value: data.num_of_prev_attempts,
});
}
return factors.slice(0, 4);
};
const handleInputChange = (e) => {
const { name, value } = e.target;
setFormData((prev) => ({
...prev,
[name]: isNaN(value) ? value : parseFloat(value) || 0,
}));
};
const loadSampleData = (type) => {
if (type === "risk") {
setFormData({
...formData,
avg_score: 35,
total_clicks: 200,
num_interactions: 5,
num_assessments: 3,
avg_submission_date: 180,
num_of_prev_attempts: 2,
});
} else {
setFormData({
...formData,
avg_score: 75,
total_clicks: 800,
num_interactions: 15,
num_assessments: 8,
avg_submission_date: 90,
num_of_prev_attempts: 0,
});
}
};
return (
<div className="min-h-screen bg-gradient-to-br from-blue-50 via-white to-purple-50 p-6">
<div className="max-w-6xl mx-auto">
{/* Header */}
<div className="bg-white rounded-2xl shadow-lg p-8 mb-6">
<div className="flex items-center justify-between">
<div>
<h1 className="text-4xl font-bold text-gray-800 mb-2">
Student At-Risk Detection System
</h1>
<p className="text-gray-600">
AI-powered early warning system for online education
</p>
</div>
<Activity className="w-16 h-16 text-blue-600" />
</div>
<div className="grid grid-cols-3 gap-4 mt-6">
<div className="bg-blue-50 rounded-lg p-4">
<Users className="w-8 h-8 text-blue-600 mb-2" />
<div className="text-2xl font-bold text-gray-800">28,785</div>
<div className="text-sm text-gray-600">Students Analyzed</div>
</div>
<div className="bg-green-50 rounded-lg p-4">
<TrendingUp className="w-8 h-8 text-green-600 mb-2" />
<div className="text-2xl font-bold text-gray-800">89.4%</div>
<div className="text-sm text-gray-600">Model Accuracy</div>
</div>
<div className="bg-purple-50 rounded-lg p-4">
<CheckCircle className="w-8 h-8 text-purple-600 mb-2" />
<div className="text-2xl font-bold text-gray-800">
Isolation Forest
</div>
<div className="text-sm text-gray-600">Best Model</div>
</div>
</div>
</div>
<div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
{/* Input Form */}
<div className="lg:col-span-2 bg-white rounded-2xl shadow-lg p-6">
<div className="flex items-center justify-between mb-6">
<h2 className="text-2xl font-bold text-gray-800">
Student Information
</h2>
<div className="flex gap-2">
<button
onClick={() => loadSampleData("risk")}
className="px-3 py-1 text-sm bg-red-100 text-red-700 rounded-lg hover:bg-red-200"
>
Load At-Risk Sample
</button>
<button
onClick={() => loadSampleData("normal")}
className="px-3 py-1 text-sm bg-green-100 text-green-700 rounded-lg hover:bg-green-200"
>
Load Normal Sample
</button>
</div>
</div>
{/* Tabs */}
<div className="flex gap-2 mb-6 border-b">
{["basic", "academic", "engagement"].map((tab) => (
<button
key={tab}
onClick={() => setActiveTab(tab)}
className={`px-4 py-2 font-medium capitalize transition-colors ${
activeTab === tab
? "text-blue-600 border-b-2 border-blue-600"
: "text-gray-500 hover:text-gray-700"
}`}
>
{tab}
</button>
))}
</div>
{/* Basic Info Tab */}
{activeTab === "basic" && (
<div className="grid grid-cols-2 gap-4">
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Module Code
</label>
<select
name="code_module"
value={formData.code_module}
onChange={handleInputChange}
className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
>
<option value="AAA">AAA</option>
<option value="BBB">BBB</option>
<option value="CCC">CCC</option>
<option value="DDD">DDD</option>
</select>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Gender
</label>
<select
name="gender"
value={formData.gender}
onChange={handleInputChange}
className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
>
<option value="M">Male</option>
<option value="F">Female</option>
</select>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Education Level
</label>
<select
name="highest_education"
value={formData.highest_education}
onChange={handleInputChange}
className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
>
<option value="Lower Than A Level">
Lower Than A Level
</option>
<option value="A Level or Equivalent">
A Level or Equivalent
</option>
<option value="HE Qualification">HE Qualification</option>
<option value="Post Graduate Qualification">
Post Graduate
</option>
</select>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Age Band
</label>
<select
name="age_band"
value={formData.age_band}
onChange={handleInputChange}
className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
>
<option value="0-35">0-35</option>
<option value="35-55">35-55</option>
<option value="55<=">55+</option>
</select>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Disability
</label>
<select
name="disability"
value={formData.disability}
onChange={handleInputChange}
className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
>
<option value="N">No</option>
<option value="Y">Yes</option>
</select>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Studied Credits
</label>
<input
type="number"
name="studied_credits"
value={formData.studied_credits}
onChange={handleInputChange}
className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
/>
</div>
</div>
)}
{/* Academic Performance Tab */}
{activeTab === "academic" && (
<div className="grid grid-cols-2 gap-4">
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Average Score (0-100)
</label>
<input
type="number"
name="avg_score"
value={formData.avg_score}
onChange={handleInputChange}
min="0"
max="100"
className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Number of Assessments
</label>
<input
type="number"
name="num_assessments"
value={formData.num_assessments}
onChange={handleInputChange}
min="0"
className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Minimum Score
</label>
<input
type="number"
name="min_score"
value={formData.min_score}
onChange={handleInputChange}
min="0"
max="100"
className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Maximum Score
</label>
<input
type="number"
name="max_score"
value={formData.max_score}
onChange={handleInputChange}
min="0"
max="100"
className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Previous Attempts
</label>
<input
type="number"
name="num_of_prev_attempts"
value={formData.num_of_prev_attempts}
onChange={handleInputChange}
min="0"
className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Avg Submission Date
</label>
<input
type="number"
name="avg_submission_date"
value={formData.avg_submission_date}
onChange={handleInputChange}
className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
/>
</div>
</div>
)}
{/* Engagement Tab */}
{activeTab === "engagement" && (
<div className="grid grid-cols-2 gap-4">
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Total Clicks
</label>
<input
type="number"
name="total_clicks"
value={formData.total_clicks}
onChange={handleInputChange}
min="0"
className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Average Clicks
</label>
<input
type="number"
name="avg_clicks"
value={formData.avg_clicks}
onChange={handleInputChange}
min="0"
className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Number of Interactions
</label>
<input
type="number"
name="num_interactions"
value={formData.num_interactions}
onChange={handleInputChange}
min="0"
className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Maximum Clicks
</label>
<input
type="number"
name="max_clicks"
value={formData.max_clicks}
onChange={handleInputChange}
min="0"
className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
First Access Day
</label>
<input
type="number"
name="first_access"
value={formData.first_access}
onChange={handleInputChange}
className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Last Access Day
</label>
<input
type="number"
name="last_access"
value={formData.last_access}
onChange={handleInputChange}
className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
/>
</div>
</div>
)}
<button
onClick={predictAnomaly}
disabled={loading}
className="w-full mt-6 bg-gradient-to-r from-blue-600 to-purple-600 text-white py-3 rounded-lg font-semibold hover:from-blue-700 hover:to-purple-700 transition-all disabled:opacity-50 disabled:cursor-not-allowed"
>
{loading ? "Analyzing..." : "Predict Risk Level"}
</button>
</div>
{/* Results Panel */}
<div className="space-y-6">
{prediction && (
<>
<div
className={`bg-white rounded-2xl shadow-lg p-6 ${
prediction.isAnomaly
? "ring-2 ring-red-500"
: "ring-2 ring-green-500"
}`}
>
<div className="flex items-center justify-between mb-4">
<h3 className="text-xl font-bold text-gray-800">
Prediction
</h3>
{prediction.isAnomaly ? (
<AlertCircle className="w-8 h-8 text-red-500" />
) : (
<CheckCircle className="w-8 h-8 text-green-500" />
)}
</div>
<div
className={`text-center py-6 rounded-xl ${
prediction.isAnomaly ? "bg-red-50" : "bg-green-50"
}`}
>
<div
className={`text-4xl font-bold mb-2 ${
prediction.isAnomaly ? "text-red-600" : "text-green-600"
}`}
>
{prediction.isAnomaly ? "AT RISK" : "ON TRACK"}
</div>
<div className="text-gray-600">
Risk Score: {(prediction.riskScore * 100).toFixed(1)}%
</div>
<div className="text-sm text-gray-500 mt-1">
Confidence: {(prediction.confidence * 100).toFixed(1)}%
</div>
</div>
<div className="mt-4">
<div className="text-sm font-medium text-gray-700 mb-2">
Risk Level
</div>
<div className="w-full bg-gray-200 rounded-full h-3">
<div
className={`h-3 rounded-full transition-all ${
prediction.riskScore > 0.7
? "bg-red-500"
: prediction.riskScore > 0.4
? "bg-yellow-500"
: "bg-green-500"
}`}
style={{ width: `${prediction.riskScore * 100}%` }}
/>
</div>
</div>
</div>
<div className="bg-white rounded-2xl shadow-lg p-6">
<h3 className="text-xl font-bold text-gray-800 mb-4">
Key Factors
</h3>
<div className="space-y-3">
{prediction.factors.map((factor, idx) => (
<div
key={idx}
className="border-l-4 border-blue-500 pl-3"
>
<div className="flex items-center justify-between">
<div className="font-medium text-gray-800">
{factor.name}
</div>
<span
className={`text-xs px-2 py-1 rounded ${
factor.impact === "high"
? "bg-red-100 text-red-700"
: factor.impact === "medium"
? "bg-yellow-100 text-yellow-700"
: "bg-blue-100 text-blue-700"
}`}
>
{factor.impact}
</span>
</div>
<div className="text-sm text-gray-600">
Value: {factor.value}
</div>
</div>
))}
</div>
</div>
{prediction.isAnomaly && (
<div className="bg-orange-50 border border-orange-200 rounded-2xl p-6">
<h3 className="text-lg font-bold text-orange-800 mb-3">
Recommendations
</h3>
<ul className="space-y-2 text-sm text-orange-900">
<li className="flex items-start">
<span className="mr-2">•</span>
<span>
Schedule one-on-one meeting with academic advisor
</span>
</li>
<li className="flex items-start">
<span className="mr-2">•</span>
<span>
Provide additional learning resources and support
</span>
</li>
<li className="flex items-start">
<span className="mr-2">•</span>
<span>Monitor engagement metrics weekly</span>
</li>
<li className="flex items-start">
<span className="mr-2">•</span>
<span>Consider peer mentoring or study groups</span>
</li>
</ul>
</div>
)}
</>
)}
{!prediction && (
<div className="bg-white rounded-2xl shadow-lg p-8 text-center">
<Upload className="w-16 h-16 text-gray-400 mx-auto mb-4" />
<p className="text-gray-600">
Enter student information and click "Predict Risk Level" to
analyze
</p>
</div>
)}
</div>
</div>
{/* Footer */}
<div className="mt-6 bg-white rounded-2xl shadow-lg p-6">
<div className="text-center text-sm text-gray-600">
<p className="font-semibold mb-2">Model Information</p>
<p>
Algorithm: Isolation Forest (Tuned) | Accuracy: 89.4% | F1-Score:
0.8741
</p>
<p className="mt-2 text-xs">
Dataset: Open University Learning Analytics (28,785 students)
</p>
</div>
</div>
</div>
</div>
);
};
export default StudentAnomalyDetector;