Spaces:
Running
Running
Download usage_logger.py from DeepImagix/self-trained2: direct link, hf CLI and curl.
- Browser
- Download file 665 Bytes
-
https://huggingface.co/spaces/DeepImagix/self-trained2/resolve/refs%2Fpr%2F2/usage_logger.py
- Command line
-
hf download hf://spaces/DeepImagix/self-trained2@refs/pr/2/usage_logger.py
-
curl -L -o usage_logger.py https://huggingface.co/spaces/DeepImagix/self-trained2/resolve/refs%2Fpr%2F2/usage_logger.py
665 Bytes
| # usage_logger.py | |
| import os, json, time | |
| STATS_FILE = "/data/ai_stats.json" | |
| def log_request(user_id: str): | |
| if os.path.exists(STATS_FILE): | |
| with open(STATS_FILE, "r") as f: | |
| stats = json.load(f) | |
| else: | |
| stats = {"total": 0, "users": {}} | |
| stats["total"] += 1 | |
| stats["users"][user_id] = stats["users"].get(user_id, 0) + 1 | |
| stats["last_request"] = time.strftime("%Y-%m-%d %H:%M:%S") | |
| with open(STATS_FILE, "w") as f: | |
| json.dump(stats, f, indent=2) | |
| def get_stats(): | |
| if os.path.exists(STATS_FILE): | |
| with open(STATS_FILE, "r") as f: | |
| return json.load(f) | |
| return {"message": "No stats found."} |