Spaces:
Sleeping
Sleeping
File size: 16,153 Bytes
b0c3a57 a99c555 749d250 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 5b7432c b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 5b7432c b0c3a57 5b7432c b0c3a57 5b7432c b0c3a57 5b7432c b0c3a57 5b7432c b0c3a57 a99c555 b0c3a57 5b7432c b0c3a57 5b7432c b0c3a57 5b7432c b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 a99c555 b0c3a57 |
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
"""OphthalmoCapture — Database Layer (Metadata Only)
Option B: The database persists annotation metadata (labels, transcriptions,
doctor info, timestamps) for audit and history. It NEVER stores images or audio.
"""
import os
import datetime
import sqlite3
# Try importing firebase_admin
try:
import firebase_admin
from firebase_admin import credentials, firestore
FIREBASE_AVAILABLE = True
except ImportError:
FIREBASE_AVAILABLE = False
DB_TYPE = "SQLITE"
# Use /tmp for writable storage in Docker (ephemeral but always writable).
# Locally, falls back to the script's own directory.
_DB_DIR = "/tmp" if os.path.isdir("/tmp") else os.path.dirname(os.path.abspath(__file__))
DB_FILE = os.path.join(_DB_DIR, "annotations.db")
db_ref = None
def init_db():
"""Initialize the database connection (Firebase or SQLite fallback)."""
global DB_TYPE, db_ref
# Try Firebase first
if FIREBASE_AVAILABLE and os.path.exists("serviceAccountKey.json"):
try:
if not firebase_admin._apps:
cred = credentials.Certificate("serviceAccountKey.json")
firebase_admin.initialize_app(cred)
db_ref = firestore.client()
DB_TYPE = "FIREBASE"
return "FIREBASE"
except Exception as e:
print(f"Firebase init failed: {e}")
# Fallback to SQLite
try:
conn = sqlite3.connect(DB_FILE, check_same_thread=False)
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS annotations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
image_filename TEXT NOT NULL,
label TEXT,
transcription TEXT,
doctor_name TEXT DEFAULT '',
created_at DATETIME
)''')
c.execute('''CREATE INDEX IF NOT EXISTS idx_ann_filename
ON annotations (image_filename)''')
# Migration: add session_id column if it doesn't exist yet
try:
c.execute("ALTER TABLE annotations ADD COLUMN session_id TEXT DEFAULT ''")
except sqlite3.OperationalError:
pass # column already exists
# Migration: add locs_data column (JSON string)
try:
c.execute("ALTER TABLE annotations ADD COLUMN locs_data TEXT DEFAULT ''")
except sqlite3.OperationalError:
pass # column already exists
c.execute('''CREATE INDEX IF NOT EXISTS idx_ann_session
ON annotations (image_filename, session_id)''')
conn.commit()
conn.close()
DB_TYPE = "SQLITE"
return "SQLITE"
except Exception as e:
raise Exception(f"Database initialization failed: {e}")
def save_annotation(image_filename, label, transcription, doctor_name=""):
"""Save an annotation record (always INSERT). Stores metadata only."""
timestamp = datetime.datetime.now()
if DB_TYPE == "FIREBASE":
db_ref.collection("annotations").add({
"imageFilename": image_filename,
"label": label,
"transcription": transcription,
"doctorName": doctor_name,
"createdAt": timestamp,
})
else:
conn = sqlite3.connect(DB_FILE, check_same_thread=False)
c = conn.cursor()
c.execute(
"INSERT INTO annotations "
"(image_filename, label, transcription, doctor_name, created_at) "
"VALUES (?, ?, ?, ?, ?)",
(image_filename, label, transcription, doctor_name, timestamp),
)
conn.commit()
conn.close()
def save_or_update_annotation(
image_filename, label, transcription, doctor_name="", session_id="",
locs_data=None,
):
"""Upsert: within the same session, keep only ONE record per image.
If a record for (image_filename, session_id) already exists → UPDATE it.
Otherwise → INSERT a new one.
"""
import json as _json
timestamp = datetime.datetime.now()
locs_json = _json.dumps(locs_data or {}, ensure_ascii=False)
if DB_TYPE == "FIREBASE":
# Query for existing doc with matching filename + session
docs = list(
db_ref.collection("annotations")
.where("imageFilename", "==", image_filename)
.where("sessionId", "==", session_id)
.limit(1)
.stream()
)
if docs:
docs[0].reference.update({
"label": label,
"transcription": transcription,
"doctorName": doctor_name,
"locsData": locs_data or {},
"createdAt": timestamp,
})
else:
db_ref.collection("annotations").add({
"imageFilename": image_filename,
"label": label,
"transcription": transcription,
"doctorName": doctor_name,
"sessionId": session_id,
"locsData": locs_data or {},
"createdAt": timestamp,
})
else:
conn = sqlite3.connect(DB_FILE, check_same_thread=False)
c = conn.cursor()
# Check if a row for this image+session already exists
c.execute(
"SELECT id FROM annotations "
"WHERE image_filename = ? AND session_id = ? LIMIT 1",
(image_filename, session_id),
)
row = c.fetchone()
if row:
c.execute(
"UPDATE annotations "
"SET label = ?, transcription = ?, doctor_name = ?, "
"created_at = ?, locs_data = ? "
"WHERE id = ?",
(label, transcription, doctor_name, timestamp, locs_json, row[0]),
)
else:
c.execute(
"INSERT INTO annotations "
"(image_filename, label, transcription, doctor_name, "
"created_at, session_id, locs_data) "
"VALUES (?, ?, ?, ?, ?, ?, ?)",
(image_filename, label, transcription, doctor_name,
timestamp, session_id, locs_json),
)
conn.commit()
conn.close()
def get_latest_annotation(image_filename):
"""Retrieve the most recent annotation for a given image filename."""
if DB_TYPE == "FIREBASE":
docs = (
db_ref.collection("annotations")
.where("imageFilename", "==", image_filename)
.order_by("createdAt", direction=firestore.Query.DESCENDING)
.limit(1)
.stream()
)
for doc in docs:
return doc.to_dict()
return None
else:
conn = sqlite3.connect(DB_FILE, check_same_thread=False)
c = conn.cursor()
c.execute(
"SELECT image_filename, label, transcription, doctor_name, created_at "
"FROM annotations WHERE image_filename = ? ORDER BY id DESC LIMIT 1",
(image_filename,),
)
row = c.fetchone()
conn.close()
if row:
return {
"imageFilename": row[0],
"label": row[1],
"transcription": row[2],
"doctorName": row[3],
"createdAt": row[4],
}
return None
def get_history_paginated(search_query="", page=1, per_page=10):
"""Retrieve annotation history with search and pagination.
Returns: (list_of_items, total_count)
"""
offset = (page - 1) * per_page
history = []
total_count = 0
if DB_TYPE == "FIREBASE":
ref = db_ref.collection("annotations")
if search_query:
query = (
ref.where("imageFilename", ">=", search_query)
.where("imageFilename", "<=", search_query + "\uf8ff")
)
else:
query = ref.order_by("createdAt", direction=firestore.Query.DESCENDING)
all_docs = list(query.stream())
total_count = len(all_docs)
for doc in all_docs[offset : offset + per_page]:
history.append(doc.to_dict())
else:
conn = sqlite3.connect(DB_FILE, check_same_thread=False)
c = conn.cursor()
# Count
if search_query:
c.execute(
"SELECT COUNT(*) FROM annotations WHERE image_filename LIKE ?",
(f"%{search_query}%",),
)
else:
c.execute("SELECT COUNT(*) FROM annotations")
total_count = c.fetchone()[0]
# Fetch page
sql = (
"SELECT image_filename, label, transcription, doctor_name, created_at "
"FROM annotations"
)
params = []
if search_query:
sql += " WHERE image_filename LIKE ?"
params.append(f"%{search_query}%")
sql += " ORDER BY id DESC LIMIT ? OFFSET ?"
params.extend([per_page, offset])
c.execute(sql, params)
for row in c.fetchall():
history.append({
"imageFilename": row[0],
"label": row[1],
"transcription": row[2],
"doctorName": row[3],
"createdAt": row[4],
})
conn.close()
return history, total_count
def get_annotation_stats():
"""Get summary statistics of all stored annotations."""
if DB_TYPE == "FIREBASE":
docs = list(db_ref.collection("annotations").stream())
total = len(docs)
labels = {}
for doc in docs:
lbl = doc.to_dict().get("label", "sin_etiqueta")
labels[lbl] = labels.get(lbl, 0) + 1
return {"total": total, "by_label": labels}
else:
conn = sqlite3.connect(DB_FILE, check_same_thread=False)
c = conn.cursor()
c.execute("SELECT COUNT(*) FROM annotations")
total = c.fetchone()[0]
c.execute("SELECT label, COUNT(*) FROM annotations GROUP BY label")
labels = {row[0]: row[1] for row in c.fetchall()}
conn.close()
return {"total": total, "by_label": labels}
def get_previously_labeled_filenames(filenames: list[str]) -> dict[str, list[dict]]:
"""Check which filenames have been previously annotated in the DB.
Returns a dict mapping filename → list of annotation records.
Only filenames with at least one record are included.
"""
if not filenames:
return {}
result = {}
if DB_TYPE == "FIREBASE":
# Firestore doesn't support 'IN' with >30 items, so batch
for fname in filenames:
docs = (
db_ref.collection("annotations")
.where("imageFilename", "==", fname)
.order_by("createdAt", direction=firestore.Query.DESCENDING)
.stream()
)
records = [doc.to_dict() for doc in docs]
if records:
result[fname] = records
else:
conn = sqlite3.connect(DB_FILE, check_same_thread=False)
c = conn.cursor()
placeholders = ",".join("?" for _ in filenames)
c.execute(
f"SELECT image_filename, label, transcription, doctor_name, created_at "
f"FROM annotations WHERE image_filename IN ({placeholders}) "
f"ORDER BY created_at DESC",
filenames,
)
for row in c.fetchall():
fname = row[0]
record = {
"imageFilename": row[0],
"label": row[1],
"transcription": row[2],
"doctorName": row[3],
"createdAt": row[4],
}
result.setdefault(fname, []).append(record)
conn.close()
return result
def get_all_annotations_for_file(image_filename: str) -> list[dict]:
"""Retrieve ALL annotations for a given image filename, ordered by date desc."""
if DB_TYPE == "FIREBASE":
docs = (
db_ref.collection("annotations")
.where("imageFilename", "==", image_filename)
.order_by("createdAt", direction=firestore.Query.DESCENDING)
.stream()
)
return [doc.to_dict() for doc in docs]
else:
conn = sqlite3.connect(DB_FILE, check_same_thread=False)
c = conn.cursor()
c.execute(
"SELECT image_filename, label, transcription, doctor_name, created_at "
"FROM annotations WHERE image_filename = ? ORDER BY created_at DESC",
(image_filename,),
)
results = []
for row in c.fetchall():
results.append({
"imageFilename": row[0],
"label": row[1],
"transcription": row[2],
"doctorName": row[3],
"createdAt": row[4],
})
conn.close()
return results
def get_history_grouped(search_query="", page=1, per_page=10):
"""Retrieve annotation history GROUPED by image filename.
Returns: (list_of_groups, total_unique_images)
Each group = {"imageFilename": str, "annotations": [list of records]}
sorted by most recent annotation date per image.
"""
offset = (page - 1) * per_page
if DB_TYPE == "FIREBASE":
ref = db_ref.collection("annotations")
if search_query:
query = (
ref.where("imageFilename", ">=", search_query)
.where("imageFilename", "<=", search_query + "\uf8ff")
)
else:
query = ref.order_by("createdAt", direction=firestore.Query.DESCENDING)
all_docs = [doc.to_dict() for doc in query.stream()]
# Group by filename
grouped = {}
for doc in all_docs:
fname = doc.get("imageFilename", "")
grouped.setdefault(fname, []).append(doc)
# Sort groups by most recent annotation
sorted_groups = sorted(
grouped.items(),
key=lambda x: max(str(a.get("createdAt", "")) for a in x[1]),
reverse=True,
)
total_unique = len(sorted_groups)
page_groups = sorted_groups[offset:offset + per_page]
result = []
for fname, annotations in page_groups:
result.append({
"imageFilename": fname,
"annotations": sorted(
annotations,
key=lambda a: str(a.get("createdAt", "")),
reverse=True,
),
})
return result, total_unique
else:
conn = sqlite3.connect(DB_FILE, check_same_thread=False)
c = conn.cursor()
# Count unique filenames
where = ""
params = []
if search_query:
where = " WHERE image_filename LIKE ?"
params.append(f"%{search_query}%")
c.execute(
f"SELECT COUNT(DISTINCT image_filename) FROM annotations{where}",
params,
)
total_unique = c.fetchone()[0]
# Get unique filenames for this page, sorted by most recent
c.execute(
f"SELECT image_filename, MAX(created_at) as latest "
f"FROM annotations{where} "
f"GROUP BY image_filename ORDER BY latest DESC "
f"LIMIT ? OFFSET ?",
params + [per_page, offset],
)
page_filenames = [row[0] for row in c.fetchall()]
# Fetch all annotations for those filenames
result = []
for fname in page_filenames:
c.execute(
"SELECT image_filename, label, transcription, doctor_name, created_at "
"FROM annotations WHERE image_filename = ? ORDER BY created_at DESC",
(fname,),
)
annotations = []
for row in c.fetchall():
annotations.append({
"imageFilename": row[0],
"label": row[1],
"transcription": row[2],
"doctorName": row[3],
"createdAt": row[4],
})
result.append({
"imageFilename": fname,
"annotations": annotations,
})
conn.close()
return result, total_unique
|