Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,93 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import sqlite3
|
| 3 |
import datetime
|
|
|
|
| 4 |
|
| 5 |
# Create or connect to the database
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
def set_device(username, device_id):
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
def check_device(username, device_id):
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
# Attendance marking
|
| 51 |
def mark_attendance(username, session):
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
# View attendance dashboard
|
| 59 |
def view_attendance():
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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")
|
|
@@ -78,20 +102,20 @@ with gr.Blocks() as app:
|
|
| 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 |
|
|
@@ -105,7 +129,7 @@ with gr.Blocks() as app:
|
|
| 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 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import sqlite3
|
| 3 |
import datetime
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
# Create or connect to the database
|
| 7 |
+
def init_db():
|
| 8 |
+
conn = sqlite3.connect("attendance.db")
|
| 9 |
+
c = conn.cursor()
|
| 10 |
+
|
| 11 |
+
# Create tables if they don't exist
|
| 12 |
+
c.execute('''CREATE TABLE IF NOT EXISTS users (
|
| 13 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 14 |
+
username TEXT UNIQUE,
|
| 15 |
+
password TEXT,
|
| 16 |
+
role TEXT,
|
| 17 |
+
device_id TEXT
|
| 18 |
+
)''')
|
| 19 |
+
|
| 20 |
+
c.execute('''CREATE TABLE IF NOT EXISTS attendance (
|
| 21 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 22 |
+
username TEXT,
|
| 23 |
+
date TEXT,
|
| 24 |
+
time TEXT,
|
| 25 |
+
session TEXT
|
| 26 |
+
)''')
|
| 27 |
+
conn.commit()
|
| 28 |
+
return conn, c
|
| 29 |
+
|
| 30 |
+
conn, c = init_db()
|
| 31 |
|
| 32 |
# Auth functions
|
| 33 |
def register_user(username, password, role):
|
| 34 |
try:
|
| 35 |
c.execute("INSERT INTO users (username, password, role) VALUES (?, ?, ?)", (username, password, role))
|
| 36 |
conn.commit()
|
| 37 |
+
return "β
User registered successfully."
|
| 38 |
except sqlite3.IntegrityError:
|
| 39 |
+
return "β Username already exists."
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return f"β Registration error: {str(e)}"
|
| 42 |
|
| 43 |
def login_user(username, password):
|
| 44 |
+
try:
|
| 45 |
+
c.execute("SELECT * FROM users WHERE username=? AND password=?", (username, password))
|
| 46 |
+
return c.fetchone()
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return None
|
| 49 |
|
| 50 |
def set_device(username, device_id):
|
| 51 |
+
try:
|
| 52 |
+
c.execute("UPDATE users SET device_id=? WHERE username=?", (device_id, username))
|
| 53 |
+
conn.commit()
|
| 54 |
+
return "Device registered."
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return f"β Device registration error: {str(e)}"
|
| 57 |
|
| 58 |
def check_device(username, device_id):
|
| 59 |
+
try:
|
| 60 |
+
c.execute("SELECT device_id FROM users WHERE username=?", (username,))
|
| 61 |
+
result = c.fetchone()
|
| 62 |
+
return result and result[0] == device_id
|
| 63 |
+
except:
|
| 64 |
+
return False
|
| 65 |
|
| 66 |
# Attendance marking
|
| 67 |
def mark_attendance(username, session):
|
| 68 |
+
try:
|
| 69 |
+
date = datetime.date.today().isoformat()
|
| 70 |
+
time = datetime.datetime.now().strftime("%H:%M:%S")
|
| 71 |
+
c.execute("INSERT INTO attendance (username, date, time, session) VALUES (?, ?, ?, ?)", (username, date, time, session))
|
| 72 |
+
conn.commit()
|
| 73 |
+
return f"β
Attendance marked at {time} for session: {session}"
|
| 74 |
+
except Exception as e:
|
| 75 |
+
return f"β Attendance error: {str(e)}"
|
| 76 |
|
| 77 |
# View attendance dashboard
|
| 78 |
def view_attendance():
|
| 79 |
+
try:
|
| 80 |
+
c.execute("SELECT * FROM attendance ORDER BY date DESC, time DESC")
|
| 81 |
+
records = c.fetchall()
|
| 82 |
+
if not records:
|
| 83 |
+
return "No attendance records yet."
|
| 84 |
+
return "\n".join([f"{r[1]} | {r[2]} | {r[3]} | {r[4]}" for r in records])
|
| 85 |
+
except Exception as e:
|
| 86 |
+
return f"β Error fetching attendance: {str(e)}"
|
| 87 |
|
| 88 |
# Gradio UI
|
| 89 |
with gr.Blocks() as app:
|
| 90 |
+
gr.Markdown("# π Attendance System (No Firebase, Local DB)")
|
| 91 |
|
| 92 |
with gr.Tab("Register"):
|
| 93 |
reg_username = gr.Text(label="Username")
|
|
|
|
| 102 |
log_password = gr.Text(label="Password", type="password")
|
| 103 |
log_device = gr.Text(label="Device ID")
|
| 104 |
session_input = gr.Text(label="Session ID")
|
| 105 |
+
login_button = gr.Button("Login & Mark Attendance")
|
| 106 |
login_output = gr.Textbox(label="Result")
|
| 107 |
|
| 108 |
def login_and_mark(username, password, device_id, session):
|
| 109 |
user = login_user(username, password)
|
| 110 |
if not user:
|
| 111 |
+
return "β Login failed. Invalid credentials."
|
| 112 |
if not user[4]:
|
| 113 |
set_device(username, device_id)
|
| 114 |
return mark_attendance(username, session)
|
| 115 |
elif check_device(username, device_id):
|
| 116 |
return mark_attendance(username, session)
|
| 117 |
else:
|
| 118 |
+
return "β Unauthorized device."
|
| 119 |
|
| 120 |
login_button.click(fn=login_and_mark, inputs=[log_username, log_password, log_device, session_input], outputs=login_output)
|
| 121 |
|
|
|
|
| 129 |
user = login_user(username, password)
|
| 130 |
if user and user[3] == "teacher":
|
| 131 |
return view_attendance()
|
| 132 |
+
return "β Access denied. Only teachers can view this."
|
| 133 |
|
| 134 |
dash_button.click(fn=dashboard, inputs=[dash_username, dash_password], outputs=dash_output)
|
| 135 |
|