Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,138 +1,82 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import smtplib
|
| 3 |
-
from email.mime.text import MIMEText
|
| 4 |
-
from email.mime.multipart import MIMEMultipart
|
| 5 |
-
import sqlite3
|
| 6 |
-
from datetime import datetime
|
| 7 |
import pandas as pd
|
| 8 |
-
|
| 9 |
-
import
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
cursor = conn.cursor()
|
| 15 |
-
|
| 16 |
-
cursor.execute('''CREATE TABLE IF NOT EXISTS users (
|
| 17 |
-
id INTEGER PRIMARY KEY,
|
| 18 |
-
email TEXT UNIQUE,
|
| 19 |
-
password TEXT,
|
| 20 |
-
verified INTEGER DEFAULT 0
|
| 21 |
-
)''')
|
| 22 |
-
|
| 23 |
-
cursor.execute('''CREATE TABLE IF NOT EXISTS attendance (
|
| 24 |
-
id INTEGER PRIMARY KEY,
|
| 25 |
-
email TEXT,
|
| 26 |
-
date TEXT,
|
| 27 |
-
status TEXT
|
| 28 |
-
)''')
|
| 29 |
|
| 30 |
# Helper Functions
|
| 31 |
-
def
|
| 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 |
-
hashed_new_password = generate_password_hash(new_password)
|
| 77 |
-
cursor.execute("UPDATE users SET password = ? WHERE email = ?", (hashed_new_password, email))
|
| 78 |
-
conn.commit()
|
| 79 |
-
return "Password changed successfully!"
|
| 80 |
-
return "Incorrect old password."
|
| 81 |
-
|
| 82 |
-
# Log Attendance
|
| 83 |
-
def log_attendance(email, status):
|
| 84 |
-
date = datetime.now().strftime("%Y-%m-%d")
|
| 85 |
-
cursor.execute("INSERT INTO attendance (email, date, status) VALUES (?, ?, ?)", (email, date, status))
|
| 86 |
-
conn.commit()
|
| 87 |
-
return "Attendance logged!"
|
| 88 |
-
|
| 89 |
-
# Generate Monthly Fees Report
|
| 90 |
-
def calculate_fees(email):
|
| 91 |
-
current_month = datetime.now().strftime("%Y-%m")
|
| 92 |
-
cursor.execute("SELECT COUNT(*) FROM attendance WHERE email = ? AND status = 'Present' AND date LIKE ?", (email, f"{current_month}%"))
|
| 93 |
-
count_present = cursor.fetchone()[0]
|
| 94 |
-
total_fees = count_present * 66.67
|
| 95 |
-
|
| 96 |
-
# Send email with fees details
|
| 97 |
-
send_email(email, "Monthly Fees", f"You attended {count_present} classes. Your total fees for the month: ${total_fees:.2f}")
|
| 98 |
-
|
| 99 |
-
return f"Fees calculated and email sent: ${total_fees:.2f}"
|
| 100 |
|
| 101 |
# Gradio Interface
|
| 102 |
-
def
|
| 103 |
-
|
| 104 |
-
result = cursor.fetchone()
|
| 105 |
-
if result and check_password_hash(result[0], password):
|
| 106 |
-
if result[1] == 1:
|
| 107 |
-
return f"Welcome {email}!", gr.update(visible=True), email
|
| 108 |
-
else:
|
| 109 |
-
return "Account not verified. Check your email.", gr.update(visible=False), None
|
| 110 |
-
return "Invalid credentials.", gr.update(visible=False), None
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
gr.Markdown("# Attendance Tracker")
|
| 118 |
|
| 119 |
with gr.Row():
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
gr.Markdown("### Log Attendance")
|
| 130 |
-
email_display = gr.Textbox(label="Email", interactive=False)
|
| 131 |
-
status = gr.Radio(["Present", "Absent"], label="Status")
|
| 132 |
-
log_button = gr.Button("Log Attendance")
|
| 133 |
-
attendance_message = gr.Textbox(label="Message", interactive=False)
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
| 137 |
|
| 138 |
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
+
import os
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
|
| 6 |
+
# Attendance Tracker Setup
|
| 7 |
+
if not os.path.exists("attendance_records"):
|
| 8 |
+
os.makedirs("attendance_records")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Helper Functions
|
| 11 |
+
def log_attendance(name, day, date, status):
|
| 12 |
+
month = datetime.strptime(date, "%Y-%m-%d").strftime("%Y-%m")
|
| 13 |
+
file_path = f"attendance_records/{month}.csv"
|
| 14 |
+
|
| 15 |
+
# Load or create attendance sheet for the month
|
| 16 |
+
if os.path.exists(file_path):
|
| 17 |
+
df = pd.read_csv(file_path)
|
| 18 |
+
else:
|
| 19 |
+
df = pd.DataFrame(columns=["Name", "Day", "Date", "Status"])
|
| 20 |
+
|
| 21 |
+
# Add new attendance record
|
| 22 |
+
new_entry = {"Name": name, "Day": day, "Date": date, "Status": status}
|
| 23 |
+
df = df.append(new_entry, ignore_index=True)
|
| 24 |
+
|
| 25 |
+
# Save back to the file
|
| 26 |
+
df.to_csv(file_path, index=False)
|
| 27 |
+
return "Attendance logged successfully!"
|
| 28 |
+
|
| 29 |
+
def calculate_fees():
|
| 30 |
+
attendance_summaries = []
|
| 31 |
+
|
| 32 |
+
for file in os.listdir("attendance_records"):
|
| 33 |
+
if file.endswith(".csv"):
|
| 34 |
+
file_path = os.path.join("attendance_records", file)
|
| 35 |
+
df = pd.read_csv(file_path)
|
| 36 |
+
|
| 37 |
+
# Calculate fees for each candidate
|
| 38 |
+
fees_summary = df[df["Status"] == "Present"].groupby("Name").size() * (1000 / 12) # Example: Monthly fees divided by 12
|
| 39 |
+
fees_summary.name = "Fees"
|
| 40 |
+
|
| 41 |
+
# Merge fees into the attendance sheet
|
| 42 |
+
df = df.merge(fees_summary, on="Name", how="left")
|
| 43 |
+
|
| 44 |
+
# Save updated file
|
| 45 |
+
df.to_csv(file_path, index=False)
|
| 46 |
+
|
| 47 |
+
# Summarize for all candidates
|
| 48 |
+
attendance_summaries.append(df[["Name", "Fees"]].drop_duplicates())
|
| 49 |
+
|
| 50 |
+
if attendance_summaries:
|
| 51 |
+
summary_df = pd.concat(attendance_summaries).drop_duplicates()
|
| 52 |
+
summary_df.to_csv("attendance_records/fees_summary.csv", index=False)
|
| 53 |
+
return "Fees calculated and updated successfully!"
|
| 54 |
+
|
| 55 |
+
return "No attendance records found for fees calculation."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# Gradio Interface
|
| 58 |
+
def submit_attendance(name, day, date, status):
|
| 59 |
+
return log_attendance(name, day, date, status)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
def generate_fees():
|
| 62 |
+
return calculate_fees()
|
| 63 |
|
| 64 |
+
with gr.Blocks() as app:
|
| 65 |
+
gr.Markdown("# Attendance Tracker")
|
|
|
|
| 66 |
|
| 67 |
with gr.Row():
|
| 68 |
+
name = gr.Textbox(label="Name")
|
| 69 |
+
day = gr.Textbox(label="Day")
|
| 70 |
+
date = gr.Textbox(label="Date (YYYY-MM-DD)")
|
| 71 |
+
status = gr.Radio(["Present", "Absent"], label="Status")
|
| 72 |
|
| 73 |
+
submit_button = gr.Button("Submit Attendance")
|
| 74 |
+
submit_message = gr.Textbox(label="Message", interactive=False)
|
| 75 |
|
| 76 |
+
calculate_button = gr.Button("Calculate Fees")
|
| 77 |
+
calculate_message = gr.Textbox(label="Fees Calculation Message", interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
submit_button.click(submit_attendance, inputs=[name, day, date, status], outputs=[submit_message])
|
| 80 |
+
calculate_button.click(generate_fees, outputs=[calculate_message])
|
| 81 |
|
| 82 |
app.launch()
|