Spaces:
Runtime error
Runtime error
File size: 1,829 Bytes
96a945a | 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 54 55 56 | import os
import aiosqlite
# Use persistent storage on Fly.io / HF Spaces, local file otherwise
if os.environ.get("FLY_APP_NAME") or os.environ.get("SPACE_ID"):
DB_PATH = "/data/responses.db"
else:
DB_PATH = os.path.join(os.path.dirname(__file__), "..", "responses.db")
async def get_db() -> aiosqlite.Connection:
db = await aiosqlite.connect(DB_PATH)
db.row_factory = aiosqlite.Row
await db.execute("PRAGMA journal_mode=WAL")
return db
async def init_db():
os.makedirs(os.path.dirname(DB_PATH) if os.path.dirname(DB_PATH) else ".", exist_ok=True)
db = await get_db()
try:
await db.execute("""
CREATE TABLE IF NOT EXISTS sessions (
session_id TEXT PRIMARY KEY,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
expertise TEXT,
colorblind INTEGER DEFAULT 0,
device TEXT,
cb_redgreen TEXT DEFAULT '',
cb_blueyellow TEXT DEFAULT '',
trials TEXT,
completed INTEGER DEFAULT 0
)
""")
await db.execute("""
CREATE TABLE IF NOT EXISTS responses (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT,
trial_index INTEGER,
image_id TEXT,
image_path TEXT,
method TEXT,
variant TEXT,
dataset TEXT,
base_id TEXT,
label TEXT,
response TEXT,
correct INTEGER,
response_time_ms INTEGER,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
""")
await db.commit()
finally:
await db.close()
|