Spaces:
Sleeping
Sleeping
File size: 3,570 Bytes
0c6fe9c 6a2dd05 0c6fe9c a4a884a 0c6fe9c 4634051 6a2dd05 a4a884a 0c6fe9c a4a884a 07a01b5 a4a884a 4634051 0c6fe9c 07a01b5 0c6fe9c 6a2dd05 0c6fe9c 4634051 0c6fe9c 4634051 0c6fe9c 6a2dd05 0c6fe9c 6a2dd05 07a01b5 0c6fe9c a4a884a 0c6fe9c 6a2dd05 a4a884a 0c6fe9c 07a01b5 0c6fe9c 6a2dd05 0c6fe9c 07a01b5 0c6fe9c 6a2dd05 0c6fe9c 4634051 a4a884a 4634051 0c6fe9c 07a01b5 | 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 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 | import os
import uuid
import csv
import pytz
from datetime import datetime
from pathlib import Path
from huggingface_hub import CommitScheduler, hf_hub_download
# --- CONFIGURAZIONE ---
DATASET_REPO_ID = "NextGenTech/ngt-ai-platform-logs"
LOG_DIR = Path("data/logs")
LOG_FILE = LOG_DIR / "access_logs.csv"
HF_TOKEN = os.environ.get("HF_TOKEN")
ITALY_TZ = pytz.timezone("Europe/Rome")
TIME = 5 # Minuti di intervallo tra ogni aggiornamento del dataset
LOG_DIR.mkdir(parents=True, exist_ok=True)
if HF_TOKEN:
try:
print("📥 Controllo presenza log remoti...")
hf_hub_download(
repo_id=DATASET_REPO_ID,
filename="logs/access_logs.csv",
repo_type="dataset",
token=HF_TOKEN,
local_dir="data",
local_dir_use_symlinks=False
)
print("✅ Storico log scaricato e ripristinato!")
except Exception as e:
print(f"⚠️ Nessun log remoto trovato (o primo avvio): {e}")
print("🆕 Si partirà con un nuovo file di log.")
if not LOG_FILE.exists() or LOG_FILE.stat().st_size == 0:
print("🆕 Creazione header file di log...")
with open(LOG_FILE, "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f, lineterminator='\n')
writer.writerow([
"timestamp", "session_id", "module", "action",
"ip_address", "user_agent", "language", "input_size", "input_text" ,"processing_time"
])
scheduler = CommitScheduler(
repo_id=DATASET_REPO_ID,
repo_type="dataset",
folder_path=LOG_DIR,
path_in_repo="logs",
every=TIME,
token=HF_TOKEN
)
def log_interaction(request, module_name, action, input_data=None, execution_time=0.0):
try:
if request:
headers = request.headers
ip = headers.get("x-forwarded-for", request.client.host)
user_agent = headers.get("user-agent", "Unknown")
language = headers.get("accept-language", "Unknown").split(',')[0]
else:
ip, user_agent, language = "LOCAL", "Dev-Mode", "it"
session_raw = f"{ip}{user_agent}{datetime.now().date()}"
session_id = str(uuid.uuid5(uuid.NAMESPACE_DNS, session_raw))[:8]
input_meta = "0"
input_text_content = ""
now_italy = datetime.now(ITALY_TZ)
if isinstance(input_data, str):
input_meta = f"{len(input_data)} chars"
clean_text = input_data.replace('\n', ' ').replace('\r', '')
input_text_content = (clean_text[:1000] + '..') if len(clean_text) > 1000 else clean_text
elif hasattr(input_data, 'shape'):
input_meta = f"{input_data.shape}"
input_text_content = "[IMAGE/BINARY]"
elif input_data is not None:
input_meta = "Binary/File"
input_text_content = "[FILE]"
# Scrittura Log (Append)
with scheduler.lock:
with open(LOG_FILE, "a", newline="", encoding="utf-8") as f:
writer = csv.writer(f, lineterminator='\n')
writer.writerow([
now_italy.isoformat(),
session_id,
module_name,
action,
ip,
user_agent,
language,
input_meta,
input_text_content,
f"{execution_time:.4f}s"
])
print(f"📝 LOG SALVATO: {module_name}")
except Exception as e:
print(f"❌ ERRORE LOGGING: {e}") |