cstr commited on
Commit
1f46ad2
·
verified ·
1 Parent(s): bb71fb2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -12
app.py CHANGED
@@ -75,16 +75,18 @@ def run_query(start_node, relation, end_node, limit):
75
  Runs an efficient query by filtering on text paths first,
76
  then joining by indexed IDs.
77
 
78
- *** FIX ***: This now queries the `node.path` column, not `node.label`.
 
79
  """
80
  print(f"Running query: start='{start_node}', rel='{relation}', end='{end_node}'")
81
 
 
82
  select_clause = """
83
  SELECT
84
  e.id AS edge_id,
85
- s.path AS start_path,
86
  r.label AS relation_label,
87
- en.path AS end_path,
88
  e.weight,
89
  e.dataset,
90
  e.surface_text,
@@ -104,21 +106,25 @@ def run_query(start_node, relation, end_node, limit):
104
  params = []
105
 
106
  try:
107
- # --- FIX: Query `node.path` for nodes, `relation.label` for relations ---
108
  if start_node:
109
- op = "LIKE" if "%" in start_node else "="
110
- where_conditions.append(f"e.start_id IN (SELECT id FROM node WHERE path {op} ?)")
111
- params.append(start_node)
 
 
112
 
113
  if relation:
 
114
  op = "LIKE" if "%" in relation else "="
115
  where_conditions.append(f"e.rel_id IN (SELECT id FROM relation WHERE label {op} ?)")
116
  params.append(relation)
117
 
118
  if end_node:
119
- op = "LIKE" if "%" in end_node else "="
120
- where_conditions.append(f"e.end_id IN (SELECT id FROM node WHERE path {op} ?)")
121
- params.append(end_node)
 
122
  # --- End of Fix ---
123
 
124
  if not where_conditions:
@@ -182,9 +188,10 @@ with gr.Blocks(title="ConceptNet SQLite Explorer") as demo:
182
  gr.Markdown(
183
  "**Run a query against the database.**\n"
184
  "This query joins the `edge`, `node`, and `relation` tables to find connections.\n"
185
- "You can use SQL wildcards like `%` (e.g., `/c/en/dog%` or `/r/Is%`)."
186
  )
187
  with gr.Row():
 
188
  start_input = gr.Textbox(label="Start Node Path", placeholder="/c/en/dog")
189
  rel_input = gr.Textbox(label="Relation Path", placeholder="/r/IsA")
190
  end_input = gr.Textbox(label="End Node Path", placeholder="/c/en/animal")
@@ -197,9 +204,10 @@ with gr.Blocks(title="ConceptNet SQLite Explorer") as demo:
197
 
198
  with gr.TabItem("Raw SQL Query"):
199
  gr.Markdown("**Danger Zone:** Run a raw `SELECT` query against the database. Use the Schema Explorer tab to see table/column names.")
 
200
  raw_sql_input = gr.Textbox(
201
  label="Raw SQL Query",
202
- placeholder="SELECT s.path, s.label FROM node AS s WHERE s.label = 'dog' LIMIT 5",
203
  lines=5
204
  )
205
  raw_query_btn = gr.Button("Run Raw SQL", variant="secondary")
 
75
  Runs an efficient query by filtering on text paths first,
76
  then joining by indexed IDs.
77
 
78
+ *** FIX ***: This now queries the `node.id` column (which contains the full path)
79
+ and the `relation.label` column, based on the raw query results.
80
  """
81
  print(f"Running query: start='{start_node}', rel='{relation}', end='{end_node}'")
82
 
83
+ # --- FIX: Select the `id` column (which has the path) ---
84
  select_clause = """
85
  SELECT
86
  e.id AS edge_id,
87
+ s.id AS start_id_path,
88
  r.label AS relation_label,
89
+ en.id AS end_id_path,
90
  e.weight,
91
  e.dataset,
92
  e.surface_text,
 
106
  params = []
107
 
108
  try:
109
+ # --- FIX: Query `node.id` for nodes, `relation.label` for relations ---
110
  if start_node:
111
+ # We add a wildcard to the front to match the full http://... ID
112
+ # unless the user has already added their own wildcard.
113
+ param_val = start_node if "%" in start_node else f"%{start_node}"
114
+ where_conditions.append(f"e.start_id IN (SELECT id FROM node WHERE id LIKE ?)")
115
+ params.append(param_val)
116
 
117
  if relation:
118
+ # Relation table uses `label` which is a direct match (e.g., /r/IsA)
119
  op = "LIKE" if "%" in relation else "="
120
  where_conditions.append(f"e.rel_id IN (SELECT id FROM relation WHERE label {op} ?)")
121
  params.append(relation)
122
 
123
  if end_node:
124
+ # We add a wildcard to the front to match the full http://... ID
125
+ param_val = end_node if "%" in end_node else f"%{end_node}"
126
+ where_conditions.append(f"e.end_id IN (SELECT id FROM node WHERE id LIKE ?)")
127
+ params.append(param_val)
128
  # --- End of Fix ---
129
 
130
  if not where_conditions:
 
188
  gr.Markdown(
189
  "**Run a query against the database.**\n"
190
  "This query joins the `edge`, `node`, and `relation` tables to find connections.\n"
191
+ "Use the *path part* of the ID (e.g., `/c/en/dog` or `/r/Is%`)."
192
  )
193
  with gr.Row():
194
+ # --- FIX: Updated labels for clarity ---
195
  start_input = gr.Textbox(label="Start Node Path", placeholder="/c/en/dog")
196
  rel_input = gr.Textbox(label="Relation Path", placeholder="/r/IsA")
197
  end_input = gr.Textbox(label="End Node Path", placeholder="/c/en/animal")
 
204
 
205
  with gr.TabItem("Raw SQL Query"):
206
  gr.Markdown("**Danger Zone:** Run a raw `SELECT` query against the database. Use the Schema Explorer tab to see table/column names.")
207
+ # --- FIX: Updated placeholder to a useful, correct query ---
208
  raw_sql_input = gr.Textbox(
209
  label="Raw SQL Query",
210
+ placeholder="SELECT id, label, language, path FROM node WHERE id LIKE '%/c/en/dog' LIMIT 5",
211
  lines=5
212
  )
213
  raw_query_btn = gr.Button("Run Raw SQL", variant="secondary")