File size: 1,152 Bytes
a0f3d24
b4fadea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# SQLite + file logging

import sqlite3
import json
from datetime import datetime
from app.core.config import DB_PATH, MODEL_VERSION


def get_connection():
    return sqlite3.connect(DB_PATH, check_same_thread=False)


def init_db():
    conn = get_connection()
    cursor = conn.cursor()

    cursor.execute("""
        CREATE TABLE IF NOT EXISTS predictions (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            timestamp TEXT,
            model_version TEXT,
            input_features TEXT,
            prediction INTEGER,
            probability REAL
        )
    """)

    conn.commit()
    conn.close()


def log_prediction(features: dict, prediction: int, probability: float):
    conn = get_connection()
    cursor = conn.cursor()

    cursor.execute(
        """
        INSERT INTO predictions
        (timestamp, model_version, input_features, prediction, probability)
        VALUES (?, ?, ?, ?, ?)
        """,
        (
            datetime.utcnow().isoformat(),
            MODEL_VERSION,
            json.dumps(features),
            prediction,
            probability,
        )
    )

    conn.commit()
    conn.close()