Spaces:
Runtime error
Runtime error
Commit ·
be452a0
1
Parent(s): 37bd4dd
persistent storage for logging
Browse files- persistStorage.py +38 -0
persistStorage.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import CommitScheduler
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
import pytz
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Set the time zone to Pacific Time Zone
|
| 7 |
+
TIME_ZONE = 'US/Pacific'
|
| 8 |
+
TIMEZONE_OBJ = pytz.timezone(TIME_ZONE)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
LOGS_DIR = "logs"
|
| 13 |
+
os.makedirs(LOGS_DIR, exist_ok=True)
|
| 14 |
+
|
| 15 |
+
scheduler = CommitScheduler(
|
| 16 |
+
repo_id="QueryHelper",
|
| 17 |
+
folder_path=LOGS_DIR,
|
| 18 |
+
path_in_repo="data",
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
def append_dict_to_csv(file_path, row_data):
|
| 22 |
+
fieldnames = row_data.keys()
|
| 23 |
+
|
| 24 |
+
with open(file_path, 'a+', newline='\n') as csv_file:
|
| 25 |
+
csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
|
| 26 |
+
|
| 27 |
+
# Check if the file is empty, and if so, write the header
|
| 28 |
+
if csv_file.tell() == 0:
|
| 29 |
+
csv_writer.writeheader()
|
| 30 |
+
csv_writer.writerow(row_data)
|
| 31 |
+
|
| 32 |
+
def saveLog(message, level='info') -> None:
|
| 33 |
+
with scheduler.lock:
|
| 34 |
+
current_time = datetime.now(TIMEZONE_OBJ)
|
| 35 |
+
message = str(message)
|
| 36 |
+
JSON_DATASET_PATH = os.path.join(LOGS_DIR, f"{current_time.strftime("%Y-%m")}.csv")
|
| 37 |
+
data_dict = {"time":str(currrent_time), "level": level, "message": message}
|
| 38 |
+
append_dict_to_csv(JSON_DATASET_PATH, data_dict)
|