cstr commited on
Commit
78578c2
·
verified ·
1 Parent(s): b945468

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -26
app.py CHANGED
@@ -73,7 +73,7 @@ def get_schema_info():
73
  schema_md += f"| `{name}` | `{dtype}` | {notnull} | {pk} |\n"
74
  schema_md += "\n"
75
 
76
- # --- FIX: Moved return statement outside the loop ---
77
  return schema_md
78
  # --- End of Fix ---
79
 
@@ -84,39 +84,49 @@ def get_schema_info():
84
 
85
  def run_query(start_node, relation, end_node, limit):
86
  """
87
- Runs a query against the ConceptNet database.
88
-
89
- *** ASSUMPTION ***:
90
- This function *assumes* the main table is named 'edges' and has
91
- columns 'start', 'rel', and 'end'.
92
-
93
- Use the 'Schema Explorer' tab to verify this. If the names are
94
- different (e.g., 'conceptnet_edges'), you must edit this function.
95
  """
96
  print(f"Running query: start='{start_node}', rel='{relation}', end='{end_node}'")
97
 
98
- # --- !! EDIT THESE IF SCHEMA IS DIFFERENT !! ---
99
- TABLE_NAME = "edges"
100
- START_COL = "start"
101
- REL_COL = "rel"
102
- END_COL = "end"
103
- # ---
104
 
105
- query = f"SELECT * FROM {TABLE_NAME} WHERE 1=1"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  params = []
107
 
108
  try:
109
  if start_node:
110
- query += f" AND {START_COL} LIKE ?"
111
  params.append(start_node)
112
  if relation:
113
- query += f" AND {REL_COL} LIKE ?"
114
  params.append(relation)
115
  if end_node:
116
- query += f" AND {END_COL} LIKE ?"
117
  params.append(end_node)
118
 
119
- query += " LIMIT ?"
120
  params.append(limit)
121
 
122
  print(f"Executing SQL: {query}")
@@ -133,8 +143,9 @@ def run_query(start_node, relation, end_node, limit):
133
  except Exception as e:
134
  print(f"Error in run_query: {e}")
135
  traceback.print_exc()
136
- err_msg = f"**Query Failed!**\n\n`{e}`\n\n**Tip:** Did you check the 'Schema Explorer' tab? The table name might not be `{TABLE_NAME}` or the columns might be different."
137
  return pd.DataFrame(), err_msg
 
138
 
139
  # --- 3. Build the Gradio UI ---
140
 
@@ -149,15 +160,17 @@ with gr.Blocks(title="ConceptNet SQLite Explorer") as demo:
149
  schema_output = gr.Markdown("Schema will appear here...")
150
 
151
  with gr.TabItem("Query Edges"):
 
152
  gr.Markdown(
153
  "**Run a query against the database.**\n"
154
- f"This tab *assumes* the table is named `edges` and columns are `start`, `rel`, and `end`.\n"
155
- "Use the 'Schema Explorer' to confirm. You can use SQL wildcards like `%` (e.g., `/c/en/dog%`)."
156
  )
 
157
  with gr.Row():
158
- start_input = gr.Textbox(label="Start Node", placeholder="/c/en/dog")
159
- rel_input = gr.Textbox(label="Relation", placeholder="/r/IsA")
160
- end_input = gr.Textbox(label="End Node", placeholder="/c/en/animal")
161
 
162
  limit_slider = gr.Slider(label="Limit", minimum=1, maximum=200, value=10, step=1)
163
  query_btn = gr.Button("Run Query", variant="primary")
 
73
  schema_md += f"| `{name}` | `{dtype}` | {notnull} | {pk} |\n"
74
  schema_md += "\n"
75
 
76
+ # --- Moved return statement outside the loop ---
77
  return schema_md
78
  # --- End of Fix ---
79
 
 
84
 
85
  def run_query(start_node, relation, end_node, limit):
86
  """
87
+ Runs a query against the ConceptNet database by joining the
88
+ edge, node, and relation tables based on the schema.
 
 
 
 
 
 
89
  """
90
  print(f"Running query: start='{start_node}', rel='{relation}', end='{end_node}'")
91
 
92
+ # --- FIX: Use correct schema from Schema Explorer ---
 
 
 
 
 
93
 
94
+ # We will build a complex JOIN query to get human-readable labels
95
+ select_clause = """
96
+ SELECT
97
+ e.id AS edge_id,
98
+ s.label AS start_label,
99
+ r.label AS relation_label,
100
+ en.label AS end_label,
101
+ e.weight,
102
+ e.dataset,
103
+ e.license,
104
+ e.surface_text
105
+ FROM
106
+ edge AS e
107
+ JOIN
108
+ relation AS r ON e.rel_id = r.id
109
+ JOIN
110
+ node AS s ON e.start_id = s.id
111
+ JOIN
112
+ node AS en ON e.end_id = en.id
113
+ """
114
+
115
+ where_clause = " WHERE 1=1"
116
  params = []
117
 
118
  try:
119
  if start_node:
120
+ where_clause += " AND s.label LIKE ?"
121
  params.append(start_node)
122
  if relation:
123
+ where_clause += " AND r.label LIKE ?"
124
  params.append(relation)
125
  if end_node:
126
+ where_clause += " AND en.label LIKE ?"
127
  params.append(end_node)
128
 
129
+ query = select_clause + where_clause + " LIMIT ?"
130
  params.append(limit)
131
 
132
  print(f"Executing SQL: {query}")
 
143
  except Exception as e:
144
  print(f"Error in run_query: {e}")
145
  traceback.print_exc()
146
+ err_msg = f"**Query Failed!**\n\n`{e}`"
147
  return pd.DataFrame(), err_msg
148
+ # --- End of Fix ---
149
 
150
  # --- 3. Build the Gradio UI ---
151
 
 
160
  schema_output = gr.Markdown("Schema will appear here...")
161
 
162
  with gr.TabItem("Query Edges"):
163
+ # --- FIX: Updated description to be accurate ---
164
  gr.Markdown(
165
  "**Run a query against the database.**\n"
166
+ "This query joins the `edge`, `node`, and `relation` tables to find connections.\n"
167
+ "You can use SQL wildcards like `%` (e.g., `/c/en/dog%` or `/r/Is%`)."
168
  )
169
+ # --- End of Fix ---
170
  with gr.Row():
171
+ start_input = gr.Textbox(label="Start Node Label", placeholder="/c/en/dog")
172
+ rel_input = gr.Textbox(label="Relation Label", placeholder="/r/IsA")
173
+ end_input = gr.Textbox(label="End Node Label", placeholder="/c/en/animal")
174
 
175
  limit_slider = gr.Slider(label="Limit", minimum=1, maximum=200, value=10, step=1)
176
  query_btn = gr.Button("Run Query", variant="primary")