Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,52 +13,56 @@ class AttendanceTracker:
|
|
| 13 |
self.token = HfFolder.get_token()
|
| 14 |
|
| 15 |
def load_original_attendance_list(self, course):
|
| 16 |
-
# Download the CSV file using
|
| 17 |
local_file_path = hf_hub_download(repo_id=self.dataset_id, filename=f"{course}.csv", repo_type="dataset", use_auth_token=self.token)
|
| 18 |
-
|
| 19 |
-
attendance_list = [entry['Name'].lower() for entry in dataset]
|
| 20 |
-
return attendance_list, local_file_path
|
| 21 |
|
| 22 |
def mark_attendance(self, attendance_file, course):
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
#
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
with open(attendance_file.name, "r") as f:
|
| 28 |
reader = csv.reader(f)
|
| 29 |
-
|
| 30 |
|
| 31 |
# Get current date
|
| 32 |
current_date = date.today().strftime("%d-%m-%Y")
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
for name in attendance_list:
|
| 39 |
-
if name.split()[0] in attendance_dict:
|
| 40 |
-
attendance_dict[name.split()[0]] = "Present"
|
| 41 |
-
|
| 42 |
-
# Load the dataset and prepare updated data
|
| 43 |
-
dataset = load_dataset(self.dataset_id, data_files={course: local_file_path}, split=course)
|
| 44 |
updated_rows = []
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
if current_date not in existing_columns:
|
| 49 |
-
existing_columns.append(current_date)
|
| 50 |
-
|
| 51 |
-
for entry in dataset:
|
| 52 |
-
name = entry['Name']
|
| 53 |
-
row = [name]
|
| 54 |
-
# Mark attendance
|
| 55 |
-
row.append(attendance_dict.get(name.split()[0].lower(), "Absent"))
|
| 56 |
updated_rows.append(row)
|
| 57 |
|
| 58 |
# Save the updated attendance list locally
|
| 59 |
with open(local_file_path, "w", newline="") as f:
|
| 60 |
writer = csv.writer(f)
|
| 61 |
-
writer.writerow(
|
| 62 |
writer.writerows(updated_rows)
|
| 63 |
|
| 64 |
# Upload the updated file back to the Hugging Face dataset
|
|
|
|
| 13 |
self.token = HfFolder.get_token()
|
| 14 |
|
| 15 |
def load_original_attendance_list(self, course):
|
| 16 |
+
# Download the CSV file using hf_hub_download
|
| 17 |
local_file_path = hf_hub_download(repo_id=self.dataset_id, filename=f"{course}.csv", repo_type="dataset", use_auth_token=self.token)
|
| 18 |
+
return local_file_path
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def mark_attendance(self, attendance_file, course):
|
| 21 |
+
local_file_path = self.load_original_attendance_list(course)
|
| 22 |
+
|
| 23 |
+
# Load existing data from CSV
|
| 24 |
+
existing_data = {}
|
| 25 |
+
with open(local_file_path, "r") as f:
|
| 26 |
+
reader = csv.reader(f)
|
| 27 |
+
header = next(reader)
|
| 28 |
+
for row in reader:
|
| 29 |
+
if row:
|
| 30 |
+
name = row[0]
|
| 31 |
+
statuses = row[1:]
|
| 32 |
+
existing_data[name] = statuses
|
| 33 |
+
|
| 34 |
+
# Read the new attendance file
|
| 35 |
+
new_attendance_list = []
|
| 36 |
with open(attendance_file.name, "r") as f:
|
| 37 |
reader = csv.reader(f)
|
| 38 |
+
new_attendance_list = [row[0].lower() for row in reader if row]
|
| 39 |
|
| 40 |
# Get current date
|
| 41 |
current_date = date.today().strftime("%d-%m-%Y")
|
| 42 |
|
| 43 |
+
# Update existing records with new attendance data
|
| 44 |
+
for name in new_attendance_list:
|
| 45 |
+
name_lower = name.lower()
|
| 46 |
+
if name_lower in existing_data:
|
| 47 |
+
existing_data[name_lower].append("Present")
|
| 48 |
+
else:
|
| 49 |
+
# Add new record for new student
|
| 50 |
+
existing_data[name_lower] = ["Absent"] * (len(header) - 1) + ["Present"]
|
| 51 |
|
| 52 |
+
# Prepare updated data including the new date column
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
updated_rows = []
|
| 54 |
+
updated_header = header[:]
|
| 55 |
+
if current_date not in header:
|
| 56 |
+
updated_header.append(current_date)
|
| 57 |
|
| 58 |
+
for name, statuses in existing_data.items():
|
| 59 |
+
row = [name] + statuses
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
updated_rows.append(row)
|
| 61 |
|
| 62 |
# Save the updated attendance list locally
|
| 63 |
with open(local_file_path, "w", newline="") as f:
|
| 64 |
writer = csv.writer(f)
|
| 65 |
+
writer.writerow(updated_header)
|
| 66 |
writer.writerows(updated_rows)
|
| 67 |
|
| 68 |
# Upload the updated file back to the Hugging Face dataset
|