File size: 25,735 Bytes
16c6d7c | 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 | import gradio as gr
import os
import pandas as pd
from datetime import datetime
import calendar
from db import Database
from UserDetail import UserDetail
import image
import encdec
import plotly.graph_objects as go
import plotly.express as px
# Initialize database
db = Database()
# Custom CSS for beautiful styling
custom_css = """
.gradio-container {
font-family: 'Arial', sans-serif;
}
.header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
padding: 2rem;
border-radius: 15px;
color: white;
text-align: center;
margin-bottom: 2rem;
}
.success-box {
background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
padding: 1rem;
border-radius: 10px;
color: white;
text-align: center;
}
.error-box {
background: #ff6b6b;
padding: 1rem;
border-radius: 10px;
color: white;
}
"""
# ============================================
# HOME PAGE
# ============================================
def show_home():
"""Display home page with statistics"""
stats = db.get_statistics()
html = f"""
<div style='background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
padding: 2rem; border-radius: 15px; color: white; text-align: center; margin-bottom: 2rem;'>
<h1>π Suthukeny Government High School</h1>
<p style='font-size: 1.2rem;'>AI-Powered Student Management & Performance Analysis System</p>
</div>
<div style='display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; margin: 2rem 0;'>
<div style='background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
padding: 1.5rem; border-radius: 10px; color: white; text-align: center;'>
<h2 style='margin: 0; font-size: 3rem;'>{stats['total_students']}</h2>
<p style='margin: 0.5rem 0 0 0;'>Total Students</p>
</div>
<div style='background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
padding: 1.5rem; border-radius: 10px; color: white; text-align: center;'>
<h2 style='margin: 0; font-size: 3rem;'>{len(stats['class_wise'])}</h2>
<p style='margin: 0.5rem 0 0 0;'>Active Classes</p>
</div>
<div style='background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
padding: 1.5rem; border-radius: 10px; color: white; text-align: center;'>
<h2 style='margin: 0; font-size: 3rem;'>{stats['students_with_marks']}</h2>
<p style='margin: 0.5rem 0 0 0;'>Students with Records</p>
</div>
</div>
<h2>β¨ Key Features</h2>
<ul style='font-size: 1.1rem; line-height: 1.8;'>
<li>π Face Recognition Login - Secure AI authentication</li>
<li>π Complete Student Profiles - Photo ID with full details</li>
<li>π Monthly Performance Tracking - 5 subjects with analytics</li>
<li>π Visual Analytics - Beautiful interactive charts</li>
<li>π Class Rankings - Top performers identification</li>
<li>πΎ Persistent Storage - Data never lost</li>
</ul>
<div style='background: #f0f2f6; padding: 1.5rem; border-radius: 10px; margin-top: 2rem;'>
<h3>π How to Use</h3>
<ol style='line-height: 1.8;'>
<li><strong>Register Student:</strong> Upload photo and enter details</li>
<li><strong>Student Login:</strong> Use face recognition to access profile</li>
<li><strong>Update Marks:</strong> Teachers enter monthly exam scores</li>
<li><strong>View Reports:</strong> Analyze performance with charts</li>
<li><strong>Manage Students:</strong> Edit or update student information</li>
</ol>
</div>
<div style='text-align: center; margin-top: 3rem; padding: 2rem;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 15px; color: white;'>
<h3>Suthukeny Government High School</h3>
<p>Empowering Education Through Technology π</p>
</div>
"""
return html
# ============================================
# STUDENT REGISTRATION
# ============================================
def register_student(photo, name, dob, class_std, section, father_name, mother_name,
address, phone, email, blood_group):
"""Register a new student with face recognition"""
try:
# Validation
if photo is None:
return "β Please upload a student photo", None, None
if not name or not dob or not class_std or not section:
return "β Please fill all required fields (Name, DOB, Class, Section)", None, None
# Generate student ID
student_id = db.generate_student_id(class_std)
# Save image and create face encoding
success, message = image.save_image_from_path(photo, student_id)
if not success:
return f"β {message}", None, None
# Create user detail object
user_detail = UserDetail(
student_id, name, str(dob), class_std, section,
father_name or "", mother_name or "", address or "",
phone or "", email or "", blood_group or ""
)
# Insert into database
success, db_message = db.insert_student(user_detail)
if success:
# Create student card HTML
card_html = f"""
<div style='background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
padding: 2rem; border-radius: 15px; color: white; text-align: center;'>
<h2>β
Registration Successful!</h2>
<h1 style='font-size: 2.5rem; margin: 1rem 0;'>Student ID: {student_id}</h1>
<p style='font-size: 1.2rem;'>Student registered successfully with face recognition!</p>
</div>
<div style='background: white; padding: 1.5rem; border-radius: 10px; margin-top: 1rem; border: 2px solid #667eea;'>
<h3>Student Information</h3>
<p><strong>Name:</strong> {name}</p>
<p><strong>Class:</strong> {class_std}-{section}</p>
<p><strong>DOB:</strong> {dob}</p>
<p><strong>Father:</strong> {father_name}</p>
<p><strong>Mother:</strong> {mother_name}</p>
<p><strong>Blood Group:</strong> {blood_group}</p>
</div>
"""
return card_html, photo, f"Student ID: {student_id}"
else:
image.delete_image(student_id)
return f"β {db_message}", None, None
except Exception as e:
return f"β Error: {str(e)}", None, None
# ============================================
# STUDENT LOGIN
# ============================================
def login_with_face(photo):
"""Login student using face recognition"""
try:
if photo is None:
return "β Please capture your photo", None
# Save temporary image
temp_path = image.save_temp_image_from_path(photo)
if not temp_path:
return "β Failed to process image", None
# Recognize face
is_match, student_id, confidence, message = image.recognize_face(temp_path)
# Clean up
image.delete_temp_image()
if is_match:
# Get student details
student = db.get_student(student_id)
if student:
# Create profile HTML
profile_html = f"""
<div style='background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
padding: 1.5rem; border-radius: 10px; color: white; margin-bottom: 1rem;'>
<h2>π Welcome, {student.student_name}!</h2>
<p><strong>Student ID:</strong> {student.student_id} | <strong>Class:</strong> {student.class_std}-{student.section}</p>
<p><strong>Match Confidence:</strong> {confidence:.1f}%</p>
</div>
<div style='background: white; padding: 1.5rem; border-radius: 10px; border: 2px solid #667eea;'>
<h3>π Personal Information</h3>
<p><strong>Name:</strong> {student.student_name}</p>
<p><strong>Date of Birth:</strong> {student.dob}</p>
<p><strong>Class:</strong> {student.class_std}-{student.section}</p>
<p><strong>Father's Name:</strong> {student.father_name or 'Not specified'}</p>
<p><strong>Mother's Name:</strong> {student.mother_name or 'Not specified'}</p>
<p><strong>Address:</strong> {student.address or 'Not specified'}</p>
<p><strong>Phone:</strong> {student.phone or 'Not specified'}</p>
<p><strong>Email:</strong> {student.email or 'Not specified'}</p>
<p><strong>Blood Group:</strong> {student.blood_group or 'Not specified'}</p>
</div>
"""
# Get marks
marks_records = db.get_student_marks(student_id)
if marks_records:
marks_html = "<div style='margin-top: 1rem;'><h3>π Recent Performance</h3>"
for record in marks_records[:3]: # Show last 3 exams
marks_html += f"""
<div style='background: #f0f2f6; padding: 1rem; border-radius: 8px; margin: 0.5rem 0;'>
<h4>{record[0]} {record[1]}</h4>
<p><strong>Total:</strong> {record[12]}/500 | <strong>Percentage:</strong> {record[13]:.1f}%</p>
<p><strong>{record[2]}:</strong> {record[3]} | <strong>{record[4]}:</strong> {record[5]} |
<strong>{record[6]}:</strong> {record[7]} | <strong>{record[8]}:</strong> {record[9]} |
<strong>{record[10]}:</strong> {record[11]}</p>
</div>
"""
marks_html += "</div>"
profile_html += marks_html
return profile_html, student_id
else:
return "β Student record not found", None
else:
return f"β {message}\n\nπ‘ Tip: Ensure good lighting and face the camera directly", None
except Exception as e:
return f"β Error: {str(e)}", None
# ============================================
# MARKS MANAGEMENT
# ============================================
def get_students_in_class(class_std, section):
"""Get list of students in a class"""
students = db.get_all_students(class_filter=class_std)
class_students = [s for s in students if s[4] == section]
if class_students:
return gr.Dropdown(choices=[f"{s[1]} ({s[0]})" for s in class_students], label="Select Student")
else:
return gr.Dropdown(choices=[], label="No students in this class")
def save_marks(class_std, section, student_display, exam_month, exam_year,
subj1_name, subj1_marks, subj2_name, subj2_marks,
subj3_name, subj3_marks, subj4_name, subj4_marks,
subj5_name, subj5_marks):
"""Save student marks"""
try:
if not student_display:
return "β Please select a student"
# Extract student ID from display string
student_id = student_display.split("(")[-1].strip(")")
# Validate inputs
subject_names = [subj1_name, subj2_name, subj3_name, subj4_name, subj5_name]
marks_list = [subj1_marks, subj2_marks, subj3_marks, subj4_marks, subj5_marks]
if any(not s.strip() for s in subject_names):
return "β Please enter names for all subjects"
# Save marks
success, message = db.insert_marks(
student_id, exam_month, exam_year,
subject_names, marks_list
)
if success:
total = sum(marks_list)
percentage = (total / 500) * 100
return f"""
<div style='background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
padding: 1.5rem; border-radius: 10px; color: white; text-align: center;'>
<h2>β
Marks Saved Successfully!</h2>
<h3>Total: {total}/500 | Percentage: {percentage:.2f}%</h3>
</div>
"""
else:
return f"β {message}"
except Exception as e:
return f"β Error: {str(e)}"
# ============================================
# REPORTS
# ============================================
def generate_class_report(class_std, exam_year):
"""Generate class performance report"""
try:
performance = db.get_class_performance(
class_std,
exam_year=exam_year if exam_year != "All" else None
)
if not performance:
return "π No performance data available for selected filters", None
# Create DataFrame
df = pd.DataFrame(performance, columns=[
'Student ID', 'Name', 'Month', 'Year', 'Total', 'Percentage'
])
# Calculate average
avg_percentage = df['Percentage'].mean()
# Create report HTML
report_html = f"""
<div style='background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
padding: 1.5rem; border-radius: 10px; color: white; text-align: center; margin-bottom: 1rem;'>
<h2>π Class {class_std} Performance Report</h2>
<h3>Average: {avg_percentage:.1f}%</h3>
</div>
"""
# Create bar chart
fig = go.Figure(data=[
go.Bar(
x=df['Name'],
y=df['Percentage'],
marker=dict(
color=df['Percentage'],
colorscale='RdYlGn',
showscale=True
),
text=df['Percentage'].round(1),
textposition='auto',
)
])
fig.update_layout(
title=f"Class {class_std} Student Performance",
xaxis_title="Student",
yaxis_title="Percentage",
height=500
)
return report_html, fig
except Exception as e:
return f"β Error: {str(e)}", None
# ============================================
# ADMIN PANEL
# ============================================
def admin_login(password):
"""Verify admin password"""
if password == encdec.get_admin_password():
stats = db.get_statistics()
html = f"""
<div style='background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
padding: 1.5rem; border-radius: 10px; color: white; text-align: center;'>
<h2>β
Admin Access Granted</h2>
</div>
<div style='margin-top: 1rem; padding: 1.5rem; background: #f0f2f6; border-radius: 10px;'>
<h3>π System Statistics</h3>
<p><strong>Total Students:</strong> {stats['total_students']}</p>
<p><strong>Active Classes:</strong> {len(stats['class_wise'])}</p>
<p><strong>Students with Marks:</strong> {stats['students_with_marks']}</p>
<h4>Class Distribution:</h4>
<ul>
"""
for class_name, count in stats['class_wise']:
html += f"<li>Class {class_name}: {count} students</li>"
html += """
</ul>
</div>
<div style='margin-top: 1rem; padding: 1.5rem; background: #fff3cd; border-radius: 10px; border: 2px solid #ffc107;'>
<h3>β οΈ Data Management</h3>
<p>Use with caution! Data operations are permanent.</p>
</div>
"""
return html
else:
return """
<div style='background: #ff6b6b; padding: 1.5rem; border-radius: 10px; color: white; text-align: center;'>
<h2>β Incorrect Password</h2>
</div>
"""
# ============================================
# CREATE GRADIO INTERFACE
# ============================================
with gr.Blocks(css=custom_css, title="Suthukeny Govt High School", theme=gr.themes.Soft()) as app:
gr.Markdown("""
# π Suthukeny Government High School
## AI-Powered Student Management & Performance Analysis System
""")
with gr.Tabs():
# HOME TAB
with gr.Tab("π Home"):
home_output = gr.HTML(show_home())
gr.Button("π Refresh Statistics").click(
fn=show_home,
outputs=home_output
)
# REGISTRATION TAB
with gr.Tab("π Register Student"):
gr.Markdown("### Register New Student")
gr.Markdown("Upload a clear, front-facing photo of the student")
with gr.Row():
with gr.Column(scale=1):
reg_photo = gr.Image(type="filepath", label="Student Photo", sources=["upload", "webcam"])
with gr.Column(scale=2):
with gr.Group():
gr.Markdown("#### Required Information")
reg_name = gr.Textbox(label="Student Name *", placeholder="Enter full name")
with gr.Row():
reg_dob = gr.Textbox(label="Date of Birth * (YYYY-MM-DD)", placeholder="2010-01-01")
reg_class = gr.Dropdown(choices=["6", "7", "8", "9", "10", "11", "12"], label="Class *")
reg_section = gr.Dropdown(choices=["A", "B", "C"], label="Section *")
with gr.Group():
gr.Markdown("#### Family Information")
with gr.Row():
reg_father = gr.Textbox(label="Father's Name", placeholder="Optional")
reg_mother = gr.Textbox(label="Mother's Name", placeholder="Optional")
with gr.Group():
gr.Markdown("#### Contact Information")
reg_address = gr.Textbox(label="Address", placeholder="Optional", lines=2)
with gr.Row():
reg_phone = gr.Textbox(label="Phone", placeholder="Optional")
reg_email = gr.Textbox(label="Email", placeholder="Optional")
reg_blood = gr.Dropdown(choices=["", "A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-"],
label="Blood Group")
reg_button = gr.Button("β
Register Student", variant="primary", size="lg")
reg_output = gr.HTML()
reg_photo_display = gr.Image(label="Registered Student", visible=False)
reg_id_display = gr.Textbox(label="Student ID", visible=False)
reg_button.click(
fn=register_student,
inputs=[reg_photo, reg_name, reg_dob, reg_class, reg_section,
reg_father, reg_mother, reg_address, reg_phone, reg_email, reg_blood],
outputs=[reg_output, reg_photo_display, reg_id_display]
)
# LOGIN TAB
with gr.Tab("π Student Login"):
gr.Markdown("### Face Recognition Login")
gr.Markdown("πΈ Position your face in the center and click capture")
with gr.Row():
with gr.Column(scale=1):
login_photo = gr.Image(type="filepath", label="Capture Your Face", sources=["webcam"])
login_button = gr.Button("π Login with Face", variant="primary", size="lg")
with gr.Column(scale=2):
login_output = gr.HTML()
login_student_id = gr.Textbox(label="Logged in Student ID", visible=False)
login_button.click(
fn=login_with_face,
inputs=login_photo,
outputs=[login_output, login_student_id]
)
# MARKS ENTRY TAB
with gr.Tab("π Marks Entry"):
gr.Markdown("### Enter Student Marks")
with gr.Row():
marks_class = gr.Dropdown(choices=["6", "7", "8", "9", "10", "11", "12"], label="Class")
marks_section = gr.Dropdown(choices=["A", "B", "C"], label="Section")
marks_student = gr.Dropdown(label="Select Student", choices=[])
# Update student dropdown when class/section changes
marks_class.change(
fn=get_students_in_class,
inputs=[marks_class, marks_section],
outputs=marks_student
)
marks_section.change(
fn=get_students_in_class,
inputs=[marks_class, marks_section],
outputs=marks_student
)
with gr.Row():
marks_month = gr.Dropdown(
choices=list(calendar.month_name)[1:],
label="Exam Month",
value=calendar.month_name[datetime.now().month]
)
marks_year = gr.Dropdown(
choices=[str(y) for y in range(datetime.now().year - 1, datetime.now().year + 2)],
label="Exam Year",
value=str(datetime.now().year)
)
gr.Markdown("#### Enter Marks (Out of 100)")
with gr.Row():
with gr.Column():
subj1_name = gr.Textbox(label="Subject 1 Name", value="Tamil")
subj1_marks = gr.Number(label="Marks", value=0, minimum=0, maximum=100)
with gr.Column():
subj2_name = gr.Textbox(label="Subject 2 Name", value="English")
subj2_marks = gr.Number(label="Marks", value=0, minimum=0, maximum=100)
with gr.Row():
with gr.Column():
subj3_name = gr.Textbox(label="Subject 3 Name", value="Mathematics")
subj3_marks = gr.Number(label="Marks", value=0, minimum=0, maximum=100)
with gr.Column():
subj4_name = gr.Textbox(label="Subject 4 Name", value="Science")
subj4_marks = gr.Number(label="Marks", value=0, minimum=0, maximum=100)
with gr.Row():
with gr.Column():
subj5_name = gr.Textbox(label="Subject 5 Name", value="Social Science")
subj5_marks = gr.Number(label="Marks", value=0, minimum=0, maximum=100)
with gr.Column():
save_marks_btn = gr.Button("πΎ Save Marks", variant="primary", size="lg")
marks_output = gr.HTML()
save_marks_btn.click(
fn=save_marks,
inputs=[marks_class, marks_section, marks_student, marks_month, marks_year,
subj1_name, subj1_marks, subj2_name, subj2_marks,
subj3_name, subj3_marks, subj4_name, subj4_marks,
subj5_name, subj5_marks],
outputs=marks_output
)
# REPORTS TAB
with gr.Tab("π Reports"):
gr.Markdown("### Performance Reports")
with gr.Row():
report_class = gr.Dropdown(choices=["6", "7", "8", "9", "10", "11", "12"],
label="Select Class", value="6")
report_year = gr.Dropdown(choices=["All"] + [str(y) for y in range(datetime.now().year - 2, datetime.now().year + 1)],
label="Exam Year", value=str(datetime.now().year))
report_btn = gr.Button("π Generate Report", variant="primary")
report_output = gr.HTML()
report_chart = gr.Plot()
report_btn.click(
fn=generate_class_report,
inputs=[report_class, report_year],
outputs=[report_output, report_chart]
)
# ADMIN TAB
with gr.Tab("π§ Admin Panel"):
gr.Markdown("### Admin Access")
gr.Markdown("β οΈ Authorized personnel only")
admin_password = gr.Textbox(label="Admin Password", type="password")
admin_login_btn = gr.Button("π Login", variant="primary")
admin_output = gr.HTML()
admin_login_btn.click(
fn=admin_login,
inputs=admin_password,
outputs=admin_output
)
gr.Markdown("""
---
<div style='text-align: center; padding: 1rem; color: #666;'>
<p><strong>Suthukeny Government High School</strong></p>
<p>Empowering Education Through Technology π</p>
</div>
""")
# Launch the app
if __name__ == "__main__":
app.launch(server_name="0.0.0.0", server_port=7860)
|