bndl's picture
Update spaces_utils.py
a0ecdd1
import os
import csv
import pandas as pd
from datetime import datetime
from datasets import load_dataset, Dataset
from huggingface_hub import Repository
# Load tokens
GRADIO_TOKEN = os.environ.get("READ_TOKEN") # read
WRITE_TOKEN = os.environ.get("WRITE_TOKEN") # write
def write_logs(message, dataset_url, dataset_path, log_type="Prediction"):
"""
Write logs
"""
with Repository(local_dir="data", clone_from=dataset_url, use_auth_token=WRITE_TOKEN).commit(
commit_message="from private", blocking=False
):
with open(dataset_path, "a") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])
writer.writerow({"name": log_type, "message": message, "time": str(datetime.now())})
return
def authenticate(username, password, dataset_url, dataset_path):
"""
Verify if the provided tuple (username, password) is in the database
"""
df_password = load_dataset(
"sandl/test_passwords", data_files="test_password.csv", delimiter=";", use_auth_token=GRADIO_TOKEN
)
df_password = pd.DataFrame(df_password["train"])
if username not in list(df_password["username"]):
return False
authentication_sucess = df_password[df_password["username"] == username]["password"].iloc[0] == password
if authentication_sucess:
message = f"{username} logged"
write_logs(message, dataset_url, dataset_path, log_type="Authentication")
return authentication_sucess