omarkashif commited on
Commit
cb40099
·
verified ·
1 Parent(s): 7056e7a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -20
app.py CHANGED
@@ -1,31 +1,31 @@
1
  import gradio as gr
2
  import csv
3
  from datetime import date
4
- from huggingface_hub import HfApi, Repository
5
  import os
6
 
7
- repo_id = "omarkashif/attendanceSystem"
8
- repo_type = "space"
9
-
10
  class AttendanceTracker:
11
- def __init__(self, repo_id, repo_type="space"):
12
  self.courses = ["AI", "DS", "ML"] # List of courses
13
  self.repo_id = repo_id
 
14
  self.repo_type = repo_type
15
 
16
  self.token = os.getenv("HF_TOKEN") # Fetch the token from environment variables
17
 
18
  def load_original_attendance_list(self, course):
 
 
 
19
  attendance_list = []
20
- file_path = f"{course}.csv"
21
  with open(file_path, "r") as f:
22
  reader = csv.reader(f)
23
  for row in reader:
24
  attendance_list.append(row[0].lower())
25
- return attendance_list
26
 
27
  def mark_attendance(self, attendance_file, course):
28
- original_attendance_list = self.load_original_attendance_list(course)
29
 
30
  # Read attendance file
31
  attendance_list = []
@@ -45,7 +45,6 @@ class AttendanceTracker:
45
  attendance_dict[name.split()[0]] = "Present"
46
 
47
  # Save marked attendance to the original file
48
- file_path = f"{course}.csv"
49
  with open(file_path, "r") as f:
50
  reader = csv.reader(f)
51
  rows = list(reader)
@@ -66,13 +65,12 @@ class AttendanceTracker:
66
  writer = csv.writer(f)
67
  writer.writerows(rows)
68
 
69
- # Push the updated file to the repository
70
- self.push_to_hf(course)
71
 
72
  return f"Attendance marked successfully for {course}!"
73
 
74
-
75
- def push_to_hf(self, course):
76
  # Push updated CSV to Hugging Face repository
77
  api = HfApi()
78
  repo = Repository(
@@ -82,17 +80,13 @@ class AttendanceTracker:
82
  use_auth_token=self.token
83
  )
84
 
85
- file_path = f"{course}.csv"
86
  repo.git_add(file_path)
87
  repo.git_commit(f"Update {course} attendance")
88
  repo.git_push()
89
 
90
-
91
-
92
-
93
-
94
- repo_id = "omarkashif/attendanceSystem"
95
- tracker = AttendanceTracker(repo_id)
96
 
97
  def upload_and_mark(file, course):
98
  return tracker.mark_attendance(file, course)
 
1
  import gradio as gr
2
  import csv
3
  from datetime import date
4
+ from huggingface_hub import HfApi, Repository, hf_hub_download
5
  import os
6
 
 
 
 
7
  class AttendanceTracker:
8
+ def __init__(self, repo_id, dataset_id, repo_type="space"):
9
  self.courses = ["AI", "DS", "ML"] # List of courses
10
  self.repo_id = repo_id
11
+ self.dataset_id = dataset_id
12
  self.repo_type = repo_type
13
 
14
  self.token = os.getenv("HF_TOKEN") # Fetch the token from environment variables
15
 
16
  def load_original_attendance_list(self, course):
17
+ # Download the CSV file from the Hugging Face dataset
18
+ file_path = hf_hub_download(repo_id=self.dataset_id, filename=f"{course}.csv")
19
+
20
  attendance_list = []
 
21
  with open(file_path, "r") as f:
22
  reader = csv.reader(f)
23
  for row in reader:
24
  attendance_list.append(row[0].lower())
25
+ return attendance_list, file_path
26
 
27
  def mark_attendance(self, attendance_file, course):
28
+ original_attendance_list, file_path = self.load_original_attendance_list(course)
29
 
30
  # Read attendance file
31
  attendance_list = []
 
45
  attendance_dict[name.split()[0]] = "Present"
46
 
47
  # Save marked attendance to the original file
 
48
  with open(file_path, "r") as f:
49
  reader = csv.reader(f)
50
  rows = list(reader)
 
65
  writer = csv.writer(f)
66
  writer.writerows(rows)
67
 
68
+ # Push the updated file to the Hugging Face dataset
69
+ self.push_to_hf(course, file_path)
70
 
71
  return f"Attendance marked successfully for {course}!"
72
 
73
+ def push_to_hf(self, course, file_path):
 
74
  # Push updated CSV to Hugging Face repository
75
  api = HfApi()
76
  repo = Repository(
 
80
  use_auth_token=self.token
81
  )
82
 
 
83
  repo.git_add(file_path)
84
  repo.git_commit(f"Update {course} attendance")
85
  repo.git_push()
86
 
87
+ repo_id = "omarkashif/attendanceSystem"
88
+ dataset_id = "omarkashif/attendace-ML" # Replace with your actual dataset ID
89
+ tracker = AttendanceTracker(repo_id, dataset_id)
 
 
 
90
 
91
  def upload_and_mark(file, course):
92
  return tracker.mark_attendance(file, course)