bndl commited on
Commit
1a73c47
·
1 Parent(s): c9207fd

Create spaces_utils.py

Browse files
Files changed (1) hide show
  1. spaces_utils.py +43 -0
spaces_utils.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import csv
3
+ import pandas as pd
4
+ from datetime import datetime
5
+ from datasets import load_dataset, Dataset
6
+ from huggingface_hub import Repository
7
+
8
+
9
+ # Load tokens
10
+ GRADIO_TOKEN = os.environ.get("GRADIO_ORG") # read
11
+ WRITE_TOKEN = os.environ.get("WRITE_PER") # write
12
+
13
+
14
+ def write_logs(message, dataset_url, dataset_path, log_type="Prediction"):
15
+ """
16
+ Write logs
17
+ """
18
+ with Repository(local_dir="data", clone_from=dataset_url, use_auth_token=WRITE_TOKEN).commit(
19
+ commit_message="from private", blocking=False
20
+ ):
21
+ with open(dataset_path, "a") as csvfile:
22
+ writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])
23
+ writer.writerow({"name": log_type, "message": message, "time": str(datetime.now())})
24
+ return
25
+
26
+
27
+ def authenticate(username, password, dataset_url, dataset_path):
28
+ """
29
+ Verify if the provided tuple (username, password) is in the database
30
+ """
31
+ df_password = load_dataset(
32
+ "sandl/test_passwords", data_files="test_password.csv", delimiter=";", use_auth_token=GRADIO_TOKEN
33
+ )
34
+ df_password = pd.DataFrame(df_password["train"])
35
+
36
+ if username not in list(df_password["username"]):
37
+ return False
38
+
39
+ authentication_sucess = df_password[df_password["username"] == username]["password"].iloc[0] == password
40
+ if authentication_sucess:
41
+ message = f"{username} logged"
42
+ write_logs(message, dataset_url, dataset_path, log_type="Authentication")
43
+ return authentication_sucess