File size: 1,507 Bytes
1a73c47
 
 
 
 
 
 
 
 
a0ecdd1
 
1a73c47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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