Download admin/exports.py from Hfzune/Test: direct link, hf CLI and curl.
- Browser
- Download file 2.77 kB
-
https://huggingface.co/spaces/Hfzune/Test/resolve/main/admin/exports.py
- Command line
-
hf download hf://spaces/Hfzune/Test/admin/exports.py
-
curl -L -o exports.py https://huggingface.co/spaces/Hfzune/Test/resolve/main/admin/exports.py
2.77 kB
| import csv | |
| import io | |
| import json | |
| from datetime import datetime, timezone | |
| from db import models as dbm | |
| def export_json() -> str: | |
| users = dbm.list_users() | |
| out = { | |
| "exported_at": datetime.now(timezone.utc).isoformat(), | |
| "stats": dbm.stats(), | |
| "users": [], | |
| } | |
| for u in users: | |
| conv = dbm.list_conversations(u["user_id"], limit=100000) | |
| conv = list(reversed(conv)) | |
| sessions = dbm.archive_list(u["user_id"]) | |
| sessions = sorted(sessions, key=lambda s: s["filename"]) | |
| out["users"].append({ | |
| "user_id": u["user_id"], | |
| "username": u.get("username"), | |
| "token": u.get("token"), | |
| "created_at": u.get("created_at"), | |
| "total_turns": u.get("total_turns", 0), | |
| "live_context": [ | |
| { | |
| "timestamp": c["timestamp"], | |
| "role": c["role"], | |
| "content": c["content"], | |
| "latency_ms": c.get("latency_ms"), | |
| "gpu_time_ms": c.get("gpu_time_ms"), | |
| } | |
| for c in conv | |
| ], | |
| "sessions": [ | |
| { | |
| "filename": s["filename"], | |
| "modified": s["modified"], | |
| "size_bytes": s["size"], | |
| "transcript_md": dbm.archive_read(u["user_id"], s["filename"]), | |
| } | |
| for s in sessions | |
| ], | |
| }) | |
| return json.dumps(out, ensure_ascii=False, indent=2) | |
| def export_csv() -> str: | |
| buf = io.StringIO() | |
| w = csv.writer(buf) | |
| w.writerow(["timestamp", "user_id", "username", "role", | |
| "content", "latency_ms", "gpu_time_ms"]) | |
| users = {u["user_id"]: u for u in dbm.list_users()} | |
| for uid, u in users.items(): | |
| conv = list(reversed(dbm.list_conversations(uid, limit=100000))) | |
| for c in conv: | |
| w.writerow([ | |
| c["timestamp"], uid, u.get("username"), | |
| c["role"], c["content"], | |
| c.get("latency_ms"), c.get("gpu_time_ms"), | |
| ]) | |
| return buf.getvalue() | |
| def export_markdown() -> str: | |
| lines = [f"# Conversation Export", | |
| f"Generated: {datetime.now(timezone.utc).isoformat()}", ""] | |
| for u in dbm.list_users(): | |
| lines.append(f"## User {u['user_id']} ({u.get('username') or 'n/a'})") | |
| lines.append("") | |
| conv = list(reversed(dbm.list_conversations(u["user_id"], limit=100000))) | |
| for c in conv: | |
| role = "User" if c["role"] == "user" else "Assistant" | |
| lines.append(f"**{role}** ({c['timestamp']}):") | |
| lines.append("") | |
| lines.append(c["content"]) | |
| lines.append("") | |
| return "\n".join(lines) |