File size: 2,985 Bytes
dbd94cd
 
657654b
dbd94cd
f06ef27
dbd94cd
657654b
 
 
 
 
 
dbd94cd
657654b
dbd94cd
 
657654b
d2ca3a7
657654b
dbd94cd
657654b
dbd94cd
 
 
d2ca3a7
657654b
 
dbd94cd
657654b
 
 
 
 
 
dbd94cd
d2ca3a7
657654b
 
 
dbd94cd
f06ef27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dbd94cd
657654b
 
 
 
 
 
f06ef27
657654b
 
d2ca3a7
657654b
 
d2ca3a7
657654b
 
 
 
d2ca3a7
657654b
 
 
f06ef27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
657654b
 
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
import gradio as gr
import sqlite3
from PIL import Image
import os
import pandas as pd

# Database setup
DB_PATH = "students.db"
os.makedirs("uploads", exist_ok=True)

def setup_database():
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()
    cursor.execute("""
        CREATE TABLE IF NOT EXISTS students (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            name TEXT,
            age INTEGER,
            image_path TEXT
        )
    """)
    conn.commit()
    conn.close()

def register_student(name, age, image_file):
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()

    # Save image to the uploads directory
    if image_file is not None:
        image_path = f"uploads/{name.replace(' ', '_')}_{os.path.basename(image_file)}"
        os.rename(image_file, image_path)  # Move file to uploads directory
    else:
        image_path = None

    cursor.execute("INSERT INTO students (name, age, image_path) VALUES (?, ?, ?)", (name, age, image_path))
    conn.commit()
    conn.close()
    return f"Student {name} registered successfully!"

def view_students():
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()
    cursor.execute("SELECT id, name, age, image_path FROM students")
    records = cursor.fetchall()
    conn.close()

    # Format data for display
    if records:
        data = []
        for record in records:
            student_id, name, age, image_path = record
            if image_path and os.path.exists(image_path):
                image = Image.open(image_path)
            else:
                image = None
            data.append({"ID": student_id, "Name": name, "Age": age, "Image": image})
        return pd.DataFrame(data).drop(columns=["Image"]).to_string(), data  # Display text table without images
    else:
        return "No records found.", None

# Initialize the database
setup_database()

# Gradio Interface
with gr.Blocks() as demo:
    with gr.Row():
        gr.Markdown("### Student Registration")

    with gr.Row():
        name_input = gr.Textbox(label="Name", placeholder="Enter the student's name")
        age_input = gr.Number(label="Age", precision=0)
        image_input = gr.Image(label="Upload Image", type="filepath")

    register_button = gr.Button("Register Student")
    output = gr.Textbox(label="Status")

    register_button.click(
        register_student,
        inputs=[name_input, age_input, image_input],
        outputs=output,
    )

    with gr.Row():
        gr.Markdown("### View Registered Students")

    view_button = gr.Button("View Records")
    record_output = gr.Textbox(label="Student Records")
    image_output = gr.Image(label="Images", type="pil", visible=False)  # For displaying images if needed

    def view_records_handler():
        records, images = view_students()
        return records

    view_button.click(
        view_records_handler,
        inputs=[],
        outputs=record_output,
    )

# Launch the Gradio app
demo.launch()