Spaces:
Sleeping
Sleeping
Update src/tracker.py
Browse filesfixing user permission issues
- src/tracker.py +267 -261
src/tracker.py
CHANGED
|
@@ -1,262 +1,268 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import streamlit_authenticator as stauth
|
| 3 |
-
import yaml
|
| 4 |
-
from yaml.loader import SafeLoader
|
| 5 |
-
import json
|
| 6 |
-
import os
|
| 7 |
-
import
|
| 8 |
-
|
| 9 |
-
import
|
| 10 |
-
|
| 11 |
-
from
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
#
|
| 25 |
-
LOCAL_DATA_DIR = Path("data_persistence")
|
| 26 |
-
LOCAL_DATA_DIR.mkdir(exist_ok=True)
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
return LOCAL_DATA_DIR /
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
return {"total_tokens": 0, "users": {}}
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import streamlit_authenticator as stauth
|
| 3 |
+
import yaml
|
| 4 |
+
from yaml.loader import SafeLoader
|
| 5 |
+
import json
|
| 6 |
+
import os
|
| 7 |
+
import stat # <--- NEW IMPORT for permission handling
|
| 8 |
+
import uuid
|
| 9 |
+
from datetime import datetime
|
| 10 |
+
import pytz
|
| 11 |
+
from huggingface_hub import HfApi, hf_hub_download, snapshot_download, CommitScheduler
|
| 12 |
+
from pathlib import Path
|
| 13 |
+
import bcrypt
|
| 14 |
+
|
| 15 |
+
# --- CONFIGURATION ---
|
| 16 |
+
DATASET_REPO_ID = "NavyDevilDoc/navy-ai-logs"
|
| 17 |
+
LOG_FILE = "usage_log.json"
|
| 18 |
+
CONFIG_FILE = "config.yaml"
|
| 19 |
+
CHROMA_ROOT = "chroma_db"
|
| 20 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 21 |
+
INVITE_CODE = os.getenv("INVITE_CODE", "CHANGE_ME_IN_SETTINGS")
|
| 22 |
+
TIMEZONE = pytz.timezone("US/Eastern")
|
| 23 |
+
|
| 24 |
+
# --- DATA PERSISTENCE SETUP ---
|
| 25 |
+
LOCAL_DATA_DIR = Path("data_persistence")
|
| 26 |
+
LOCAL_DATA_DIR.mkdir(exist_ok=True)
|
| 27 |
+
|
| 28 |
+
scheduler = CommitScheduler(
|
| 29 |
+
repo_id=DATASET_REPO_ID,
|
| 30 |
+
repo_type="dataset",
|
| 31 |
+
folder_path=LOCAL_DATA_DIR,
|
| 32 |
+
path_in_repo=".",
|
| 33 |
+
every=1,
|
| 34 |
+
token=HF_TOKEN
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# --- PATH HELPERS ---
|
| 38 |
+
def get_config_path():
|
| 39 |
+
return LOCAL_DATA_DIR / CONFIG_FILE
|
| 40 |
+
|
| 41 |
+
def get_log_path():
|
| 42 |
+
return LOCAL_DATA_DIR / LOG_FILE
|
| 43 |
+
|
| 44 |
+
# --- GENERIC FILE SYNC ---
|
| 45 |
+
@st.cache_data(ttl=60)
|
| 46 |
+
def download_config_if_needed():
|
| 47 |
+
if not HF_TOKEN: return
|
| 48 |
+
try:
|
| 49 |
+
hf_hub_download(
|
| 50 |
+
repo_id=DATASET_REPO_ID,
|
| 51 |
+
filename=CONFIG_FILE,
|
| 52 |
+
repo_type="dataset",
|
| 53 |
+
local_dir=LOCAL_DATA_DIR,
|
| 54 |
+
token=HF_TOKEN,
|
| 55 |
+
force_download=True
|
| 56 |
+
)
|
| 57 |
+
print("✅ Config refreshed from cloud.")
|
| 58 |
+
except Exception as e:
|
| 59 |
+
print(f"⚠️ Cloud pull failed for config: {e}")
|
| 60 |
+
|
| 61 |
+
def ensure_log_exists():
|
| 62 |
+
if not (LOCAL_DATA_DIR / LOG_FILE).exists():
|
| 63 |
+
try:
|
| 64 |
+
hf_hub_download(
|
| 65 |
+
repo_id=DATASET_REPO_ID,
|
| 66 |
+
filename=LOG_FILE,
|
| 67 |
+
repo_type="dataset",
|
| 68 |
+
local_dir=LOCAL_DATA_DIR,
|
| 69 |
+
token=HF_TOKEN
|
| 70 |
+
)
|
| 71 |
+
except:
|
| 72 |
+
with open(LOCAL_DATA_DIR / LOG_FILE, "w") as f:
|
| 73 |
+
json.dump({}, f)
|
| 74 |
+
|
| 75 |
+
# --- USER DB SYNC (THE FIX IS HERE) ---
|
| 76 |
+
def download_user_db(username):
|
| 77 |
+
"""Restores ONLY the specific user's Knowledge Base and unlocks permissions."""
|
| 78 |
+
if not HF_TOKEN: return
|
| 79 |
+
|
| 80 |
+
target_dir = os.path.dirname(os.path.abspath(__file__))
|
| 81 |
+
user_db_path = f"{CHROMA_ROOT}/{username}"
|
| 82 |
+
|
| 83 |
+
try:
|
| 84 |
+
print(f"📥 Syncing Knowledge Base for {username}...")
|
| 85 |
+
snapshot_download(
|
| 86 |
+
repo_id=DATASET_REPO_ID,
|
| 87 |
+
repo_type="dataset",
|
| 88 |
+
allow_patterns=[f"{user_db_path}/*"],
|
| 89 |
+
local_dir=target_dir,
|
| 90 |
+
token=HF_TOKEN
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
# --- PERMISSION FIX ---
|
| 94 |
+
# Force-add Write permissions to the downloaded folder and files.
|
| 95 |
+
# This solves the "readonly database" (Error 1032).
|
| 96 |
+
|
| 97 |
+
user_folder = Path(target_dir) / CHROMA_ROOT / username
|
| 98 |
+
|
| 99 |
+
if user_folder.exists():
|
| 100 |
+
# 1. Unlock the User Directory itself
|
| 101 |
+
current_mode = user_folder.stat().st_mode
|
| 102 |
+
user_folder.chmod(current_mode | stat.S_IWUSR | stat.S_IXUSR)
|
| 103 |
+
|
| 104 |
+
# 2. Unlock every file and subfolder inside
|
| 105 |
+
for item in user_folder.rglob('*'):
|
| 106 |
+
current_mode = item.stat().st_mode
|
| 107 |
+
if item.is_dir():
|
| 108 |
+
# Directories need Execute (IX) to be traversable
|
| 109 |
+
item.chmod(current_mode | stat.S_IWUSR | stat.S_IXUSR)
|
| 110 |
+
else:
|
| 111 |
+
# Files need Write (IW) to be modifiable
|
| 112 |
+
item.chmod(current_mode | stat.S_IWUSR)
|
| 113 |
+
|
| 114 |
+
print("✅ User Knowledge Base Restored & Unlocked.")
|
| 115 |
+
|
| 116 |
+
except Exception as e:
|
| 117 |
+
print(f"⚠️ New user or sync error: {e}")
|
| 118 |
+
|
| 119 |
+
def upload_user_db(username):
|
| 120 |
+
"""Backs up ONLY the specific user's Knowledge Base."""
|
| 121 |
+
if not HF_TOKEN: return
|
| 122 |
+
|
| 123 |
+
target_dir = os.path.dirname(os.path.abspath(__file__))
|
| 124 |
+
user_db_rel_path = os.path.join(CHROMA_ROOT, username)
|
| 125 |
+
user_db_abs_path = os.path.join(target_dir, user_db_rel_path)
|
| 126 |
+
|
| 127 |
+
if not os.path.exists(user_db_abs_path):
|
| 128 |
+
return
|
| 129 |
+
|
| 130 |
+
try:
|
| 131 |
+
api = HfApi(token=HF_TOKEN)
|
| 132 |
+
api.upload_folder(
|
| 133 |
+
folder_path=user_db_abs_path,
|
| 134 |
+
path_in_repo=user_db_rel_path,
|
| 135 |
+
repo_id=DATASET_REPO_ID,
|
| 136 |
+
repo_type="dataset",
|
| 137 |
+
commit_message=f"KB Update ({username}): {datetime.now(TIMEZONE)}"
|
| 138 |
+
)
|
| 139 |
+
print(f"✅ Knowledge Base Saved for {username}.")
|
| 140 |
+
except Exception as e:
|
| 141 |
+
print(f"⚠️ DB sync failed: {e}")
|
| 142 |
+
|
| 143 |
+
# --- AUTHENTICATION ---
|
| 144 |
+
def check_login():
|
| 145 |
+
download_config_if_needed()
|
| 146 |
+
|
| 147 |
+
try:
|
| 148 |
+
config_path = get_config_path()
|
| 149 |
+
if not config_path.exists():
|
| 150 |
+
st.error(f"🚨 CRITICAL: Config not found at {config_path}")
|
| 151 |
+
return False
|
| 152 |
+
|
| 153 |
+
with open(config_path) as file:
|
| 154 |
+
config = yaml.load(file, Loader=SafeLoader)
|
| 155 |
+
except Exception as e:
|
| 156 |
+
st.error(f"🚨 Config Error: {e}")
|
| 157 |
+
return False
|
| 158 |
+
|
| 159 |
+
authenticator = stauth.Authenticate(
|
| 160 |
+
config['credentials'],
|
| 161 |
+
config['cookie']['name'],
|
| 162 |
+
config['cookie']['key'],
|
| 163 |
+
config['cookie']['expiry_days']
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
authenticator.login(location='main')
|
| 167 |
+
|
| 168 |
+
if st.session_state["authentication_status"]:
|
| 169 |
+
username = st.session_state["username"]
|
| 170 |
+
try:
|
| 171 |
+
user_data = config['credentials']['usernames'].get(username, {})
|
| 172 |
+
user_roles = user_data.get('roles', [])
|
| 173 |
+
except Exception as e:
|
| 174 |
+
user_roles = []
|
| 175 |
+
|
| 176 |
+
st.session_state.roles = user_roles
|
| 177 |
+
st.session_state.username = username
|
| 178 |
+
st.session_state.name = st.session_state.get("name")
|
| 179 |
+
st.session_state.authenticator = authenticator
|
| 180 |
+
return True
|
| 181 |
+
|
| 182 |
+
elif st.session_state["authentication_status"] is False:
|
| 183 |
+
st.error('Username/password is incorrect')
|
| 184 |
+
return False
|
| 185 |
+
elif st.session_state["authentication_status"] is None:
|
| 186 |
+
st.warning('Please enter your username and password')
|
| 187 |
+
return False
|
| 188 |
+
|
| 189 |
+
# --- REGISTRATION ---
|
| 190 |
+
def register_user(new_email, new_username, new_name, new_password, invite_code):
|
| 191 |
+
if invite_code != INVITE_CODE:
|
| 192 |
+
return False, "Invalid Invite Code."
|
| 193 |
+
|
| 194 |
+
download_config_if_needed()
|
| 195 |
+
config_path = get_config_path()
|
| 196 |
+
|
| 197 |
+
with scheduler.lock:
|
| 198 |
+
with open(config_path) as file:
|
| 199 |
+
config = yaml.load(file, Loader=SafeLoader)
|
| 200 |
+
|
| 201 |
+
if new_username in config['credentials']['usernames']:
|
| 202 |
+
return False, "Username already exists."
|
| 203 |
+
|
| 204 |
+
hashed_bytes = bcrypt.hashpw(new_password.encode('utf-8'), bcrypt.gensalt())
|
| 205 |
+
hashed_pwd = hashed_bytes.decode('utf-8')
|
| 206 |
+
|
| 207 |
+
new_user_entry = {
|
| 208 |
+
"email": new_email,
|
| 209 |
+
"name": new_name,
|
| 210 |
+
"password": hashed_pwd,
|
| 211 |
+
"roles": ["user"]
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
config['credentials']['usernames'][new_username] = new_user_entry
|
| 215 |
+
|
| 216 |
+
with open(config_path, 'w') as file:
|
| 217 |
+
yaml.dump(config, file, default_flow_style=False)
|
| 218 |
+
|
| 219 |
+
return True, "Account created! Please log in."
|
| 220 |
+
|
| 221 |
+
# --- LOGGING ---
|
| 222 |
+
def log_usage(model_name, input_tokens, output_tokens):
|
| 223 |
+
ensure_log_exists()
|
| 224 |
+
log_path = get_log_path()
|
| 225 |
+
|
| 226 |
+
username = st.session_state.get("username", "anonymous")
|
| 227 |
+
now_est = datetime.now(TIMEZONE)
|
| 228 |
+
today = now_est.strftime("%Y-%m-%d")
|
| 229 |
+
|
| 230 |
+
with scheduler.lock:
|
| 231 |
+
data = {}
|
| 232 |
+
if log_path.exists():
|
| 233 |
+
with open(log_path, "r") as f:
|
| 234 |
+
try:
|
| 235 |
+
data = json.load(f)
|
| 236 |
+
except:
|
| 237 |
+
data = {}
|
| 238 |
+
|
| 239 |
+
if today not in data:
|
| 240 |
+
data[today] = {"total_tokens": 0, "users": {}}
|
| 241 |
+
|
| 242 |
+
if username not in data[today]["users"]:
|
| 243 |
+
data[today]["users"][username] = {"input": 0, "output": 0, "calls": 0}
|
| 244 |
+
|
| 245 |
+
data[today]["total_tokens"] += (input_tokens + output_tokens)
|
| 246 |
+
data[today]["users"][username]["input"] += input_tokens
|
| 247 |
+
data[today]["users"][username]["output"] += output_tokens
|
| 248 |
+
data[today]["users"][username]["calls"] += 1
|
| 249 |
+
|
| 250 |
+
with open(log_path, "w") as f:
|
| 251 |
+
json.dump(data, f, indent=2)
|
| 252 |
+
|
| 253 |
+
def get_daily_stats():
|
| 254 |
+
ensure_log_exists()
|
| 255 |
+
log_path = get_log_path()
|
| 256 |
+
|
| 257 |
+
now_est = datetime.now(TIMEZONE)
|
| 258 |
+
today = now_est.strftime("%Y-%m-%d")
|
| 259 |
+
|
| 260 |
+
if log_path.exists():
|
| 261 |
+
with open(log_path, "r") as f:
|
| 262 |
+
try:
|
| 263 |
+
data = json.load(f)
|
| 264 |
+
if today in data:
|
| 265 |
+
return data[today]
|
| 266 |
+
except:
|
| 267 |
+
pass
|
| 268 |
return {"total_tokens": 0, "users": {}}
|