Upload app/api/dashboard.py with huggingface_hub
Browse files- app/api/dashboard.py +83 -0
app/api/dashboard.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Request
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
from db.database import SessionLocal
|
| 4 |
+
from db.models import Prediction, TrainingSession
|
| 5 |
+
from jinja2 import Template
|
| 6 |
+
|
| 7 |
+
router = APIRouter()
|
| 8 |
+
|
| 9 |
+
dashboard_template = """
|
| 10 |
+
<!DOCTYPE html>
|
| 11 |
+
<html lang='en'>
|
| 12 |
+
<head>
|
| 13 |
+
<meta charset='UTF-8'>
|
| 14 |
+
<title>Hallucination Detection Dashboard</title>
|
| 15 |
+
<link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css'>
|
| 16 |
+
</head>
|
| 17 |
+
<body>
|
| 18 |
+
<div class='container mt-4'>
|
| 19 |
+
<h1>Hallucination Detection Dashboard</h1>
|
| 20 |
+
<div class='row'>
|
| 21 |
+
<div class='col-md-4'>
|
| 22 |
+
<div class='card mb-3'>
|
| 23 |
+
<div class='card-body'>
|
| 24 |
+
<h5 class='card-title'>Total Predictions</h5>
|
| 25 |
+
<p class='card-text'>{{ total_predictions }}</p>
|
| 26 |
+
</div>
|
| 27 |
+
</div>
|
| 28 |
+
</div>
|
| 29 |
+
<div class='col-md-4'>
|
| 30 |
+
<div class='card mb-3'>
|
| 31 |
+
<div class='card-body'>
|
| 32 |
+
<h5 class='card-title'>Hallucination Rate</h5>
|
| 33 |
+
<p class='card-text'>{{ hallucination_rate }}%</p>
|
| 34 |
+
</div>
|
| 35 |
+
</div>
|
| 36 |
+
</div>
|
| 37 |
+
<div class='col-md-4'>
|
| 38 |
+
<div class='card mb-3'>
|
| 39 |
+
<div class='card-body'>
|
| 40 |
+
<h5 class='card-title'>Avg Confidence</h5>
|
| 41 |
+
<p class='card-text'>{{ avg_confidence }}</p>
|
| 42 |
+
</div>
|
| 43 |
+
</div>
|
| 44 |
+
</div>
|
| 45 |
+
</div>
|
| 46 |
+
<h3>Recent Predictions</h3>
|
| 47 |
+
<table class='table table-striped'>
|
| 48 |
+
<thead><tr><th>Time</th><th>Prompt</th><th>Response</th><th>Question</th><th>Result</th><th>Confidence</th></tr></thead>
|
| 49 |
+
<tbody>
|
| 50 |
+
{% for p in recent_predictions %}
|
| 51 |
+
<tr>
|
| 52 |
+
<td>{{ p.created_at }}</td>
|
| 53 |
+
<td>{{ p.prompt[:30] }}...</td>
|
| 54 |
+
<td>{{ p.response[:30] }}...</td>
|
| 55 |
+
<td>{{ p.question[:30] }}...</td>
|
| 56 |
+
<td>{{ 'Hallucination' if p.is_hallucination else 'Factual' }}</td>
|
| 57 |
+
<td>{{ p.confidence_score }}</td>
|
| 58 |
+
</tr>
|
| 59 |
+
{% endfor %}
|
| 60 |
+
</tbody>
|
| 61 |
+
</table>
|
| 62 |
+
</div>
|
| 63 |
+
</body>
|
| 64 |
+
</html>
|
| 65 |
+
"""
|
| 66 |
+
|
| 67 |
+
@router.get("/", response_class=HTMLResponse)
|
| 68 |
+
def dashboard(request: Request):
|
| 69 |
+
db = SessionLocal()
|
| 70 |
+
preds = db.query(Prediction).order_by(Prediction.created_at.desc()).limit(20).all()
|
| 71 |
+
total = db.query(Prediction).count()
|
| 72 |
+
halluc = db.query(Prediction).filter(Prediction.is_hallucination == True).count()
|
| 73 |
+
avg_conf = db.query(Prediction.confidence_score).all()
|
| 74 |
+
avg_conf = round(sum([x[0] for x in avg_conf])/len(avg_conf), 2) if avg_conf else 0
|
| 75 |
+
halluc_rate = round((halluc/total)*100, 2) if total else 0
|
| 76 |
+
html = Template(dashboard_template).render(
|
| 77 |
+
total_predictions=total,
|
| 78 |
+
hallucination_rate=halluc_rate,
|
| 79 |
+
avg_confidence=avg_conf,
|
| 80 |
+
recent_predictions=preds
|
| 81 |
+
)
|
| 82 |
+
db.close()
|
| 83 |
+
return HTMLResponse(content=html)
|