Team Ai
Apppublic

IDKHowToCodeFr/tinyml-backend

sourceHugging Faceupdated 8h agoView on Hugging Face
1likes
seed_db.py67 linesDownload Raw Back to root
1import sqlite32import os3import random4from datetime import datetime, timedelta5 6DB_NAME = 'patient_history.db'7DB_PATH = os.path.join('backend', DB_NAME)8 9def seed_data():10    if not os.path.exists('backend'):11        os.makedirs('backend')12 13    conn = sqlite3.connect(DB_PATH)14    cursor = conn.cursor()15    16    # Ensure table exists17    cursor.execute('''18        CREATE TABLE IF NOT EXISTS predictions (19            id INTEGER PRIMARY KEY AUTOINCREMENT,20            timestamp DATETIME,21            heart_rate REAL,22            spo2 REAL,23            sys_bp REAL,24            dia_bp REAL,25            temp REAL,26            fall_detection TEXT,27            prediction_label TEXT,28            confidence REAL29        )30    ''')31 32    # Sample data generation33    labels = ["Healthy", "At Risk"]34    fall_options = ["No Fall", "Fall Detected"]35    36    now = datetime.utcnow()37    38    print("Seeding 30 sample predictions (UTC)...")39    for i in range(30):40        timestamp = (now - timedelta(minutes=i*15)).strftime("%Y-%m-%d %H:%M:%S")41        hr = round(random.uniform(60, 110), 1)42        spo2 = round(random.uniform(92, 99), 1)43        sys = round(random.uniform(110, 150), 1)44        dia = round(random.uniform(70, 95), 1)45        temp = round(random.uniform(36.1, 37.8), 1)46        fall = random.choice(fall_options) if hr > 100 else "No Fall"47        48        # Simple logic for label49        if hr > 100 or spo2 < 94 or sys > 140:50            label = "At Risk"51            conf = random.uniform(0.7, 0.95)52        else:53            label = "Healthy"54            conf = random.uniform(0.85, 0.99)55 56        cursor.execute('''57            INSERT INTO predictions (timestamp, heart_rate, spo2, sys_bp, dia_bp, temp, fall_detection, prediction_label, confidence)58            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)59        ''', (timestamp, hr, spo2, sys, dia, temp, fall, label, conf))60 61    conn.commit()62    conn.close()63    print("Done seeding.")64 65if __name__ == "__main__":66    seed_data()67