LeonceNsh commited on
Commit
8760634
·
verified ·
1 Parent(s): f146007

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -22
app.py CHANGED
@@ -13,51 +13,95 @@ def load_dataset_schema():
13
  con.close()
14
  return schema.to_dict(orient="records")
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  # Generate SQL based on schema and user query
17
  def generate_sql_query(features, query):
18
- # This simple example constructs an SQL condition directly, adapt for complex queries if needed
19
- sql_query = f"SELECT * FROM contract_data WHERE {query}"
20
  return sql_query
21
 
22
  # Execute the SQL query and display results
23
  def execute_query(sql_query):
24
- con = duckdb.connect()
25
- con.execute(f"CREATE VIEW contract_data AS SELECT * FROM '{dataset_path}'")
26
- result_df = con.execute(sql_query).fetchdf()
27
- con.close()
28
- return result_df.to_markdown() # Display result as markdown
 
 
 
 
 
 
 
 
29
 
30
  # Gradio app UI
31
  with gr.Blocks() as demo:
32
  gr.Markdown("""
33
  # Local Parquet SQL Query App
 
34
  Query and explore data in `sample_contract_df.parquet` using DuckDB and SQL queries.
 
 
 
 
 
 
 
35
  """)
36
-
37
- # Load dataset schema and convert it to JSON for display
38
- schema = load_dataset_schema()
39
- schema_json = json.dumps(schema, indent=2)
40
- features = gr.Code(label="Dataset Schema", value=schema_json, language="json")
41
 
42
- # User input components
43
- query = gr.Textbox(label="Natural Language Query", placeholder="Enter a condition, e.g., 'amount > 1000'")
44
- sql_out = gr.Code(label="Generated SQL Query", language="sql")
45
- results_out = gr.Markdown(label="Query Results")
 
 
 
 
 
 
 
 
 
 
46
 
47
- # Buttons to generate and execute SQL
48
- btn_generate = gr.Button("Generate SQL")
49
- btn_execute = gr.Button("Execute Query")
50
 
51
- # Set up click events with correct inputs and outputs
52
  btn_generate.click(
53
  fn=generate_sql_query,
54
- inputs=[features, query], # Both are Gradio components now
55
  outputs=sql_out,
56
  )
57
  btn_execute.click(
58
  fn=execute_query,
59
  inputs=sql_out,
60
- outputs=results_out,
61
  )
62
 
63
  # Launch the app
 
13
  con.close()
14
  return schema.to_dict(orient="records")
15
 
16
+ # Simple parser to convert natural language to SQL syntax
17
+ def parse_query(nl_query):
18
+ # Replace common phrases with SQL syntax
19
+ replacements = {
20
+ 'equal to': '=',
21
+ 'equals to': '=',
22
+ 'equals': '=',
23
+ 'equal': '=',
24
+ 'not equal to': '!=',
25
+ 'not equal': '!=',
26
+ 'greater than or equal to': '>=',
27
+ 'less than or equal to': '<=',
28
+ 'greater than': '>',
29
+ 'less than': '<',
30
+ 'between': 'BETWEEN',
31
+ ' and ': ' AND ',
32
+ ' or ': ' OR ',
33
+ }
34
+ sql_query = nl_query.lower()
35
+ for k, v in replacements.items():
36
+ sql_query = sql_query.replace(k, v)
37
+ return sql_query
38
+
39
  # Generate SQL based on schema and user query
40
  def generate_sql_query(features, query):
41
+ condition = parse_query(query)
42
+ sql_query = f"SELECT * FROM contract_data WHERE {condition}"
43
  return sql_query
44
 
45
  # Execute the SQL query and display results
46
  def execute_query(sql_query):
47
+ try:
48
+ con = duckdb.connect()
49
+ con.execute(f"CREATE VIEW contract_data AS SELECT * FROM '{dataset_path}'")
50
+ result_df = con.execute(sql_query).fetchdf()
51
+ con.close()
52
+ return result_df, ""
53
+ except Exception as e:
54
+ con.close()
55
+ return None, f"Error executing query: {e}"
56
+
57
+ # Load dataset schema and convert it to JSON for display
58
+ schema = load_dataset_schema()
59
+ schema_json = json.dumps(schema, indent=2)
60
 
61
  # Gradio app UI
62
  with gr.Blocks() as demo:
63
  gr.Markdown("""
64
  # Local Parquet SQL Query App
65
+
66
  Query and explore data in `sample_contract_df.parquet` using DuckDB and SQL queries.
67
+
68
+ ## Instructions
69
+
70
+ - Enter a natural language query describing the data you want to retrieve.
71
+ - Click "Generate SQL" to see the SQL query that will be executed.
72
+ - Click "Execute Query" to run the query and see the results.
73
+ - You can view the dataset schema in the "Dataset Schema" tab.
74
  """)
 
 
 
 
 
75
 
76
+ with gr.Tabs():
77
+ with gr.TabItem("Query Data"):
78
+ with gr.Row():
79
+ with gr.Column(scale=1):
80
+ query = gr.Textbox(
81
+ label="Natural Language Query",
82
+ placeholder="e.g., 'amount greater than 1000 and status equal to \"approved\"'"
83
+ )
84
+ btn_generate = gr.Button("Generate SQL")
85
+ sql_out = gr.Code(label="Generated SQL Query", language="sql")
86
+ btn_execute = gr.Button("Execute Query")
87
+ error_out = gr.Markdown("", visible=False)
88
+ with gr.Column(scale=2):
89
+ results_out = gr.Dataframe(label="Query Results")
90
 
91
+ with gr.TabItem("Dataset Schema"):
92
+ gr.Markdown("### Dataset Schema")
93
+ features = gr.Code(label="Dataset Schema", value=schema_json, language="json")
94
 
95
+ # Set up click events
96
  btn_generate.click(
97
  fn=generate_sql_query,
98
+ inputs=[features, query],
99
  outputs=sql_out,
100
  )
101
  btn_execute.click(
102
  fn=execute_query,
103
  inputs=sql_out,
104
+ outputs=[results_out, error_out],
105
  )
106
 
107
  # Launch the app