Spaces:
Sleeping
Sleeping
Upload backend/scripts/report_metrics.py with huggingface_hub
Browse files
backend/scripts/report_metrics.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import os
|
| 3 |
+
import sys
|
| 4 |
+
from datetime import datetime, date
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
import django
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
ROOT_DIR = Path(__file__).resolve().parents[2]
|
| 11 |
+
BACKEND_DIR = ROOT_DIR / "backend"
|
| 12 |
+
|
| 13 |
+
HUE_PORTAL_DIR = BACKEND_DIR / "hue_portal"
|
| 14 |
+
|
| 15 |
+
for path in (HUE_PORTAL_DIR, BACKEND_DIR, ROOT_DIR):
|
| 16 |
+
if str(path) not in sys.path:
|
| 17 |
+
sys.path.insert(0, str(path))
|
| 18 |
+
|
| 19 |
+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hue_portal.hue_portal.settings")
|
| 20 |
+
django.setup()
|
| 21 |
+
|
| 22 |
+
from django.db.models import Avg, Count, Q
|
| 23 |
+
from hue_portal.core.models import AuditLog, MLMetrics
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def parse_args():
|
| 27 |
+
parser = argparse.ArgumentParser(description="Tổng hợp metrics ML hàng ngày")
|
| 28 |
+
parser.add_argument("--date", help="Ngày cần tổng hợp (YYYY-MM-DD), mặc định hôm nay")
|
| 29 |
+
return parser.parse_args()
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def target_date(arg: str) -> date:
|
| 33 |
+
if not arg:
|
| 34 |
+
return date.today()
|
| 35 |
+
return datetime.strptime(arg, "%Y-%m-%d").date()
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def compute_metrics(day: date) -> dict:
|
| 39 |
+
logs = AuditLog.objects.filter(created_at__date=day)
|
| 40 |
+
total = logs.count()
|
| 41 |
+
if total == 0:
|
| 42 |
+
return {
|
| 43 |
+
"date": day.isoformat(),
|
| 44 |
+
"total_requests": 0,
|
| 45 |
+
"intent_accuracy": None,
|
| 46 |
+
"average_latency_ms": None,
|
| 47 |
+
"error_rate": None,
|
| 48 |
+
"intent_breakdown": {},
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
latency_avg = logs.exclude(latency_ms__isnull=True).aggregate(avg=Avg("latency_ms"))["avg"]
|
| 52 |
+
errors = logs.filter(status__gte=400).count()
|
| 53 |
+
intents_with_conf = logs.filter(~Q(intent=""), status__lt=400)
|
| 54 |
+
intent_accuracy = None
|
| 55 |
+
if intents_with_conf.exists():
|
| 56 |
+
confident = intents_with_conf.filter(Q(confidence__gte=0.6) | Q(confidence__isnull=True)).count()
|
| 57 |
+
intent_accuracy = confident / intents_with_conf.count()
|
| 58 |
+
|
| 59 |
+
breakdown = (
|
| 60 |
+
logs.exclude(intent="")
|
| 61 |
+
.values("intent")
|
| 62 |
+
.annotate(count=Count("id"))
|
| 63 |
+
.order_by("intent")
|
| 64 |
+
)
|
| 65 |
+
breakdown_dict = {row["intent"]: row["count"] for row in breakdown}
|
| 66 |
+
|
| 67 |
+
return {
|
| 68 |
+
"date": day.isoformat(),
|
| 69 |
+
"total_requests": total,
|
| 70 |
+
"intent_accuracy": intent_accuracy,
|
| 71 |
+
"average_latency_ms": latency_avg,
|
| 72 |
+
"error_rate": errors / total,
|
| 73 |
+
"intent_breakdown": breakdown_dict,
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def save_metrics(day: date, metrics: dict) -> MLMetrics:
|
| 78 |
+
obj, _ = MLMetrics.objects.update_or_create(
|
| 79 |
+
date=day,
|
| 80 |
+
defaults={
|
| 81 |
+
"total_requests": metrics["total_requests"],
|
| 82 |
+
"intent_accuracy": metrics["intent_accuracy"],
|
| 83 |
+
"average_latency_ms": metrics["average_latency_ms"],
|
| 84 |
+
"error_rate": metrics["error_rate"],
|
| 85 |
+
"intent_breakdown": metrics["intent_breakdown"],
|
| 86 |
+
},
|
| 87 |
+
)
|
| 88 |
+
return obj
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def main():
|
| 92 |
+
args = parse_args()
|
| 93 |
+
day = target_date(args.date)
|
| 94 |
+
metrics = compute_metrics(day)
|
| 95 |
+
save_metrics(day, metrics)
|
| 96 |
+
|
| 97 |
+
print("=== ML Metrics ===")
|
| 98 |
+
print(f"Ngày: {metrics['date']}")
|
| 99 |
+
print(f"Tổng request: {metrics['total_requests']}")
|
| 100 |
+
print(f"Độ chính xác (ước tính): {metrics['intent_accuracy']}")
|
| 101 |
+
print(f"Latency trung bình (ms): {metrics['average_latency_ms']}")
|
| 102 |
+
print(f"Tỉ lệ lỗi: {metrics['error_rate']}")
|
| 103 |
+
print(f"Phân bổ intent: {metrics['intent_breakdown']}")
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
if __name__ == "__main__":
|
| 107 |
+
main()
|