Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,184 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import streamlit as st
|
3 |
+
import os
|
4 |
+
import pandas as pd
|
5 |
+
from PIL import Image
|
6 |
+
import io
|
7 |
+
import datetime
|
8 |
+
from huggingface_hub import HfApi, hf_hub_download, upload_file
|
9 |
+
from attendance_system import AttendanceSystem
|
10 |
+
from record_attendance import record_attendance_new
|
11 |
+
from huggingface_hub import create_repo
|
12 |
+
|
13 |
+
# Function to create a new Hugging Face repository
|
14 |
+
def create_new_hf_repo(repo_id, repo_type="model", private=True):
|
15 |
+
try:
|
16 |
+
create_repo(repo_id, repo_type=repo_type, private=private)
|
17 |
+
st.success(f"New Hugging Face repository created: {repo_id}")
|
18 |
+
return repo_id
|
19 |
+
except Exception as e:
|
20 |
+
st.error(f"Failed to create Hugging Face repository: {e}")
|
21 |
+
return None
|
22 |
+
|
23 |
+
# Add a new section in your Streamlit app
|
24 |
+
st.header("Create New Hugging Face Repository")
|
25 |
+
|
26 |
+
# Set the repository details
|
27 |
+
new_repo_id = "PRIYANSHUDHAKED/attendance-embeddings"
|
28 |
+
repo_type = "model"
|
29 |
+
is_private = True
|
30 |
+
|
31 |
+
# Create the new repository
|
32 |
+
new_repo_id = create_new_hf_repo(new_repo_id, repo_type=repo_type, private=is_private)
|
33 |
+
if new_repo_id:
|
34 |
+
# Update the HF_REPO_ID variable with the new repository ID
|
35 |
+
HF_REPO_ID = new_repo_id
|
36 |
+
|
37 |
+
|
38 |
+
# Set up AttendanceSystem
|
39 |
+
DATABASE_DIR = "database"
|
40 |
+
os.makedirs(DATABASE_DIR, exist_ok=True)
|
41 |
+
attendance_system = AttendanceSystem(DATABASE_DIR)
|
42 |
+
|
43 |
+
# Function to save embeddings to Hugging Face
|
44 |
+
def save_embeddings_to_hf(roll_number, embedding):
|
45 |
+
filename = f"{roll_number}_embedding.npy"
|
46 |
+
with io.BytesIO() as buffer:
|
47 |
+
np.save(buffer, embedding)
|
48 |
+
buffer.seek(0)
|
49 |
+
upload_file(
|
50 |
+
path_or_fileobj=buffer,
|
51 |
+
path_in_repo=f"embeddings/{filename}",
|
52 |
+
repo_id=HF_REPO_ID,
|
53 |
+
repo_type=HF_REPO_TYPE,
|
54 |
+
)
|
55 |
+
|
56 |
+
# Function to load embeddings from Hugging Face
|
57 |
+
def load_embeddings_from_hf():
|
58 |
+
embeddings = {}
|
59 |
+
files = hf_api.list_repo_files(repo_id=HF_REPO_ID, repo_type=HF_REPO_TYPE)
|
60 |
+
for file in files:
|
61 |
+
if file.startswith("embeddings/") and file.endswith("_embedding.npy"):
|
62 |
+
roll_number = file.split("/")[-1].split("_")[0]
|
63 |
+
file_content = hf_hub_download(repo_id=HF_REPO_ID, filename=file, repo_type=HF_REPO_TYPE)
|
64 |
+
embedding = np.load(file_content)
|
65 |
+
embeddings[roll_number] = embedding
|
66 |
+
return embeddings
|
67 |
+
|
68 |
+
# Function to save CSV to Hugging Face
|
69 |
+
def save_csv_to_hf(subject):
|
70 |
+
filename = f"{subject}.csv"
|
71 |
+
upload_file(
|
72 |
+
path_or_fileobj=filename,
|
73 |
+
path_in_repo=f"attendance/{filename}",
|
74 |
+
repo_id=HF_REPO_ID,
|
75 |
+
repo_type=HF_REPO_TYPE,
|
76 |
+
)
|
77 |
+
|
78 |
+
# Function to load CSV from Hugging Face
|
79 |
+
def load_csv_from_hf(subject):
|
80 |
+
filename = f"{subject}.csv"
|
81 |
+
try:
|
82 |
+
file_content = hf_hub_download(repo_id=HF_REPO_ID, filename=f"attendance/{filename}", repo_type=HF_REPO_TYPE)
|
83 |
+
return pd.read_csv(file_content)
|
84 |
+
except:
|
85 |
+
return None
|
86 |
+
|
87 |
+
# Load embeddings from Hugging Face
|
88 |
+
attendance_system.database = load_embeddings_from_hf()
|
89 |
+
|
90 |
+
st.title("Attendance System")
|
91 |
+
|
92 |
+
# Sidebar for navigation
|
93 |
+
page = st.sidebar.selectbox("Choose a page", ["Take Attendance", "Add to Database", "View Attendance"])
|
94 |
+
|
95 |
+
if page == "Take Attendance":
|
96 |
+
st.header("Take Attendance")
|
97 |
+
|
98 |
+
# Course selection
|
99 |
+
courses = [f.split('.')[0] for f in hf_api.list_repo_files(repo_id=HF_REPO_ID, repo_type=HF_REPO_TYPE) if f.endswith('.csv')]
|
100 |
+
course = st.selectbox("Select Course", courses)
|
101 |
+
|
102 |
+
# Date selection
|
103 |
+
date = st.date_input("Select Date", datetime.date.today())
|
104 |
+
|
105 |
+
# Image upload
|
106 |
+
uploaded_file = st.file_uploader("Upload Classroom Image", type=["jpg", "jpeg", "png"])
|
107 |
+
|
108 |
+
if st.button("Take Attendance"):
|
109 |
+
if uploaded_file is not None:
|
110 |
+
# Save the uploaded image temporarily
|
111 |
+
image = Image.open(uploaded_file)
|
112 |
+
img_path = "temp_classroom.jpg"
|
113 |
+
image.save(img_path)
|
114 |
+
|
115 |
+
# Process attendance
|
116 |
+
present_students = attendance_system.record_attendance(course, str(date), img_path)
|
117 |
+
|
118 |
+
# Display results
|
119 |
+
st.subheader("Present Students:")
|
120 |
+
st.write(", ".join(present_students))
|
121 |
+
|
122 |
+
# Option to mark attendance in sheet
|
123 |
+
if st.button("Mark Attendance in Sheet"):
|
124 |
+
record_attendance_new(present_students, course)
|
125 |
+
save_csv_to_hf(course)
|
126 |
+
st.success("Attendance marked successfully!")
|
127 |
+
|
128 |
+
# Clean up temporary file
|
129 |
+
os.remove(img_path)
|
130 |
+
else:
|
131 |
+
st.error("Please upload an image.")
|
132 |
+
|
133 |
+
elif page == "Add to Database":
|
134 |
+
st.header("Add to Database")
|
135 |
+
|
136 |
+
roll_number = st.text_input("Enter Roll Number")
|
137 |
+
uploaded_file = st.file_uploader("Upload Student Image", type=["jpg", "jpeg", "png"])
|
138 |
+
|
139 |
+
if st.button("Add to Database"):
|
140 |
+
if uploaded_file is not None and roll_number:
|
141 |
+
# Save the uploaded image temporarily
|
142 |
+
image = Image.open(uploaded_file)
|
143 |
+
img_path = f"temp_{roll_number}.jpg"
|
144 |
+
image.save(img_path)
|
145 |
+
|
146 |
+
# Add to database
|
147 |
+
if attendance_system.add_to_database(roll_number, img_path):
|
148 |
+
# Save embeddings to Hugging Face
|
149 |
+
embedding = attendance_system.database[roll_number]
|
150 |
+
save_embeddings_to_hf(roll_number, embedding)
|
151 |
+
st.success("Student added to database successfully!")
|
152 |
+
else:
|
153 |
+
st.error("Failed to add student to database.")
|
154 |
+
|
155 |
+
# Clean up temporary file
|
156 |
+
os.remove(img_path)
|
157 |
+
else:
|
158 |
+
st.error("Please enter a roll number and upload an image.")
|
159 |
+
|
160 |
+
elif page == "View Attendance":
|
161 |
+
st.header("View Attendance")
|
162 |
+
|
163 |
+
# Course selection for viewing attendance
|
164 |
+
courses = [f.split('.')[0] for f in hf_api.list_repo_files(repo_id=HF_REPO_ID, repo_type=HF_REPO_TYPE) if f.endswith('.csv')]
|
165 |
+
selected_course = st.selectbox("Select Course to View Attendance", courses)
|
166 |
+
|
167 |
+
if selected_course:
|
168 |
+
# Read the CSV file from Hugging Face
|
169 |
+
df = load_csv_from_hf(selected_course)
|
170 |
+
|
171 |
+
if df is not None:
|
172 |
+
# Display the attendance data
|
173 |
+
st.dataframe(df)
|
174 |
+
|
175 |
+
# Option to download the CSV
|
176 |
+
csv = df.to_csv(index=False)
|
177 |
+
st.download_button(
|
178 |
+
label="Download CSV",
|
179 |
+
data=csv,
|
180 |
+
file_name=f"{selected_course}_attendance.csv",
|
181 |
+
mime="text/csv",
|
182 |
+
)
|
183 |
+
else:
|
184 |
+
st.error("Failed to load attendance data.")
|