Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import sqlite3
|
| 3 |
+
import datetime
|
| 4 |
+
|
| 5 |
+
# Create or connect to the database
|
| 6 |
+
conn = sqlite3.connect("attendance.db")
|
| 7 |
+
c = conn.cursor()
|
| 8 |
+
|
| 9 |
+
# Create necessary tables
|
| 10 |
+
c.execute('''CREATE TABLE IF NOT EXISTS users (
|
| 11 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 12 |
+
username TEXT UNIQUE,
|
| 13 |
+
password TEXT,
|
| 14 |
+
role TEXT,
|
| 15 |
+
device_id TEXT
|
| 16 |
+
)''')
|
| 17 |
+
|
| 18 |
+
c.execute('''CREATE TABLE IF NOT EXISTS attendance (
|
| 19 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 20 |
+
username TEXT,
|
| 21 |
+
date TEXT,
|
| 22 |
+
time TEXT,
|
| 23 |
+
session TEXT
|
| 24 |
+
)''')
|
| 25 |
+
conn.commit()
|
| 26 |
+
|
| 27 |
+
# Auth functions
|
| 28 |
+
def register_user(username, password, role):
|
| 29 |
+
try:
|
| 30 |
+
c.execute("INSERT INTO users (username, password, role) VALUES (?, ?, ?)", (username, password, role))
|
| 31 |
+
conn.commit()
|
| 32 |
+
return "User registered successfully."
|
| 33 |
+
except sqlite3.IntegrityError:
|
| 34 |
+
return "Username already exists."
|
| 35 |
+
|
| 36 |
+
def login_user(username, password):
|
| 37 |
+
c.execute("SELECT * FROM users WHERE username=? AND password=?", (username, password))
|
| 38 |
+
return c.fetchone()
|
| 39 |
+
|
| 40 |
+
def set_device(username, device_id):
|
| 41 |
+
c.execute("UPDATE users SET device_id=? WHERE username=?", (device_id, username))
|
| 42 |
+
conn.commit()
|
| 43 |
+
return "Device registered."
|
| 44 |
+
|
| 45 |
+
def check_device(username, device_id):
|
| 46 |
+
c.execute("SELECT device_id FROM users WHERE username=?", (username,))
|
| 47 |
+
result = c.fetchone()
|
| 48 |
+
return result and result[0] == device_id
|
| 49 |
+
|
| 50 |
+
# Attendance marking
|
| 51 |
+
def mark_attendance(username, session):
|
| 52 |
+
date = datetime.date.today().isoformat()
|
| 53 |
+
time = datetime.datetime.now().strftime("%H:%M:%S")
|
| 54 |
+
c.execute("INSERT INTO attendance (username, date, time, session) VALUES (?, ?, ?, ?)", (username, date, time, session))
|
| 55 |
+
conn.commit()
|
| 56 |
+
return f"Attendance marked at {time} for session: {session}"
|
| 57 |
+
|
| 58 |
+
# View attendance dashboard
|
| 59 |
+
def view_attendance():
|
| 60 |
+
c.execute("SELECT * FROM attendance ORDER BY date DESC, time DESC")
|
| 61 |
+
records = c.fetchall()
|
| 62 |
+
return "\n".join([f"{r[1]} | {r[2]} | {r[3]} | {r[4]}" for r in records])
|
| 63 |
+
|
| 64 |
+
# Gradio UI
|
| 65 |
+
with gr.Blocks() as app:
|
| 66 |
+
gr.Markdown("# Attendance System (No Firebase)")
|
| 67 |
+
|
| 68 |
+
with gr.Tab("Register"):
|
| 69 |
+
reg_username = gr.Text(label="Username")
|
| 70 |
+
reg_password = gr.Text(label="Password", type="password")
|
| 71 |
+
reg_role = gr.Radio(["teacher", "student"], label="Role")
|
| 72 |
+
reg_button = gr.Button("Register")
|
| 73 |
+
reg_output = gr.Textbox(label="Result")
|
| 74 |
+
reg_button.click(fn=register_user, inputs=[reg_username, reg_password, reg_role], outputs=reg_output)
|
| 75 |
+
|
| 76 |
+
with gr.Tab("Login & Mark Attendance"):
|
| 77 |
+
log_username = gr.Text(label="Username")
|
| 78 |
+
log_password = gr.Text(label="Password", type="password")
|
| 79 |
+
log_device = gr.Text(label="Device ID")
|
| 80 |
+
session_input = gr.Text(label="Session ID")
|
| 81 |
+
login_button = gr.Button("Login & Mark")
|
| 82 |
+
login_output = gr.Textbox(label="Result")
|
| 83 |
+
|
| 84 |
+
def login_and_mark(username, password, device_id, session):
|
| 85 |
+
user = login_user(username, password)
|
| 86 |
+
if not user:
|
| 87 |
+
return "Login failed."
|
| 88 |
+
if not user[4]:
|
| 89 |
+
set_device(username, device_id)
|
| 90 |
+
return mark_attendance(username, session)
|
| 91 |
+
elif check_device(username, device_id):
|
| 92 |
+
return mark_attendance(username, session)
|
| 93 |
+
else:
|
| 94 |
+
return "Unauthorized device."
|
| 95 |
+
|
| 96 |
+
login_button.click(fn=login_and_mark, inputs=[log_username, log_password, log_device, session_input], outputs=login_output)
|
| 97 |
+
|
| 98 |
+
with gr.Tab("Dashboard (Teacher Only)"):
|
| 99 |
+
dash_username = gr.Text(label="Teacher Username")
|
| 100 |
+
dash_password = gr.Text(label="Password", type="password")
|
| 101 |
+
dash_button = gr.Button("View Attendance")
|
| 102 |
+
dash_output = gr.Textbox(label="Attendance Records", lines=20)
|
| 103 |
+
|
| 104 |
+
def dashboard(username, password):
|
| 105 |
+
user = login_user(username, password)
|
| 106 |
+
if user and user[3] == "teacher":
|
| 107 |
+
return view_attendance()
|
| 108 |
+
return "Access denied."
|
| 109 |
+
|
| 110 |
+
dash_button.click(fn=dashboard, inputs=[dash_username, dash_password], outputs=dash_output)
|
| 111 |
+
|
| 112 |
+
app.launch()
|