omarkashif commited on
Commit
c828dfb
·
verified ·
1 Parent(s): 0c64309

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -26
app.py CHANGED
@@ -1,24 +1,27 @@
1
  import gradio as gr
2
- from gradio import Interface
3
  import csv
4
  from datetime import date
 
 
5
 
6
  class AttendanceTracker:
7
- def __init__(self):
8
- self.original_attendance_list = []
9
- self.attendance_file_path = ""
 
10
 
11
- # Load original attendance list
12
- self.load_original_attendance_list()
13
-
14
- def load_original_attendance_list(self):
15
- # Load original attendance list from a file (e.g. original_attendance_list.csv)
16
- with open("original_attendance.csv", "r") as f:
17
  reader = csv.reader(f)
18
  for row in reader:
19
- self.original_attendance_list.append(row[0].lower())
 
 
 
 
20
 
21
- def mark_attendance(self, attendance_file):
22
  # Read attendance file
23
  attendance_list = []
24
  with open(attendance_file.name, "r") as f:
@@ -29,7 +32,7 @@ class AttendanceTracker:
29
  current_date = date.today().strftime("%d-%m-%Y")
30
 
31
  # Create a dictionary to store attendance
32
- attendance_dict = {name.split()[0]: "Absent" for name in self.original_attendance_list if len(name.split()) > 0}
33
 
34
  # Mark present students
35
  for name in attendance_list:
@@ -37,7 +40,8 @@ class AttendanceTracker:
37
  attendance_dict[name.split()[0]] = "Present"
38
 
39
  # Save marked attendance to the original file
40
- with open("original_attendance.csv", "r") as f:
 
41
  reader = csv.reader(f)
42
  rows = list(reader)
43
 
@@ -46,29 +50,44 @@ class AttendanceTracker:
46
 
47
  # Update the rows with attendance data
48
  for row in range(1, len(rows)):
49
- if rows[row] and len(rows[row][0].split()) > 0 and rows[row][0].split()[0].lower() in attendance_dict:
50
- rows[row].append(attendance_dict[rows[row][0].split()[0].lower()])
51
- else:
52
- rows[row].append("Absent")
 
53
 
54
  # Save the updated attendance list
55
- with open("original_attendance.csv", "w", newline="") as f:
56
  writer = csv.writer(f)
57
  writer.writerows(rows)
58
 
59
- return "Attendance marked successfully!"
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
- tracker = AttendanceTracker()
 
62
 
63
- def upload_and_mark(file):
64
- return tracker.mark_attendance(file)
65
 
66
- iface = Interface(
67
  fn=upload_and_mark,
68
- inputs=gr.File(label="Upload Attendance File (CSV)"),
69
  outputs=gr.Textbox(label="Status"),
70
  title="Attendance Tracker",
71
- description="Upload a CSV file of today's attendance to update the original attendance file."
72
  )
73
 
74
  iface.launch()
 
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
  class AttendanceTracker:
8
+ def __init__(self, repo_id):
9
+ self.courses = ["AI", "DS", "ML"] # List of courses
10
+ self.repo_id = repo_id
11
+ self.token = os.getenv("HF_TOKEN") # Fetch the token from environment variables
12
 
13
+ def load_original_attendance_list(self, course):
14
+ attendance_list = []
15
+ file_path = f"attendance/{course}.csv"
16
+ with open(file_path, "r") as f:
 
 
17
  reader = csv.reader(f)
18
  for row in reader:
19
+ attendance_list.append(row[0].lower())
20
+ return attendance_list
21
+
22
+ def mark_attendance(self, attendance_file, course):
23
+ original_attendance_list = self.load_original_attendance_list(course)
24
 
 
25
  # Read attendance file
26
  attendance_list = []
27
  with open(attendance_file.name, "r") as f:
 
32
  current_date = date.today().strftime("%d-%m-%Y")
33
 
34
  # Create a dictionary to store attendance
35
+ attendance_dict = {name.split()[0]: "Absent" for name in original_attendance_list if len(name.split()) > 0}
36
 
37
  # Mark present students
38
  for name in attendance_list:
 
40
  attendance_dict[name.split()[0]] = "Present"
41
 
42
  # Save marked attendance to the original file
43
+ file_path = f"attendance/{course}.csv"
44
+ with open(file_path, "r") as f:
45
  reader = csv.reader(f)
46
  rows = list(reader)
47
 
 
50
 
51
  # Update the rows with attendance data
52
  for row in range(1, len(rows)):
53
+ if rows[row] and len(rows[row][0].split()) > 0:
54
+ if rows[row][0].split()[0].lower() in attendance_dict:
55
+ rows[row].append(attendance_dict[rows[row][0].split()[0].lower()])
56
+ else:
57
+ rows[row].append("Absent")
58
 
59
  # Save the updated attendance list
60
+ with open(file_path, "w", newline="") as f:
61
  writer = csv.writer(f)
62
  writer.writerows(rows)
63
 
64
+ # Push the updated file to the repository
65
+ self.push_to_hf(course)
66
+
67
+ return f"Attendance marked successfully for {course}!"
68
+
69
+ def push_to_hf(self, course):
70
+ # Push updated CSV to Hugging Face repository
71
+ api = HfApi()
72
+ repo = Repository(local_dir=".", clone_from=self.repo_id, use_auth_token=self.token)
73
+
74
+ file_path = f"attendance/{course}.csv"
75
+ repo.git_add(file_path)
76
+ repo.git_commit(f"Update {course} attendance")
77
+ repo.git_push()
78
 
79
+ repo_id = "omarkashif/attendanceSystem"
80
+ tracker = AttendanceTracker(repo_id)
81
 
82
+ def upload_and_mark(file, course):
83
+ return tracker.mark_attendance(file, course)
84
 
85
+ iface = gr.Interface(
86
  fn=upload_and_mark,
87
+ inputs=[gr.File(label="Upload Attendance File (CSV)"), gr.Dropdown(choices=["AI", "DS", "ML"], label="Select Course")],
88
  outputs=gr.Textbox(label="Status"),
89
  title="Attendance Tracker",
90
+ description="Upload a CSV file of today's attendance to update the attendance file for the selected course."
91
  )
92
 
93
  iface.launch()