Ajay98 commited on
Commit
cfad6fa
·
verified ·
1 Parent(s): ba78090

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -59
app.py CHANGED
@@ -1,67 +1,54 @@
1
- import streamlit as st
2
- from PIL import Image
3
- from deepface import DeepFace
4
  import os
5
  from googleapiclient.discovery import build
6
- from google.oauth2 import service_account
7
- import io
8
 
9
- # Google Drive API setup
10
  SCOPES = ['https://www.googleapis.com/auth/drive']
11
  SERVICE_ACCOUNT_FILE = 'service_account.json'
12
 
13
- credentials = service_account.Credentials.from_service_account_file(
14
- SERVICE_ACCOUNT_FILE, scopes=SCOPES)
15
- drive_service = build('drive', 'v3', credentials=credentials)
16
- REFERENCE_FOLDER_ID = '1DGeRqRbCPcfLDdEgP0h5fyX-MF8EQ8AH'
17
-
18
- # Helper function to download reference files
19
- def list_reference_files(folder_id):
20
- query = f"'{folder_id}' in parents and trashed=false"
21
- results = drive_service.files().list(q=query, fields="files(id, name)").execute()
22
- return results.get("files", [])
23
-
24
- def download_file(file_id, file_name):
25
- request = drive_service.files().get_media(fileId=file_id)
26
- file_io = io.BytesIO()
27
- downloader = MediaIoBaseDownload(file_io, request)
28
- done = False
29
- while not done:
30
- _, done = downloader.next_chunk()
31
- os.makedirs("reference_images", exist_ok=True)
32
- file_path = os.path.join("reference_images", file_name)
33
- with open(file_path, "wb") as f:
34
- f.write(file_io.getvalue())
35
- return file_path
36
-
37
- # Streamlit app
38
- st.title("Face Authentication Demo")
39
- st.write("Upload a selfie to authenticate.")
40
-
41
- uploaded_file = st.file_uploader("Choose a selfie", type=["jpg", "jpeg", "png"])
42
-
43
- if uploaded_file is not None:
44
- selfie_path = os.path.join("static", "uploads", uploaded_file.name)
45
- os.makedirs(os.path.dirname(selfie_path), exist_ok=True)
46
- with open(selfie_path, "wb") as f:
47
- f.write(uploaded_file.getbuffer())
48
-
49
- # Download reference images from Google Drive
50
- st.write("Downloading reference images...")
51
- reference_files = list_reference_files(REFERENCE_FOLDER_ID)
52
- reference_paths = [download_file(f['id'], f['name']) for f in reference_files]
53
-
54
- # Compare selfie with reference images
55
- st.write("Authenticating...")
56
- authenticated = False
57
- for ref_path in reference_paths:
58
- if DeepFace.verify(img1_path=ref_path, img2_path=selfie_path,
59
- model_name="ArcFace", enforce_detection=False)['verified']:
60
- authenticated = True
61
- break
62
-
63
- if authenticated:
64
- st.success("Authentication successful!")
65
  else:
66
- st.error("Authentication failed. ID not verified.")
 
 
67
 
 
 
 
1
+ import requests
 
 
2
  import os
3
  from googleapiclient.discovery import build
4
+ from google.oauth2.service_account import Credentials
 
5
 
6
+ # Define the Google Drive API setup
7
  SCOPES = ['https://www.googleapis.com/auth/drive']
8
  SERVICE_ACCOUNT_FILE = 'service_account.json'
9
 
10
+ # URL of the service account JSON file on Hugging Face
11
+ HUGGINGFACE_JSON_URL = 'https://huggingface.co/username/repository-name/resolve/main/service_account.json'
12
+
13
+ def download_service_account_file():
14
+ """Download the service account JSON file from Hugging Face."""
15
+ if not os.path.exists(SERVICE_ACCOUNT_FILE): # Check if file already exists
16
+ print("Downloading service account file from Hugging Face...")
17
+ response = requests.get(HUGGINGFACE_JSON_URL)
18
+ if response.status_code == 200:
19
+ with open(SERVICE_ACCOUNT_FILE, 'wb') as f:
20
+ f.write(response.content)
21
+ print(f"Service account file downloaded: {SERVICE_ACCOUNT_FILE}")
22
+ else:
23
+ raise Exception(f"Failed to download service account file: {response.status_code}, {response.text}")
24
+ else:
25
+ print(f"Service account file already exists: {SERVICE_ACCOUNT_FILE}")
26
+
27
+ # Download the service account file
28
+ download_service_account_file()
29
+
30
+ # Authenticate with the Google API
31
+ def authenticate_google_drive():
32
+ """Authenticate and return the Google Drive service object."""
33
+ credentials = Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
34
+ drive_service = build('drive', 'v3', credentials=credentials)
35
+ print("Google Drive API authenticated successfully!")
36
+ return drive_service
37
+
38
+ # Example usage of the authenticated service
39
+ drive_service = authenticate_google_drive()
40
+
41
+ # Example API call: List files in Google Drive
42
+ def list_drive_files():
43
+ """List files in Google Drive."""
44
+ results = drive_service.files().list(pageSize=10).execute()
45
+ items = results.get('files', [])
46
+ if not items:
47
+ print("No files found.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  else:
49
+ print("Files:")
50
+ for item in items:
51
+ print(f"{item['name']} ({item['id']})")
52
 
53
+ # Call the function to list files
54
+ list_drive_files()