LovnishVerma commited on
Commit
d4a996d
·
verified ·
1 Parent(s): 2613c58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -7
app.py CHANGED
@@ -9,14 +9,13 @@ from PIL import Image
9
  KNOWN_FACES_DIR = "known_faces"
10
  os.makedirs(KNOWN_FACES_DIR, exist_ok=True)
11
 
12
- # Retrieve the token from the environment
13
- hf_token = os.getenv("token") # Ensure the key matches the name in Hugging Face secrets
14
  if not hf_token:
15
  raise ValueError("Hugging Face token not found. Ensure it's set as a secret in the Hugging Face Space.")
16
 
17
  # Initialize the Hugging Face API
18
  api = HfApi()
19
- api.set_access_token(hf_token)
20
 
21
  # Database setup
22
  DATABASE = "students.db"
@@ -63,7 +62,8 @@ def save_image_to_hugging_face(image, roll_no):
63
  path_or_fileobj=local_path,
64
  path_in_repo=filename,
65
  repo_id=repo_name,
66
- repo_type="dataset"
 
67
  )
68
  st.success(f"Image uploaded to Hugging Face: {filename}")
69
  except Exception as e:
@@ -85,7 +85,11 @@ roll_no = st.text_input("Enter your roll number")
85
  capture_mode = st.radio("Choose an option to upload your image", ["Use Webcam", "Upload File"])
86
 
87
  if capture_mode == "Use Webcam":
88
- picture = st.camera_input("Take a picture")
 
 
 
 
89
 
90
  elif capture_mode == "Upload File":
91
  picture = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
@@ -98,9 +102,9 @@ if st.button("Register"):
98
  st.error("Please upload or capture an image.")
99
  else:
100
  try:
101
- if capture_mode == "Use Webcam":
102
  image = Image.open(picture)
103
- else:
104
  image = Image.open(picture)
105
 
106
  # Save locally and upload to Hugging Face
 
9
  KNOWN_FACES_DIR = "known_faces"
10
  os.makedirs(KNOWN_FACES_DIR, exist_ok=True)
11
 
12
+ # Retrieve the Hugging Face token from the environment
13
+ hf_token = os.getenv("token") # Ensure the key matches the secret name
14
  if not hf_token:
15
  raise ValueError("Hugging Face token not found. Ensure it's set as a secret in the Hugging Face Space.")
16
 
17
  # Initialize the Hugging Face API
18
  api = HfApi()
 
19
 
20
  # Database setup
21
  DATABASE = "students.db"
 
62
  path_or_fileobj=local_path,
63
  path_in_repo=filename,
64
  repo_id=repo_name,
65
+ repo_type="dataset",
66
+ token=hf_token # Pass the token directly
67
  )
68
  st.success(f"Image uploaded to Hugging Face: {filename}")
69
  except Exception as e:
 
85
  capture_mode = st.radio("Choose an option to upload your image", ["Use Webcam", "Upload File"])
86
 
87
  if capture_mode == "Use Webcam":
88
+ try:
89
+ picture = st.camera_input("Take a picture")
90
+ except Exception as e:
91
+ st.error(f"Error accessing webcam: {e}")
92
+ picture = None
93
 
94
  elif capture_mode == "Upload File":
95
  picture = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
 
102
  st.error("Please upload or capture an image.")
103
  else:
104
  try:
105
+ if capture_mode == "Use Webcam" and picture:
106
  image = Image.open(picture)
107
+ elif capture_mode == "Upload File" and picture:
108
  image = Image.open(picture)
109
 
110
  # Save locally and upload to Hugging Face