Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,54 @@
|
|
| 1 |
-
import
|
| 2 |
-
from PIL import Image
|
| 3 |
-
from deepface import DeepFace
|
| 4 |
import os
|
| 5 |
from googleapiclient.discovery import build
|
| 6 |
-
from google.oauth2 import
|
| 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 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
return
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 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 |
-
|
|
|
|
|
|
|
| 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()
|