Spaces:
Sleeping
Sleeping
File size: 25,213 Bytes
9fc6811 a12e87b 9fc6811 09241e4 45626f2 7ff8eef 9ec2493 9fc6811 09241e4 9ec2493 09241e4 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 7ff8eef a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 7ff8eef 45626f2 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 45626f2 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 45626f2 9ec2493 45626f2 9ec2493 45626f2 9ec2493 45626f2 a12e87b 45626f2 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 45626f2 9ec2493 45626f2 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 a12e87b 9ec2493 45626f2 a12e87b 45626f2 a12e87b 45626f2 9ec2493 a12e87b 9ec2493 a12e87b 45626f2 9ec2493 45626f2 9ec2493 45626f2 9fc6811 9ec2493 45626f2 09241e4 9fc6811 45626f2 9ec2493 a12e87b 45626f2 9ec2493 a12e87b 9ec2493 a12e87b 45626f2 9ec2493 a12e87b 9ec2493 a12e87b 45626f2 9ec2493 a12e87b 9ec2493 09241e4 7ff8eef 9ec2493 45626f2 09241e4 45626f2 9ec2493 09241e4 45626f2 09241e4 7ff8eef a12e87b 09241e4 9ec2493 09241e4 a12e87b 09241e4 9ec2493 45626f2 09241e4 45626f2 a12e87b 45626f2 09241e4 45626f2 09241e4 a12e87b 09241e4 9ec2493 45626f2 09241e4 45626f2 a12e87b 45626f2 7ff8eef 45626f2 09241e4 a12e87b 7ff8eef a12e87b 09241e4 7ff8eef 9ec2493 a12e87b 09241e4 7ff8eef 9fc6811 09241e4 9fc6811 9ec2493 9fc6811 9ec2493 45626f2 9ec2493 45626f2 78578c2 45626f2 9fc6811 09241e4 a12e87b 9ec2493 45626f2 a12e87b 9ec2493 a12e87b 09241e4 9ec2493 09241e4 45626f2 09241e4 9ec2493 a12e87b 9ec2493 9fc6811 9ec2493 45626f2 09241e4 9ec2493 09241e4 9fc6811 09241e4 9ec2493 09241e4 9ec2493 09241e4 9fc6811 09241e4 9fc6811 45626f2 bb71fb2 a12e87b bb71fb2 09241e4 bb71fb2 9ec2493 bb71fb2 09241e4 74c5fd4 09241e4 a12e87b 09241e4 9ec2493 09241e4 a12e87b 09241e4 13a2324 9ec2493 a12e87b 9fc6811 a12e87b b9ef820 09241e4 9ec2493 09241e4 9ec2493 09241e4 9ec2493 9fc6811 9ec2493 bb71fb2 9ec2493 09241e4 9ec2493 74c5fd4 a12e87b bb71fb2 45626f2 09241e4 9fc6811 a12e87b bb71fb2 |
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 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 |
import gradio as gr
import sqlite3
import pandas as pd
from huggingface_hub import hf_hub_download, HfApi, HfFolder
import os
import time
import shutil
from pathlib import Path
import json
# ===== CONFIGURATION =====
TARGET_LANGUAGES = ['de']
INDEXED_REPO_ID = "cstr/conceptnet-de-indexed"
INDEXED_DB_FILENAME = "conceptnet-de-indexed.db"
PROGRESS_FILENAME = "indexing_progress.json"
LOCAL_DB_PATH = "/tmp/conceptnet-indexed.db"
# =========================
print(f"π Filtering to: {', '.join([l.upper() for l in TARGET_LANGUAGES])}")
# Get HF token
HF_TOKEN = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_TOKEN")
if not HF_TOKEN:
try:
HF_TOKEN = HfFolder.get_token()
except:
pass
if not HF_TOKEN:
print("β οΈ WARNING: No HF_TOKEN found!")
print(" Add HF_TOKEN in Space settings to enable checkpointing")
# Original database
ORIGINAL_REPO_ID = "ysenarath/conceptnet-sqlite"
ORIGINAL_DB_FILENAME = "data/conceptnet-v5.7.0.db"
def log_progress(message, level="INFO"):
"""Enhanced logging with timestamp"""
timestamp = time.strftime("%H:%M:%S")
prefix = {
"INFO": "βΉοΈ ",
"SUCCESS": "β
",
"ERROR": "β",
"WARN": "β οΈ ",
"CHECKPOINT": "πΎ"
}.get(level, "")
print(f"[{timestamp}] {prefix} {message}")
def check_remote_progress():
"""Check remote progress with detailed logging"""
if not HF_TOKEN:
log_progress("No HF_TOKEN - cannot check remote progress", "WARN")
return {
"completed_indices": [],
"analyzed_tables": [],
"database_uploaded": False,
"indexing_complete": False
}
try:
api = HfApi()
# Check if repo exists
try:
api.repo_info(repo_id=INDEXED_REPO_ID, repo_type="dataset", token=HF_TOKEN)
log_progress(f"Repository exists: {INDEXED_REPO_ID}", "SUCCESS")
except:
log_progress("Repository doesn't exist yet", "INFO")
return {
"completed_indices": [],
"analyzed_tables": [],
"database_uploaded": False,
"indexing_complete": False
}
# Download progress file
try:
progress_path = hf_hub_download(
repo_id=INDEXED_REPO_ID,
filename=PROGRESS_FILENAME,
repo_type="dataset",
token=HF_TOKEN
)
with open(progress_path, 'r') as f:
progress = json.load(f)
log_progress("Remote progress loaded:", "INFO")
log_progress(f" Completed indices: {progress.get('completed_indices', [])}", "INFO")
log_progress(f" Analyzed tables: {progress.get('analyzed_tables', [])}", "INFO")
log_progress(f" Indexing complete: {progress.get('indexing_complete', False)}", "INFO")
return progress
except Exception as e:
log_progress("No progress file found (starting fresh)", "INFO")
return {
"completed_indices": [],
"analyzed_tables": [],
"database_uploaded": False,
"indexing_complete": False
}
except Exception as e:
log_progress(f"Error checking remote: {e}", "ERROR")
return {
"completed_indices": [],
"analyzed_tables": [],
"database_uploaded": False,
"indexing_complete": False
}
def update_remote_progress(completed_indices, analyzed_tables=None, database_uploaded=False, indexing_complete=False):
"""Update progress with detailed tracking"""
if not HF_TOKEN:
log_progress("Cannot update progress: No HF_TOKEN", "WARN")
return False
if analyzed_tables is None:
analyzed_tables = []
try:
api = HfApi()
# Create repo if needed
try:
api.repo_info(repo_id=INDEXED_REPO_ID, repo_type="dataset", token=HF_TOKEN)
except:
log_progress(f"Creating repository: {INDEXED_REPO_ID}", "INFO")
api.create_repo(
repo_id=INDEXED_REPO_ID,
repo_type="dataset",
token=HF_TOKEN,
private=False
)
# Create progress file
progress = {
"completed_indices": completed_indices,
"analyzed_tables": analyzed_tables,
"database_uploaded": database_uploaded,
"indexing_complete": indexing_complete,
"timestamp": time.time(),
"languages": TARGET_LANGUAGES
}
progress_path = "/tmp/indexing_progress.json"
with open(progress_path, 'w') as f:
json.dump(progress, f, indent=2)
# Upload
api.upload_file(
path_or_fileobj=progress_path,
path_in_repo=PROGRESS_FILENAME,
repo_id=INDEXED_REPO_ID,
repo_type="dataset",
token=HF_TOKEN,
commit_message=f"Progress: {len(completed_indices)} indices, {len(analyzed_tables)} tables analyzed"
)
log_progress(f"Progress updated: {len(completed_indices)} indices, {len(analyzed_tables)} tables analyzed", "CHECKPOINT")
return True
except Exception as e:
log_progress(f"Failed to update progress: {e}", "ERROR")
return False
def upload_database_checkpoint(message=""):
"""Upload database with progress reporting"""
if not HF_TOKEN:
log_progress("Cannot upload: No HF_TOKEN", "WARN")
return False
if not os.path.exists(LOCAL_DB_PATH):
log_progress("Database file doesn't exist", "ERROR")
return False
try:
api = HfApi()
db_size = os.path.getsize(LOCAL_DB_PATH) / (2**30)
log_progress(f"Uploading database checkpoint ({db_size:.2f} GB)...", "CHECKPOINT")
log_progress(f" {message}", "INFO")
log_progress(f" This may take 5-10 minutes...", "INFO")
start = time.time()
api.upload_file(
path_or_fileobj=LOCAL_DB_PATH,
path_in_repo=INDEXED_DB_FILENAME,
repo_id=INDEXED_REPO_ID,
repo_type="dataset",
token=HF_TOKEN,
commit_message=message or "Database checkpoint"
)
elapsed = time.time() - start
log_progress(f"Database uploaded in {elapsed:.1f}s ({db_size*8/elapsed:.1f} Mbps)", "SUCCESS")
return True
except Exception as e:
log_progress(f"Upload failed: {e}", "ERROR")
import traceback
traceback.print_exc()
return False
def create_indexed_database():
"""Create indexed database with comprehensive checkpointing"""
log_progress("="*60, "INFO")
log_progress("STARTING INDEXED DATABASE CREATION", "INFO")
log_progress("="*60, "INFO")
# Check remote progress
progress = check_remote_progress()
completed_indices = set(progress.get("completed_indices", []))
analyzed_tables = set(progress.get("analyzed_tables", []))
database_uploaded = progress.get("database_uploaded", False)
indexing_complete = progress.get("indexing_complete", False)
# If fully complete, download and return
if indexing_complete:
log_progress("Fully indexed database exists!", "SUCCESS")
log_progress(f"Downloading from {INDEXED_REPO_ID}...", "INFO")
try:
indexed_path = hf_hub_download(
repo_id=INDEXED_REPO_ID,
filename=INDEXED_DB_FILENAME,
repo_type="dataset",
token=HF_TOKEN
)
log_progress(f"Downloaded: {indexed_path}", "SUCCESS")
return indexed_path
except Exception as e:
log_progress(f"Download failed: {e}", "ERROR")
log_progress("Will create locally", "INFO")
# Check for partial progress
if completed_indices or analyzed_tables:
log_progress("Resuming from checkpoint:", "INFO")
log_progress(f" Completed indices: {sorted(completed_indices)}", "INFO")
log_progress(f" Analyzed tables: {sorted(analyzed_tables)}", "INFO")
# Get or create local database
if os.path.exists(LOCAL_DB_PATH) and (completed_indices or analyzed_tables):
log_progress("Using existing local database", "SUCCESS")
elif database_uploaded:
log_progress("Downloading partial database from HF...", "INFO")
try:
remote_db = hf_hub_download(
repo_id=INDEXED_REPO_ID,
filename=INDEXED_DB_FILENAME,
repo_type="dataset",
token=HF_TOKEN
)
shutil.copy2(remote_db, LOCAL_DB_PATH)
log_progress("Downloaded partial database", "SUCCESS")
except:
log_progress("No partial database, starting from original", "INFO")
if not os.path.exists(LOCAL_DB_PATH):
# Download and copy original
log_progress("Downloading original database...", "INFO")
original_path = hf_hub_download(
repo_id=ORIGINAL_REPO_ID,
filename=ORIGINAL_DB_FILENAME,
repo_type="dataset"
)
original_size = os.path.getsize(original_path)
free_space = shutil.disk_usage("/tmp")[2]
log_progress(f"Original size: {original_size / (2**30):.2f} GB", "INFO")
log_progress(f"Free space: {free_space / (2**30):.2f} GB", "INFO")
if free_space < original_size * 2:
raise Exception(f"Not enough space! Need {original_size * 2 / (2**30):.1f} GB")
log_progress(f"Copying to {LOCAL_DB_PATH}...", "INFO")
start = time.time()
shutil.copy2(original_path, LOCAL_DB_PATH)
elapsed = time.time() - start
log_progress(f"Copied in {elapsed:.1f}s", "SUCCESS")
# Connect to database
conn = sqlite3.connect(LOCAL_DB_PATH)
cursor = conn.cursor()
# Enable optimizations
cursor.execute("PRAGMA journal_mode = WAL")
cursor.execute("PRAGMA synchronous = NORMAL")
cursor.execute("PRAGMA cache_size = -512000")
# PHASE 1: Create Indices
log_progress("="*60, "INFO")
log_progress("PHASE 1: CREATING INDICES", "INFO")
log_progress("="*60, "INFO")
indices_to_create = [
("idx_edge_start_id", "edge", "start_id"),
("idx_edge_end_id", "edge", "end_id"),
("idx_edge_rel_id", "edge", "rel_id"),
("idx_node_label", "node", "label"),
]
for i, (idx_name, table, column) in enumerate(indices_to_create, 1):
if idx_name in completed_indices:
log_progress(f"[{i}/{len(indices_to_create)}] {idx_name} - SKIPPED (already complete)", "INFO")
continue
log_progress(f"[{i}/{len(indices_to_create)}] Creating {idx_name} on {table}({column})...", "INFO")
start = time.time()
try:
cursor.execute(f"CREATE INDEX IF NOT EXISTS {idx_name} ON {table}({column})")
conn.commit()
elapsed = time.time() - start
log_progress(f" Index created in {elapsed:.1f}s ({elapsed/60:.1f} min)", "SUCCESS")
# Update progress
completed_indices.add(idx_name)
update_remote_progress(
list(completed_indices),
list(analyzed_tables),
database_uploaded=False,
indexing_complete=False
)
# Upload checkpoint
upload_database_checkpoint(f"Checkpoint: {idx_name} created")
except Exception as e:
log_progress(f"Failed to create {idx_name}: {e}", "ERROR")
conn.close()
raise
# PHASE 2: Analyze Tables (per-table with checkpoints)
log_progress("="*60, "INFO")
log_progress("PHASE 2: ANALYZING TABLES", "INFO")
log_progress("="*60, "INFO")
# Get list of tables
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'")
tables = [row[0] for row in cursor.fetchall()]
log_progress(f"Found {len(tables)} tables to analyze: {tables}", "INFO")
for i, table in enumerate(tables, 1):
if table in analyzed_tables:
log_progress(f"[{i}/{len(tables)}] {table} - SKIPPED (already analyzed)", "INFO")
continue
log_progress(f"[{i}/{len(tables)}] Analyzing table: {table}", "INFO")
# Get table size for progress estimation
cursor.execute(f"SELECT COUNT(*) FROM {table}")
row_count = cursor.fetchone()[0]
log_progress(f" Table has {row_count:,} rows", "INFO")
start = time.time()
try:
# Run ANALYZE on this specific table
cursor.execute(f"ANALYZE {table}")
conn.commit()
elapsed = time.time() - start
log_progress(f" Analyzed in {elapsed:.1f}s ({elapsed/60:.1f} min)", "SUCCESS")
# Update progress
analyzed_tables.add(table)
update_remote_progress(
list(completed_indices),
list(analyzed_tables),
database_uploaded=False,
indexing_complete=False
)
# Upload checkpoint after each table
log_progress(f" Uploading checkpoint after analyzing {table}...", "CHECKPOINT")
upload_database_checkpoint(f"Checkpoint: {table} analyzed ({i}/{len(tables)})")
except Exception as e:
log_progress(f"Failed to analyze {table}: {e}", "ERROR")
log_progress("Continuing with next table...", "WARN")
conn.close()
# PHASE 3: Final upload and completion
log_progress("="*60, "INFO")
log_progress("PHASE 3: FINAL UPLOAD", "INFO")
log_progress("="*60, "INFO")
log_progress("All indexing and analysis complete!", "SUCCESS")
log_progress("Performing final upload...", "INFO")
upload_database_checkpoint("Final indexed database - COMPLETE")
# Mark as complete
update_remote_progress(
list(completed_indices),
list(analyzed_tables),
database_uploaded=True,
indexing_complete=True
)
indexed_size = os.path.getsize(LOCAL_DB_PATH)
log_progress("="*60, "SUCCESS")
log_progress("INDEXING COMPLETE!", "SUCCESS")
log_progress("="*60, "SUCCESS")
log_progress(f"Final size: {indexed_size / (2**30):.2f} GB", "INFO")
log_progress(f"Indices: {sorted(completed_indices)}", "INFO")
log_progress(f"Analyzed: {sorted(analyzed_tables)}", "INFO")
log_progress(f"Saved to: https://huggingface.co/datasets/{INDEXED_REPO_ID}", "INFO")
log_progress("="*60, "SUCCESS")
return LOCAL_DB_PATH
# Initialize database
DB_PATH = create_indexed_database()
def get_db_connection():
"""Create optimized connection"""
conn = sqlite3.connect(DB_PATH, check_same_thread=False)
conn.execute("PRAGMA cache_size = -256000")
conn.execute("PRAGMA mmap_size = 4294967296")
return conn
def verify_indices():
"""Verify indices"""
log_progress("="*60, "INFO")
log_progress("VERIFYING INDICES", "INFO")
log_progress("="*60, "INFO")
with get_db_connection() as conn:
cursor = conn.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='index' AND name LIKE 'idx_%'")
custom_indices = cursor.fetchall()
log_progress(f"Custom indices: {len(custom_indices)}", "INFO")
for idx in custom_indices:
log_progress(f" β {idx[0]}", "SUCCESS")
# Speed test
log_progress("Running speed test...", "INFO")
start = time.time()
cursor.execute("SELECT COUNT(*) FROM edge WHERE start_id LIKE '/c/de/hund%'")
count = cursor.fetchone()[0]
elapsed = time.time() - start
status = "SUCCESS" if elapsed < 1 else "WARN" if elapsed < 5 else "ERROR"
log_progress(f"Query: {count} results in {elapsed:.3f}s", status)
log_progress("="*60, "INFO")
verify_indices()
def get_semantic_profile(word, lang='de', progress=gr.Progress()):
"""Semantic profile"""
progress(0, desc="Starting...")
if not word:
return "β οΈ Please enter a word."
word = word.strip().lower().replace(' ', '_')
like_path = f"/c/{lang}/{word}%"
relations = [
"/r/IsA", "/r/PartOf", "/r/HasA", "/r/UsedFor", "/r/CapableOf",
"/r/Causes", "/r/HasProperty", "/r/Synonym", "/r/Antonym",
"/r/AtLocation", "/r/RelatedTo"
]
output_md = f"# π§ Semantic Profile: '{word}'\n\n"
try:
with get_db_connection() as conn:
cursor = conn.cursor()
progress(0.05, desc="Finding nodes...")
cursor.execute("SELECT id, label FROM node WHERE id LIKE ? LIMIT 5", (like_path,))
nodes = cursor.fetchall()
if not nodes:
return f"# π§ Semantic Profile: '{word}'\n\nβ οΈ **Not found**"
for node_id, label in nodes[:3]:
output_md += f"**Node:** `{node_id}` ({label})\n"
output_md += "\n"
total = 0
for i, rel in enumerate(relations):
progress((i + 1) / len(relations), desc=f"Querying {rel}...")
output_md += f"## {rel}\n\n"
found = False
# Outgoing
cursor.execute("""
SELECT en.label, e.weight
FROM edge e
JOIN node en ON e.end_id = en.id
JOIN relation r ON e.rel_id = r.id
WHERE e.start_id LIKE ? AND r.label = ?
ORDER BY e.weight DESC LIMIT 7
""", (like_path, rel))
for label, weight in cursor.fetchall():
output_md += f"- **{word}** {rel} β *{label}* `[{weight:.3f}]`\n"
found = True
total += 1
# Incoming
cursor.execute("""
SELECT s.label, e.weight
FROM edge e
JOIN node s ON e.start_id = s.id
JOIN relation r ON e.rel_id = r.id
WHERE e.end_id LIKE ? AND r.label = ?
ORDER BY e.weight DESC LIMIT 7
""", (like_path, rel))
for label, weight in cursor.fetchall():
output_md += f"- *{label}* {rel} β **{word}** `[{weight:.3f}]`\n"
found = True
total += 1
if not found:
output_md += "*No results*\n"
output_md += "\n"
progress(1.0, desc="Complete!")
output_md += f"---\n**Total:** {total} relations\n"
return output_md
except Exception as e:
import traceback
traceback.print_exc()
return f"**β Error:** {e}"
def run_query(start_node, relation, end_node, limit, progress=gr.Progress()):
"""Query builder"""
progress(0, desc="Starting...")
query = """
SELECT e.id, s.id, r.label, en.id, e.weight, s.label, en.label
FROM edge e
JOIN relation r ON e.rel_id = r.id
JOIN node s ON e.start_id = s.id
JOIN node en ON e.end_id = en.id
WHERE 1=1
"""
params = []
try:
with get_db_connection() as conn:
progress(0.3, desc="Building...")
# Language filter
lang_cond = []
for lang in TARGET_LANGUAGES:
lang_cond.append(f"s.id LIKE '/c/{lang}/%'")
lang_cond.append(f"en.id LIKE '/c/{lang}/%'")
query += f" AND ({' OR '.join(lang_cond)})"
if start_node and start_node.strip():
pattern = start_node if '%' in start_node else f"%{start_node}%"
query += " AND s.id LIKE ?"
params.append(pattern)
if relation and relation.strip():
rel_value = relation if relation.startswith('/r/') else f"/r/{relation}"
query += " AND r.label = ?" if '%' not in relation else " AND r.label LIKE ?"
params.append(rel_value)
if end_node and end_node.strip():
pattern = end_node if '%' in end_node else f"%{end_node}%"
query += " AND en.id LIKE ?"
params.append(pattern)
query += " ORDER BY e.weight DESC LIMIT ?"
params.append(limit)
progress(0.6, desc="Executing...")
start_time = time.time()
df = pd.read_sql_query(query, conn, params=params)
elapsed = time.time() - start_time
progress(1.0, desc="Complete!")
if df.empty:
return pd.DataFrame(), f"β οΈ No results ({elapsed:.2f}s)"
df.columns = ['edge_id', 'start_id', 'relation', 'end_id', 'weight', 'start_label', 'end_label']
return df, f"β
{len(df)} results in {elapsed:.2f}s"
except Exception as e:
import traceback
traceback.print_exc()
return pd.DataFrame(), f"**β Error:** {e}"
def run_raw_query(sql_query):
"""Raw SQL"""
if not sql_query.strip().upper().startswith("SELECT"):
return pd.DataFrame(), "Only SELECT allowed"
try:
with get_db_connection() as conn:
df = pd.read_sql_query(sql_query, conn)
return df, f"β
{len(df)} rows"
except Exception as e:
return pd.DataFrame(), f"Error: {e}"
def get_schema_info():
"""Schema info"""
with get_db_connection() as conn:
cursor = conn.cursor()
md = f"# π Schema\n\n**Repo:** [{INDEXED_REPO_ID}](https://huggingface.co/datasets/{INDEXED_REPO_ID})\n\n"
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'")
for table, in cursor.fetchall():
cursor.execute(f"SELECT COUNT(*) FROM {table}")
md += f"## {table} ({cursor.fetchone()[0]:,} rows)\n\n"
return md
# UI
with gr.Blocks(title="ConceptNet", theme=gr.themes.Soft()) as demo:
gr.Markdown(f"# π§ ConceptNet ({', '.join([l.upper() for l in TARGET_LANGUAGES])})")
gr.Markdown(f"**Repo:** [{INDEXED_REPO_ID}](https://huggingface.co/datasets/{INDEXED_REPO_ID}) | β
Per-table checkpoints")
with gr.Tabs():
with gr.TabItem("π Profile"):
with gr.Row():
word_input = gr.Textbox(label="Word", placeholder="hund", value="hund")
lang_input = gr.Dropdown(choices=TARGET_LANGUAGES, value=TARGET_LANGUAGES[0], label="Lang")
semantic_btn = gr.Button("π Get Profile", variant="primary", size="lg")
semantic_output = gr.Markdown()
with gr.TabItem("β‘ Query"):
with gr.Row():
start_input = gr.Textbox(label="Start", placeholder="hund", value="hund")
rel_input = gr.Textbox(label="Relation", placeholder="IsA", value="IsA")
end_input = gr.Textbox(label="End", placeholder="")
limit_slider = gr.Slider(label="Limit", minimum=1, maximum=200, value=50)
query_btn = gr.Button("βΆοΈ Run", variant="primary", size="lg")
status_output = gr.Markdown()
results_output = gr.DataFrame(wrap=True)
with gr.TabItem("π» SQL"):
raw_sql_input = gr.Textbox(label="SQL", value="SELECT * FROM edge WHERE start_id LIKE '/c/de/hund%' LIMIT 10", lines=3)
raw_btn = gr.Button("βΆοΈ Execute")
raw_status = gr.Markdown()
raw_results = gr.DataFrame()
with gr.TabItem("π Schema"):
schema_btn = gr.Button("π Load")
schema_output = gr.Markdown()
gr.Markdown("---\nβ
**Per-table ANALYZE with checkpoints!** Check server logs for detailed progress.")
semantic_btn.click(get_semantic_profile, [word_input, lang_input], semantic_output)
query_btn.click(run_query, [start_input, rel_input, end_input, limit_slider], [results_output, status_output])
raw_btn.click(run_raw_query, raw_sql_input, [raw_results, raw_status])
schema_btn.click(get_schema_info, None, schema_output)
if __name__ == "__main__":
log_progress("App ready with per-table ANALYZE checkpoints!", "SUCCESS")
demo.launch(ssr_mode=False) |