omarkashif commited on
Commit
b23a22e
·
verified ·
1 Parent(s): d25a58c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -18
app.py CHANGED
@@ -2,7 +2,7 @@ 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
6
  import os
7
 
8
  class AttendanceTracker:
@@ -13,13 +13,14 @@ class AttendanceTracker:
13
  self.token = HfFolder.get_token()
14
 
15
  def load_original_attendance_list(self, course):
16
- # Load the CSV file using datasets package
17
- dataset = load_dataset(self.dataset_id, data_files={course: f"{course}.csv"},split=f"{course}")
 
18
  attendance_list = [entry['Name'].lower() for entry in dataset]
19
- return attendance_list
20
 
21
  def mark_attendance(self, attendance_file, course):
22
- original_attendance_list = self.load_original_attendance_list(course)
23
 
24
  # Read attendance file
25
  attendance_list = []
@@ -38,22 +39,24 @@ class AttendanceTracker:
38
  if name.split()[0] in attendance_dict:
39
  attendance_dict[name.split()[0]] = "Present"
40
 
41
- # Download the dataset
42
- dataset = load_dataset(self.dataset_id, data_files={course: f"{course}.csv"}, split=course)
43
-
44
- # Update the rows with attendance data
45
  updated_rows = []
 
 
 
 
 
 
46
  for entry in dataset:
47
- row = [entry['Name']]
48
- if entry['Name'].split()[0].lower() in attendance_dict:
49
- row.append(attendance_dict[entry['Name'].split()[0].lower()])
50
- else:
51
- row.append("Absent")
52
  updated_rows.append(row)
53
 
54
  # Save the updated attendance list locally
55
- updated_file_path = f"{course}.csv"
56
- with open(updated_file_path, "w", newline="") as f:
57
  writer = csv.writer(f)
58
  writer.writerow(["Name", current_date])
59
  writer.writerows(updated_rows)
@@ -61,7 +64,7 @@ class AttendanceTracker:
61
  # Upload the updated file back to the Hugging Face dataset
62
  api = HfApi()
63
  api.upload_file(
64
- path_or_fileobj=updated_file_path,
65
  path_in_repo=f"{course}.csv",
66
  repo_id=self.repo_id,
67
  repo_type="dataset",
@@ -85,4 +88,4 @@ iface = gr.Interface(
85
  description="Upload a CSV file of today's attendance to update the attendance file for the selected course."
86
  )
87
 
88
- iface.launch()
 
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:
 
13
  self.token = HfFolder.get_token()
14
 
15
  def load_original_attendance_list(self, course):
16
+ # Download the CSV file using datasets package
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
+ dataset = load_dataset(self.dataset_id, data_files={course: local_file_path}, split=course)
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
+ original_attendance_list, local_file_path = self.load_original_attendance_list(course)
24
 
25
  # Read attendance file
26
  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
+ # Check if the dataset already has a column for today's date
47
+ existing_columns = dataset.column_names
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(["Name", current_date])
62
  writer.writerows(updated_rows)
 
64
  # Upload the updated file back to the Hugging Face dataset
65
  api = HfApi()
66
  api.upload_file(
67
+ path_or_fileobj=local_file_path,
68
  path_in_repo=f"{course}.csv",
69
  repo_id=self.repo_id,
70
  repo_type="dataset",
 
88
  description="Upload a CSV file of today's attendance to update the attendance file for the selected course."
89
  )
90
 
91
+ iface.launch()