huggingface-projects/diffusers-gallery-bot
6
1import sqlite32from pathlib import Path3import math4 5def power(x, y):6 return math.pow(x, y)7 8 9 10class Database:11 def __init__(self, db_path=None):12 if db_path is None:13 raise ValueError("db_path must be provided")14 self.db_path = db_path15 self.db_file = self.db_path / "models.db"16 if not self.db_file.exists():17 print("Creating database")18 print("DB_FILE", self.db_file)19 db = sqlite3.connect(self.db_file)20 with open(Path("schema.sql"), "r") as f:21 db.executescript(f.read())22 db.commit()23 db.close()24 25 def get_db(self):26 db = sqlite3.connect(self.db_file, check_same_thread=False)27 db.create_function("MYPOWER", 2, power)28 db.row_factory = sqlite3.Row29 return db30 31 def __enter__(self):32 self.db = self.get_db()33 return self.db34 35 def __exit__(self, exc_type, exc_value, traceback):36 self.db.close()37 