project5 / init_db.py
GitHub Action
Deploy to HuggingFace Spaces from main branch
66a0674
#!/usr/bin/env python3
"""Script d'initialisation de la base de données SQLite pour HuggingFace Spaces."""
import os
import sqlite3
import sys
# Ajouter le répertoire src au path pour les imports
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src"))
# Connexion globale en mémoire partagée
_db_connection = None
def get_db_connection():
"""Retourne la connexion SQLite partagée en mémoire."""
global _db_connection
if _db_connection is None:
_db_connection = sqlite3.connect(":memory:", check_same_thread=False)
return _db_connection
def init_sqlite_data():
"""Initialise la base SQLite avec les données essentielles en utilisant SQLite direct."""
try:
conn = get_db_connection()
cursor = conn.cursor()
# Vérifier les tables existantes
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = cursor.fetchall()
print(f"📋 Tables existantes: {[table[0] for table in tables]}")
print("📊 Insertion des données de référence...")
# Données Neighborhoods
neighborhoods = [
(1, "UNKNOWN", -1, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(2, "BALLARD", 0, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(3, "CENTRAL", 1, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(4, "DELRIDGE", 2, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(5, "DOWNTOWN", 3, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(6, "EAST", 4, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(7, "GREATER DUWAMISH", 5, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(8, "LAKE UNION", 6, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(
9,
"MAGNOLIA / QUEEN ANNE",
7,
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(10, "NORTH", 8, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(11, "NORTHEAST", 9, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(12, "NORTHWEST", 10, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(13, "SOUTHEAST", 11, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(14, "SOUTHWEST", 12, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(15, "WEST", 13, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
]
cursor.executemany(
"INSERT OR REPLACE INTO neighborhood (id, neighborhood_name, model_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?)",
neighborhoods,
)
# Données Building Types
building_types = [
(
1,
-1,
"UNKNOWN",
"Type de bâtiment inconnu ou non spécifié",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
2,
0,
"CAMPUS",
"Campus building complex",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
3,
1,
"NONRESIDENTIAL",
"Non-residential building",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
4,
2,
"NONRESIDENTIAL COS",
"Non-residential COS type",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
5,
3,
"NONRESIDENTIAL WA",
"Non-residential WA type",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
6,
4,
"SPS-DISTRICT K-12",
"Seattle Public Schools District K-12",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
7,
5,
"Multifamily MR (5-9)",
"Multifamily Mid-Rise 5-9 units",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
8,
6,
"Multifamily HR (10+)",
"Multifamily High-Rise 10+ units",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
9,
7,
"Multifamily LR (2-4)",
"Multifamily Low-Rise 2-4 units",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
]
cursor.executemany(
"INSERT OR REPLACE INTO building_type (id, model_id, building_type_name, description, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)",
building_types,
)
# Données Categories
categories = [
(
1,
"UNKNOWN",
"UNKNOWN",
"Catégorie inconnue ou non spécifiée",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
2,
"CAMPUS",
"Campus",
"Complexes de campus et installations multiples",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
3,
"EDUCATION",
"Éducation",
"Établissements denseignement et de formation",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
4,
"ENTERTAINMENT",
"Divertissement",
"Théâtres, cinémas et espaces de divertissement",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
5,
"FINANCIAL",
"Services financiers",
"Banques, bureaux financiers et services monétaires",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
6,
"HEALTHCARE",
"Santé",
"Hôpitaux, cliniques et établissements de soins médicaux",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
7,
"INDUSTRIAL",
"Industrie",
"Usines et installations industrielles",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
8,
"LODGING",
"Hébergement",
"Hôtels et logements temporaires",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
9,
"MIXED",
"Usage mixte",
"Propriétés à usage multiple",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
10,
"NONE",
"Aucun",
"Aucune utilisation spécifique",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
11,
"OFFICE",
"Bureaux",
"Espaces de bureaux et administratifs",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
12,
"PARKING",
"Stationnement",
"Structures et espaces de stationnement",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
13,
"PUBLIC",
"Services publics",
"Services gouvernementaux et publics",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
14,
"RECREATION",
"Loisirs",
"Installations sportives et récréatives",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
15,
"RELIGIOUS",
"Religieux",
"Églises et lieux de culte",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
16,
"RESIDENTIAL",
"Résidentiel",
"Logements et habitations",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
17,
"RESTAURANT",
"Restauration",
"Restaurants et services alimentaires",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
18,
"RETAIL",
"Commerce de détail",
"Magasins et commerces",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
19,
"SOCIAL",
"Social",
"Clubs et espaces sociaux",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
20,
"STORE",
"Magasins",
"Commerces et magasins divers",
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
]
cursor.executemany(
"INSERT OR REPLACE INTO categories (id, category_code, category_name, description, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)",
categories,
)
# Données Properties (sélection des principales)
properties = [
(1, -1, "UNKNOWN", 1, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(2, 0, "ADULT EDUCATION", 3, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(
3,
1,
"AUTOMOBILE DEALERSHIP",
20,
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(4, 2, "BANK BRANCH", 5, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(5, 3, "BAR/NIGHTCLUB", 19, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(
6,
4,
"COLLEGE/UNIVERSITY",
3,
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
7,
5,
"CONVENIENCE STORE WITHOUT GAS STATION",
20,
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(8, 6, "COURTHOUSE", 15, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(9, 7, "DATA CENTER", 11, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(
10,
8,
"DISTRIBUTION CENTER",
7,
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
11,
9,
"FINANCIAL OFFICE",
5,
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(12, 10, "FOOD SALES", 17, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(
13,
11,
"HOSPITAL (GENERAL MEDICAL & SURGICAL)",
6,
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(14, 12, "HOTEL", 8, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(15, 13, "K-12 SCHOOL", 3, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(16, 14, "LIBRARY", 3, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(17, 15, "MEDICAL OFFICE", 6, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(
18,
16,
"MULTIFAMILY HOUSING",
16,
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
19,
17,
"MUNICIPAL WASTEWATER TREATMENT PLANT",
7,
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(20, 18, "OFFICE", 11, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(21, 19, "OTHER", 1, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(22, 20, "PARKING", 12, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(23, 21, "RESTAURANT", 17, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(24, 22, "RETAIL STORE", 18, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
(
25,
23,
"SELF-STORAGE FACILITY",
7,
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
26,
24,
"SENIOR LIVING COMMUNITY",
8,
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
27,
25,
"SUPERMARKET/GROCERY STORE",
17,
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
28,
26,
"WAREHOUSE (UNREFRIGERATED)",
7,
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
29,
27,
"WORSHIP FACILITY",
15,
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
]
cursor.executemany(
"INSERT OR REPLACE INTO property (id, model_id, property_name, category_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)",
properties,
)
# Données Building Models (exemples pour tester l'API)
building_models = [
(
1,
"SEATTLE001",
"123 Main Street",
"Seattle",
"WA",
"98101",
"TAX001",
"DISTRICT1",
47.6062,
-122.3321,
1995,
1,
15,
50000.0,
5000.0,
2000.0,
500.0,
0, # multiusage
0, # steam
1, # electricity
1, # natural_gas
2, # neighborhood_id (BALLARD)
3, # building_type_id (NONRESIDENTIAL)
20, # largest_property_use_type_id (OFFICE)
20, # primary_property_type_id (OFFICE)
1, # second_largest_property_use_type_id (UNKNOWN)
1, # third_largest_property_use_type_id (UNKNOWN)
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
2,
"SEATTLE002",
"456 Pine Avenue",
"Seattle",
"WA",
"98102",
"TAX002",
"DISTRICT2",
47.6205,
-122.3493,
2010,
1,
8,
25000.0,
2000.0,
0.0,
0.0,
0, # multiusage
0, # steam
1, # electricity
1, # natural_gas
5, # neighborhood_id (DOWNTOWN)
7, # building_type_id (Multifamily MR 5-9)
18, # largest_property_use_type_id (MULTIFAMILY HOUSING)
18, # primary_property_type_id (MULTIFAMILY HOUSING)
1, # second_largest_property_use_type_id (UNKNOWN)
1, # third_largest_property_use_type_id (UNKNOWN)
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
(
3,
"SEATTLE003",
"789 University Way",
"Seattle",
"WA",
"98105",
"TAX003",
"DISTRICT3",
47.6587,
-122.3128,
1980,
1,
3,
15000.0,
1000.0,
500.0,
200.0,
1, # multiusage
0, # steam
1, # electricity
1, # natural_gas
11, # neighborhood_id (NORTHEAST)
6, # building_type_id (SPS-DISTRICT K-12)
15, # largest_property_use_type_id (K-12 SCHOOL)
15, # primary_property_type_id (K-12 SCHOOL)
16, # second_largest_property_use_type_id (LIBRARY)
1, # third_largest_property_use_type_id (UNKNOWN)
"2025-09-09 09:56:21",
"2025-09-09 09:56:21",
),
]
cursor.executemany(
"""INSERT OR REPLACE INTO building_models (
id, ose_building_id, address, city, state, zip_code,
tax_parcel_identification_number, council_district_code,
latitude, longitude, year_built, number_of_buildings, number_of_floors,
property_gfa_total, property_gfa_parking,
second_largest_property_use_type_gfa, third_largest_property_use_type_gfa,
multiusage, steam, electricity, natural_gas,
neighborhood_id, building_type_id,
largest_property_use_type_id, primary_property_type_id,
second_largest_property_use_type_id, third_largest_property_use_type_id,
created_at, updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
building_models,
)
# Données Building Energy Predictions (exemples de prédictions)
energy_predictions = [
(
1,
1, # building_id (correspond à SEATTLE001)
45678.5, # site_energy_use_wn_kbtu
1, # predicted (True)
"2025-09-09 10:30:00",
),
(
2,
2, # building_id (correspond à SEATTLE002)
23456.7, # site_energy_use_wn_kbtu
1, # predicted (True)
"2025-09-09 11:15:00",
),
(
3,
3, # building_id (correspond à SEATTLE003)
12345.3, # site_energy_use_wn_kbtu
1, # predicted (True)
"2025-09-09 12:00:00",
),
]
cursor.executemany(
"""INSERT OR REPLACE INTO building_energy_predictions (
id, building_id, site_energy_use_wn_kbtu, predicted, updated_at
) VALUES (?, ?, ?, ?, ?)""",
energy_predictions,
)
conn.commit()
# Ne pas fermer la connexion - elle reste en mémoire
print(f"✅ {len(neighborhoods)} quartiers insérés")
print(f"✅ {len(building_types)} types de bâtiments insérés")
print(f"✅ {len(categories)} catégories insérées")
print(f"✅ {len(properties)} propriétés insérées")
print(f"✅ {len(building_models)} bâtiments d'exemple insérés")
print(f"✅ {len(energy_predictions)} prédictions d'exemple insérées")
except Exception as e:
print(f"❌ Erreur lors de l'insertion des données: {e}")
raise
def create_sqlite_tables():
"""Crée les tables SQLite nécessaires avec raw SQL."""
try:
conn = get_db_connection()
cursor = conn.cursor()
# Créer les tables de référence principales
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS neighborhood (
id INTEGER PRIMARY KEY,
neighborhood_name VARCHAR(50) NOT NULL UNIQUE,
model_id INTEGER NOT NULL UNIQUE,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS building_type (
id INTEGER PRIMARY KEY,
model_id INTEGER NOT NULL UNIQUE,
building_type_name VARCHAR(100) NOT NULL UNIQUE,
description TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS categories (
id INTEGER PRIMARY KEY,
category_code VARCHAR(50) NOT NULL UNIQUE,
category_name VARCHAR(100) NOT NULL,
description TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS property (
id INTEGER PRIMARY KEY,
model_id INTEGER NOT NULL UNIQUE,
property_name VARCHAR(150) NOT NULL UNIQUE,
category_id INTEGER NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (category_id) REFERENCES categories (id)
);
"""
)
# Créer les tables principales
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS building_energy_predictions (
id INTEGER PRIMARY KEY,
building_id INTEGER NOT NULL,
site_energy_use_wn_kbtu FLOAT NOT NULL,
predicted BOOLEAN DEFAULT 0,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS building_models (
id INTEGER PRIMARY KEY,
ose_building_id INTEGER NOT NULL UNIQUE,
address VARCHAR(255) NOT NULL,
city VARCHAR(100) NULL,
state VARCHAR(50) NULL,
zip_code VARCHAR(20) NULL,
tax_parcel_identification_number VARCHAR(100) NULL,
council_district_code varchar(20) NULL,
latitude FLOAT NULL,
longitude FLOAT NULL,
year_built INTEGER NULL,
number_of_buildings INTEGER NULL,
number_of_floors INTEGER NULL,
property_gfa_total FLOAT NULL,
property_gfa_parking FLOAT NULL,
second_largest_property_use_type_gfa FLOAT NULL,
third_largest_property_use_type_gfa FLOAT NULL,
multiusage BOOLEAN DEFAULT 0,
steam BOOLEAN DEFAULT 0,
electricity BOOLEAN DEFAULT 0,
natural_gas BOOLEAN DEFAULT 0,
neighborhood_id INTEGER NULL,
building_type_id INTEGER NULL,
largest_property_use_type_id INTEGER NULL,
primary_property_type_id INTEGER NULL,
second_largest_property_use_type_id INTEGER NULL,
third_largest_property_use_type_id INTEGER NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (id) REFERENCES building_energy_predictions (building_id),
FOREIGN KEY (largest_property_use_type_id) REFERENCES property (id),
FOREIGN KEY (primary_property_type_id) REFERENCES property (id),
FOREIGN KEY (second_largest_property_use_type_id) REFERENCES property (id),
FOREIGN KEY (third_largest_property_use_type_id) REFERENCES property (id),
FOREIGN KEY (neighborhood_id) REFERENCES neighborhood (id),
FOREIGN KEY (building_type_id) REFERENCES building_type (id)
);
"""
)
conn.commit()
# Ne pas fermer la connexion - elle reste en mémoire
print("🗄️ Structure des tables créée avec succès!")
except Exception as e:
print(f"❌ Erreur lors de la création des tables: {e}")
raise
if __name__ == "__main__":
try:
print("🚀 Initialisation de la base de données SQLite...")
# Créer les tables avec raw SQL
create_sqlite_tables()
print("📊 Insertion des données de référence...")
init_sqlite_data()
print("✅ Base de données SQLite initialisée avec succès!")
except Exception as e:
print(f"❌ Erreur lors de l'initialisation: {e}")
import traceback
traceback.print_exc()
sys.exit(1)