omarkashif commited on
Commit
33bd9ab
·
verified ·
1 Parent(s): 455397a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -22
app.py CHANGED
@@ -1,8 +1,7 @@
1
  import gradio as gr
2
  import csv
3
  from datetime import date
4
- from datasets import load_dataset
5
- from huggingface_hub import HfApi, HfFolder, hf_hub_download
6
  import os
7
 
8
  class AttendanceTracker:
@@ -17,6 +16,20 @@ class AttendanceTracker:
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
 
@@ -27,9 +40,8 @@ class AttendanceTracker:
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 = []
@@ -37,27 +49,23 @@ class AttendanceTracker:
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:
@@ -66,8 +74,7 @@ class AttendanceTracker:
66
  writer.writerows(updated_rows)
67
 
68
  # Upload the updated file back to the Hugging Face dataset
69
- api = HfApi()
70
- api.upload_file(
71
  path_or_fileobj=local_file_path,
72
  path_in_repo=f"{course}.csv",
73
  repo_id=self.repo_id,
@@ -92,4 +99,4 @@ iface = gr.Interface(
92
  description="Upload a CSV file of today's attendance to update the attendance file for the selected course."
93
  )
94
 
95
- iface.launch()
 
1
  import gradio as gr
2
  import csv
3
  from datetime import date
4
+ from huggingface_hub import HfApi, HfFolder, hf_hub_download, upload_file
 
5
  import os
6
 
7
  class AttendanceTracker:
 
16
  local_file_path = hf_hub_download(repo_id=self.dataset_id, filename=f"{course}.csv", repo_type="dataset", use_auth_token=self.token)
17
  return local_file_path
18
 
19
+ def get_new_column_name(self, existing_header, current_date):
20
+ # Determine if the current date is already in the header and generate a new column name if needed
21
+ if current_date not in existing_header:
22
+ return current_date
23
+
24
+ # Create a new column name with a suffix
25
+ base_name = current_date
26
+ suffix = 1
27
+ new_column_name = f"{base_name}({suffix})"
28
+ while new_column_name in existing_header:
29
+ suffix += 1
30
+ new_column_name = f"{base_name}({suffix})"
31
+ return new_column_name
32
+
33
  def mark_attendance(self, attendance_file, course):
34
  local_file_path = self.load_original_attendance_list(course)
35
 
 
40
  header = next(reader)
41
  for row in reader:
42
  if row:
43
+ name = row[0].lower() # Convert to lower case for case-insensitive matching
44
+ existing_data[name] = row[1:] # Store all existing columns for this name
 
45
 
46
  # Read the new attendance file
47
  new_attendance_list = []
 
49
  reader = csv.reader(f)
50
  new_attendance_list = [row[0].lower() for row in reader if row]
51
 
52
+ # Get current date and prepare the new column name
53
  current_date = date.today().strftime("%d-%m-%Y")
54
+ new_column_name = self.get_new_column_name(header, current_date)
 
 
 
 
 
 
 
 
55
 
56
  # Prepare updated data including the new date column
57
  updated_rows = []
58
  updated_header = header[:]
59
+ if new_column_name not in updated_header:
60
+ updated_header.append(new_column_name)
61
 
62
  for name, statuses in existing_data.items():
63
+ # Append new status for the new column
64
+ if name in new_attendance_list:
65
+ statuses.append("Present")
66
+ else:
67
+ statuses.append("Absent")
68
+ updated_rows.append([name] + statuses)
69
 
70
  # Save the updated attendance list locally
71
  with open(local_file_path, "w", newline="") as f:
 
74
  writer.writerows(updated_rows)
75
 
76
  # Upload the updated file back to the Hugging Face dataset
77
+ upload_file(
 
78
  path_or_fileobj=local_file_path,
79
  path_in_repo=f"{course}.csv",
80
  repo_id=self.repo_id,
 
99
  description="Upload a CSV file of today's attendance to update the attendance file for the selected course."
100
  )
101
 
102
+ iface.launch()