File size: 4,392 Bytes
019b229
 
 
33bd9ab
c828dfb
019b229
 
0f123ba
71737df
cb40099
0f123ba
 
019b229
c828dfb
a64bbd1
b23a22e
a64bbd1
c828dfb
33bd9ab
 
 
 
 
 
 
 
 
 
 
 
 
 
c828dfb
a64bbd1
 
 
e178465
a64bbd1
 
 
e178465
 
6a3795c
4c35bcb
a64bbd1
6a3795c
 
 
a64bbd1
 
019b229
 
3cd1b2f
 
3a0dd1c
3cd1b2f
019b229
6a3795c
 
 
33bd9ab
019b229
33bd9ab
e178465
 
 
 
33bd9ab
 
e178465
4c35bcb
33bd9ab
4c35bcb
 
 
0fdcb0e
0f123ba
b23a22e
0eedc9c
e178465
 
0f123ba
 
33bd9ab
b23a22e
0f123ba
 
 
 
 
c828dfb
 
 
baab874
0f123ba
 
019b229
c828dfb
 
019b229
c828dfb
019b229
71737df
019b229
 
c828dfb
019b229
 
04c22f4
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
import gradio as gr
import csv
from datetime import date
from huggingface_hub import HfApi, HfFolder, hf_hub_download, upload_file
import os

class AttendanceTracker:
    def __init__(self, dataset_id, repo_id):
        self.courses = ["AI", "DS", "ML","DA Gray","DA Black","DA White","DS6","DS7 Blue","DS7 Green"]
        self.dataset_id = dataset_id
        self.repo_id = repo_id
        self.token = HfFolder.get_token()

    def load_original_attendance_list(self, course):
        # Download the CSV file using hf_hub_download
        local_file_path = hf_hub_download(repo_id=self.dataset_id, filename=f"{course}.csv", repo_type="dataset", use_auth_token=self.token)
        return local_file_path

    def get_new_column_name(self, existing_header, current_date):
        # Determine if the current date is already in the header and generate a new column name if needed
        if current_date not in existing_header:
            return current_date
        
        # Create a new column name with a suffix
        base_name = current_date
        suffix = 1
        new_column_name = f"{base_name}({suffix})"
        while new_column_name in existing_header:
            suffix += 1
            new_column_name = f"{base_name}({suffix})"
        return new_column_name

    def mark_attendance(self, attendance_file, course):
        local_file_path = self.load_original_attendance_list(course)
        
        # Load existing data from CSV
        existing_data = {}
        with open(local_file_path, "r") as f:
            reader = csv.reader(f)
            header = next(reader)
            for row in reader:
                if row:
                    name = row[0].strip()  # Preserve original case and remove any leading/trailing spaces
                    existing_data[name.lower()] = row  # Store the entire row with the original case

        # Debug: Print existing names
        print("Existing Names:", existing_data.keys())

        # Read the new attendance file
        new_attendance_list = []
        with open(attendance_file.name, "r") as f:
            reader = csv.reader(f)
            for row in reader:
                if row:
                    combined_name = f"{row[0].strip()} {row[1].strip()}".lower().lstrip().rstrip()  # Combine first and last name and make it lowercase for comparison
                    new_attendance_list.append(combined_name)

        # Debug: Print new attendance names
        print("New Attendance Names:", new_attendance_list)

        # Get current date and prepare the new column name
        current_date = date.today().strftime("%d-%m-%Y")
        new_column_name = self.get_new_column_name(header, current_date)

        # Prepare updated data including the new date column
        updated_rows = []
        updated_header = header[:]
        if new_column_name not in updated_header:
            updated_header.append(new_column_name)

        for name_lower, row in existing_data.items():
            # Append new status for the new column
            status = "Present" if name_lower in new_attendance_list else "Absent"
            row.append(status)
            updated_rows.append(row)

        # Save the updated attendance list locally
        with open(local_file_path, "w", newline="") as f:
            writer = csv.writer(f)
            writer.writerow(updated_header)
            writer.writerows(updated_rows)

        # Upload the updated file back to the Hugging Face dataset
        upload_file(
            path_or_fileobj=local_file_path,
            path_in_repo=f"{course}.csv",
            repo_id=self.repo_id,
            repo_type="dataset",
            token=self.token
        )

        return f"Attendance marked successfully for {course}!"

dataset_id = "omarkashif/attendace-ML"
repo_id = "omarkashif/attendace-ML"
tracker = AttendanceTracker(dataset_id, repo_id)

def upload_and_mark(file, course):
    return tracker.mark_attendance(file, course)

iface = gr.Interface(
    fn=upload_and_mark,
    inputs=[gr.File(label="Upload Attendance File (CSV)"), gr.Dropdown(choices=["AI", "DS", "ML","DA Gray","DA Black","DA White","DS6","DS7 Blue","DS7 Green"], label="Select Course")],
    outputs=gr.Textbox(label="Status"),
    title="Attendance Tracker",
    description="Upload a CSV file of today's attendance to update the attendance file for the selected course."
)

iface.launch()