Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import sqlite3
|
| 3 |
import pandas as pd
|
|
@@ -10,7 +41,7 @@ from collections import defaultdict
|
|
| 10 |
|
| 11 |
# ===== CONFIGURATION =====
|
| 12 |
# 1. Point to the NEW normalized database (fixed)
|
| 13 |
-
TARGET_LANGUAGES = ['
|
| 14 |
NORMALIZED_REPO_ID = "cstr/conceptnet-normalized-multi"
|
| 15 |
NORMALIZED_DB_FILE = "conceptnet_normalized.db"
|
| 16 |
|
|
@@ -102,15 +133,15 @@ def get_db_connection():
|
|
| 102 |
return conn
|
| 103 |
|
| 104 |
def node_url_to_label(url: str) -> str:
|
| 105 |
-
"""Extract the term from ConceptNet URL: /c/{lang}/{term}/..."""
|
| 106 |
try:
|
| 107 |
parts = url.split('/')
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
return parts[
|
| 111 |
except:
|
| 112 |
pass
|
| 113 |
-
return url #
|
| 114 |
|
| 115 |
def get_semantic_profile(word: str, lang: str = 'en', selected_relations: List[str] = None, progress=gr.Progress()):
|
| 116 |
"""
|
|
@@ -287,12 +318,11 @@ def get_semantic_profile(word: str, lang: str = 'en', selected_relations: List[s
|
|
| 287 |
traceback.print_exc()
|
| 288 |
yield f"**❌ Error:** {e}"
|
| 289 |
|
| 290 |
-
def run_query(start_node, relation, end_node, limit, progress=gr.Progress()):
|
| 291 |
"""
|
| 292 |
-
--- REWRITTEN FOR NORMALIZED DB ---
|
| 293 |
Query builder using fast integer joins.
|
| 294 |
"""
|
| 295 |
-
log_progress(f"Query: start={start_node}, rel={relation}, end={end_node}", "INFO")
|
| 296 |
progress(0, desc="Building...")
|
| 297 |
|
| 298 |
if not DB_PATH:
|
|
@@ -318,12 +348,12 @@ def run_query(start_node, relation, end_node, limit, progress=gr.Progress()):
|
|
| 318 |
with get_db_connection() as conn:
|
| 319 |
progress(0.3, desc="Adding filters...")
|
| 320 |
|
| 321 |
-
# Start node
|
| 322 |
if start_node and start_node.strip():
|
| 323 |
if start_node.startswith('http://'):
|
| 324 |
pattern = f"{start_node}%"
|
| 325 |
else:
|
| 326 |
-
pattern = f"{CONCEPTNET_BASE}/c/
|
| 327 |
where_clauses.append("n_start.node_url LIKE ?")
|
| 328 |
params.append(pattern)
|
| 329 |
|
|
@@ -334,12 +364,12 @@ def run_query(start_node, relation, end_node, limit, progress=gr.Progress()):
|
|
| 334 |
where_clauses.append("r.rel_url = ?")
|
| 335 |
params.append(rel_value)
|
| 336 |
|
| 337 |
-
# End node
|
| 338 |
if end_node and end_node.strip():
|
| 339 |
if end_node.startswith('http://'):
|
| 340 |
pattern = f"{end_node}%"
|
| 341 |
else:
|
| 342 |
-
pattern = f"{CONCEPTNET_BASE}/c/
|
| 343 |
where_clauses.append("n_end.node_url LIKE ?")
|
| 344 |
params.append(pattern)
|
| 345 |
|
|
@@ -472,6 +502,7 @@ with gr.Blocks(title="ConceptNet Explorer", theme=gr.themes.Soft()) as demo:
|
|
| 472 |
|
| 473 |
with gr.Row():
|
| 474 |
start_input = gr.Textbox(label="Start Node (word)", placeholder="dog (optional)")
|
|
|
|
| 475 |
rel_input = gr.Dropdown(
|
| 476 |
choices=[""] + list(CONCEPTNET_RELATIONS.keys()),
|
| 477 |
label="Relation (name)",
|
|
@@ -479,6 +510,7 @@ with gr.Blocks(title="ConceptNet Explorer", theme=gr.themes.Soft()) as demo:
|
|
| 479 |
info="Leave blank to query all relations"
|
| 480 |
)
|
| 481 |
end_input = gr.Textbox(label="End Node (word)", placeholder="(optional)")
|
|
|
|
| 482 |
|
| 483 |
limit_slider = gr.Slider(label="Limit", minimum=1, maximum=500, value=50, step=1)
|
| 484 |
query_btn = gr.Button("▶️ Run Query", variant="primary", size="lg")
|
|
@@ -530,7 +562,7 @@ LIMIT 10
|
|
| 530 |
|
| 531 |
query_btn.click(
|
| 532 |
run_query,
|
| 533 |
-
inputs=[start_input, rel_input, end_input, limit_slider],
|
| 534 |
outputs=[results_output, status_output],
|
| 535 |
api_name="run_query"
|
| 536 |
)
|
|
@@ -560,4 +592,4 @@ if __name__ == "__main__":
|
|
| 560 |
log_progress("APP READY! (Normalized DB)", "SUCCESS")
|
| 561 |
else:
|
| 562 |
log_progress("APP LAUNCHING WITH ERRORS (DB NOT FOUND)", "ERROR")
|
| 563 |
-
demo.launch(ssr_mode=False)
|
|
|
|
| 1 |
+
|
| 2 |
+
Hugging Face's logo Hugging Face
|
| 3 |
+
|
| 4 |
+
Models
|
| 5 |
+
Datasets
|
| 6 |
+
Spaces
|
| 7 |
+
Docs
|
| 8 |
+
Pricing
|
| 9 |
+
|
| 10 |
+
Spaces:
|
| 11 |
+
cstr
|
| 12 |
+
/
|
| 13 |
+
conceptnet_normalized
|
| 14 |
+
App
|
| 15 |
+
Files
|
| 16 |
+
Community
|
| 17 |
+
Settings
|
| 18 |
+
conceptnet_normalized
|
| 19 |
+
/ app.py
|
| 20 |
+
cstr's picture
|
| 21 |
+
cstr
|
| 22 |
+
Update app.py
|
| 23 |
+
fefc16c
|
| 24 |
+
verified
|
| 25 |
+
3 minutes ago
|
| 26 |
+
raw
|
| 27 |
+
history
|
| 28 |
+
blame
|
| 29 |
+
edit
|
| 30 |
+
delete
|
| 31 |
+
23.4 kB
|
| 32 |
import gradio as gr
|
| 33 |
import sqlite3
|
| 34 |
import pandas as pd
|
|
|
|
| 41 |
|
| 42 |
# ===== CONFIGURATION =====
|
| 43 |
# 1. Point to the NEW normalized database (fixed)
|
| 44 |
+
TARGET_LANGUAGES = ['en', 'fr', 'it', 'de', 'es', 'ar', 'fa', 'grc', 'he', 'la', 'hbo']
|
| 45 |
NORMALIZED_REPO_ID = "cstr/conceptnet-normalized-multi"
|
| 46 |
NORMALIZED_DB_FILE = "conceptnet_normalized.db"
|
| 47 |
|
|
|
|
| 133 |
return conn
|
| 134 |
|
| 135 |
def node_url_to_label(url: str) -> str:
|
| 136 |
+
"""Extract the term from ConceptNet URL: http://conceptnet.io/c/{lang}/{term}/..."""
|
| 137 |
try:
|
| 138 |
parts = url.split('/')
|
| 139 |
+
# Term is ALWAYS at index 5
|
| 140 |
+
if len(parts) >= 6 and parts[3] == 'c':
|
| 141 |
+
return parts[5].replace('_', ' ')
|
| 142 |
except:
|
| 143 |
pass
|
| 144 |
+
return url # Fallback to full URL if parsing fails
|
| 145 |
|
| 146 |
def get_semantic_profile(word: str, lang: str = 'en', selected_relations: List[str] = None, progress=gr.Progress()):
|
| 147 |
"""
|
|
|
|
| 318 |
traceback.print_exc()
|
| 319 |
yield f"**❌ Error:** {e}"
|
| 320 |
|
| 321 |
+
def run_query(start_node, start_lang, relation, end_node, end_lang, limit, progress=gr.Progress()):
|
| 322 |
"""
|
|
|
|
| 323 |
Query builder using fast integer joins.
|
| 324 |
"""
|
| 325 |
+
log_progress(f"Query: start={start_node} ({start_lang}), rel={relation}, end={end_node} ({end_lang})", "INFO")
|
| 326 |
progress(0, desc="Building...")
|
| 327 |
|
| 328 |
if not DB_PATH:
|
|
|
|
| 348 |
with get_db_connection() as conn:
|
| 349 |
progress(0.3, desc="Adding filters...")
|
| 350 |
|
| 351 |
+
# Start node - USE start_lang
|
| 352 |
if start_node and start_node.strip():
|
| 353 |
if start_node.startswith('http://'):
|
| 354 |
pattern = f"{start_node}%"
|
| 355 |
else:
|
| 356 |
+
pattern = f"{CONCEPTNET_BASE}/c/{start_lang}/{start_node.strip().lower().replace(' ', '_')}%"
|
| 357 |
where_clauses.append("n_start.node_url LIKE ?")
|
| 358 |
params.append(pattern)
|
| 359 |
|
|
|
|
| 364 |
where_clauses.append("r.rel_url = ?")
|
| 365 |
params.append(rel_value)
|
| 366 |
|
| 367 |
+
# End node - USE end_lang
|
| 368 |
if end_node and end_node.strip():
|
| 369 |
if end_node.startswith('http://'):
|
| 370 |
pattern = f"{end_node}%"
|
| 371 |
else:
|
| 372 |
+
pattern = f"{CONCEPTNET_BASE}/c/{end_lang}/{end_node.strip().lower().replace(' ', '_')}%"
|
| 373 |
where_clauses.append("n_end.node_url LIKE ?")
|
| 374 |
params.append(pattern)
|
| 375 |
|
|
|
|
| 502 |
|
| 503 |
with gr.Row():
|
| 504 |
start_input = gr.Textbox(label="Start Node (word)", placeholder="dog (optional)")
|
| 505 |
+
start_lang = gr.Dropdown(choices=TARGET_LANGUAGES, value="en", label="Start Lang", scale=1)
|
| 506 |
rel_input = gr.Dropdown(
|
| 507 |
choices=[""] + list(CONCEPTNET_RELATIONS.keys()),
|
| 508 |
label="Relation (name)",
|
|
|
|
| 510 |
info="Leave blank to query all relations"
|
| 511 |
)
|
| 512 |
end_input = gr.Textbox(label="End Node (word)", placeholder="(optional)")
|
| 513 |
+
end_lang = gr.Dropdown(choices=TARGET_LANGUAGES, value="en", label="End Lang", scale=1)
|
| 514 |
|
| 515 |
limit_slider = gr.Slider(label="Limit", minimum=1, maximum=500, value=50, step=1)
|
| 516 |
query_btn = gr.Button("▶️ Run Query", variant="primary", size="lg")
|
|
|
|
| 562 |
|
| 563 |
query_btn.click(
|
| 564 |
run_query,
|
| 565 |
+
inputs=[start_input, start_lang, rel_input, end_input, end_lang, limit_slider],
|
| 566 |
outputs=[results_output, status_output],
|
| 567 |
api_name="run_query"
|
| 568 |
)
|
|
|
|
| 592 |
log_progress("APP READY! (Normalized DB)", "SUCCESS")
|
| 593 |
else:
|
| 594 |
log_progress("APP LAUNCHING WITH ERRORS (DB NOT FOUND)", "ERROR")
|
| 595 |
+
demo.launch(ssr_mode=False)
|