QueryHelper / persistStorage.py
anumaurya114exp's picture
syntax fix
f80313a
raw
history blame
1.1 kB
from huggingface_hub import HfFileSystem
from datetime import datetime
import pytz
import os
from config import HUGGING_FACE_TOKEN
# Set the time zone to Pacific Time Zone
TIME_ZONE = 'US/Pacific'
TIMEZONE_OBJ = pytz.timezone(TIME_ZONE)
LOGS_DIR = "logs"
os.makedirs(LOGS_DIR, exist_ok=True)
fs = HfFileSystem(token=HUGGING_FACE_TOKEN)
def append_dict_to_csv(file_path, row_data):
fieldnames = row_data.keys()
with fs.open(file_path, 'a+') as csv_file:
csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
# Check if the file is empty, and if so, write the header
if csv_file.tell() == 0:
csv_writer.writeheader()
csv_writer.writerow(row_data)
def saveLog(message, level='info') -> None:
current_time = datetime.now(TIMEZONE_OBJ)
message = str(message)
log_path = 'spaces/Express-Analytics/QueryHelper/'
log_file_path = os.path.join(log_path, f"{current_time.strftime('%Y-%m')}.csv")
data_dict = {"time":str(current_time), "level": level, "message": message}
append_dict_to_csv(log_file_path, data_dict)