Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,8 +3,6 @@ import openai
|
|
3 |
import gradio as gr
|
4 |
import duckdb
|
5 |
from functools import lru_cache
|
6 |
-
import pandas as pd
|
7 |
-
import plotly.express as px
|
8 |
import os
|
9 |
|
10 |
# =========================
|
@@ -44,24 +42,6 @@ schema = [
|
|
44 |
def get_schema():
|
45 |
return schema
|
46 |
|
47 |
-
COLUMN_TYPES = {col['column_name']: col['column_type'] for col in get_schema()}
|
48 |
-
|
49 |
-
# =========================
|
50 |
-
# Database Interaction
|
51 |
-
# =========================
|
52 |
-
|
53 |
-
def load_dataset_schema():
|
54 |
-
con = duckdb.connect()
|
55 |
-
try:
|
56 |
-
con.execute("DROP VIEW IF EXISTS contract_data")
|
57 |
-
con.execute(f"CREATE VIEW contract_data AS SELECT * FROM '{dataset_path}'")
|
58 |
-
return True
|
59 |
-
except Exception as e:
|
60 |
-
print(f"Error loading dataset schema: {e}")
|
61 |
-
return False
|
62 |
-
finally:
|
63 |
-
con.close()
|
64 |
-
|
65 |
# =========================
|
66 |
# OpenAI API Integration
|
67 |
# =========================
|
@@ -73,7 +53,7 @@ def parse_query(nl_query):
|
|
73 |
]
|
74 |
|
75 |
try:
|
76 |
-
response = openai.
|
77 |
model="gpt-4o-mini",
|
78 |
messages=messages,
|
79 |
temperature=0,
|
@@ -84,9 +64,19 @@ def parse_query(nl_query):
|
|
84 |
except Exception as e:
|
85 |
return f"Error generating SQL query: {e}"
|
86 |
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
# =========================
|
92 |
# Gradio Application UI
|
@@ -103,7 +93,7 @@ with gr.Blocks() as demo:
|
|
103 |
1. **Describe the data you want**: e.g., `Show awards over 1M in CA`
|
104 |
2. **Use Example Queries**: Click on any example query button below to execute.
|
105 |
3. **Generate SQL**: Or, enter your own query and click "Generate SQL" to see the SQL query.
|
106 |
-
4. **Execute Query**: Run the query to view results
|
107 |
5. **Dataset Schema**: See available columns and types in the "Schema" tab.
|
108 |
|
109 |
## Example Queries
|
@@ -116,156 +106,61 @@ with gr.Blocks() as demo:
|
|
116 |
placeholder='e.g., "What are the total awards over 1M in California?"',
|
117 |
lines=1
|
118 |
)
|
119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
btn_generate_sql = gr.Button("Generate SQL Query")
|
121 |
-
|
122 |
-
sql_query_out = gr.Textbox(label="Generated SQL Query", interactive=False)
|
123 |
-
# Execute button
|
124 |
btn_execute_query = gr.Button("Execute Query")
|
125 |
error_out = gr.Markdown("", visible=False)
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
|
|
|
|
130 |
|
131 |
# =========================
|
132 |
# Event Functions
|
133 |
# =========================
|
134 |
|
135 |
-
def
|
136 |
sql_query = parse_query(nl_query)
|
137 |
return sql_query
|
138 |
|
139 |
-
def
|
140 |
-
|
141 |
-
con = duckdb.connect()
|
142 |
-
con.execute(f"CREATE OR REPLACE VIEW contract_data AS SELECT * FROM '{dataset_path}'")
|
143 |
-
result_df = con.execute(sql_query).fetchdf()
|
144 |
-
con.close()
|
145 |
-
return result_df, ""
|
146 |
-
except Exception as e:
|
147 |
-
return None, f"Error executing query: {e}"
|
148 |
-
|
149 |
-
# Button click event handlers
|
150 |
-
btn_generate_sql.click(fn=generate_sql, inputs=query, outputs=sql_query_out)
|
151 |
-
btn_execute_query.click(fn=execute_sql_query, inputs=sql_query_out, outputs=[results_out, error_out])
|
152 |
-
|
153 |
-
gr.Markdown("""
|
154 |
-
# Parquet SQL Query and Plotting App
|
155 |
-
|
156 |
-
**Query and visualize data** in `sample_contract_df.parquet`
|
157 |
-
|
158 |
-
## Instructions
|
159 |
-
|
160 |
-
1. **Describe the data you want**: e.g., `Show awards over 1M in CA`
|
161 |
-
2. **Use Example Queries**: Click on any example query button below to execute.
|
162 |
-
3. **Generate SQL**: Or, enter your own query and click "Generate SQL" to see the SQL query.
|
163 |
-
4. **Execute Query**: Run the query to view results and plots.
|
164 |
-
5. **Dataset Schema**: See available columns and types in the "Schema" tab.
|
165 |
-
|
166 |
-
## Example Queries
|
167 |
-
""")
|
168 |
-
|
169 |
-
with gr.Tabs():
|
170 |
-
with gr.TabItem("Query Data"):
|
171 |
-
with gr.Row():
|
172 |
-
with gr.Column(scale=1):
|
173 |
-
query = gr.Textbox(label="Natural Language Query", placeholder='e.g., "Awards > 1M in CA"')
|
174 |
-
|
175 |
-
# Example query buttons
|
176 |
-
gr.Markdown("### Click on an example query:")
|
177 |
-
with gr.Row():
|
178 |
-
btn_example1 = gr.Button("Show awards over 1M in CA")
|
179 |
-
btn_example2 = gr.Button("List all contracts in New York")
|
180 |
-
btn_example3 = gr.Button("Show top 5 departments by award amount")
|
181 |
-
btn_example4 = gr.Button("Execute: SELECT * from contract_data LIMIT 10;")
|
182 |
-
|
183 |
-
btn_generate = gr.Button("Generate SQL")
|
184 |
-
sql_out = gr.Code(label="Generated SQL Query", language="sql")
|
185 |
-
plot_code_out = gr.Code(label="Generated Plot Code", language="python")
|
186 |
-
btn_execute = gr.Button("Execute Query")
|
187 |
-
error_out = gr.Markdown("", visible=False)
|
188 |
-
with gr.Column(scale=2):
|
189 |
-
results_out = gr.Dataframe(label="Query Results", interactive=False)
|
190 |
-
plot_out = gr.Plot(label="Plot")
|
191 |
-
|
192 |
-
with gr.TabItem("Dataset Schema"):
|
193 |
-
gr.Markdown("### Dataset Schema")
|
194 |
-
schema_display = gr.JSON(label="Schema", value=json.loads(json.dumps(get_schema(), indent=2)))
|
195 |
-
|
196 |
-
# =========================
|
197 |
-
# Click Event Handlers
|
198 |
-
# =========================
|
199 |
-
|
200 |
-
async def on_generate_click(nl_query):
|
201 |
-
"""
|
202 |
-
Handles the "Generate SQL" button click event.
|
203 |
-
"""
|
204 |
-
sql_query, plot_code = await generate_sql_and_plot_code(nl_query)
|
205 |
-
return sql_query, plot_code
|
206 |
-
|
207 |
-
def on_execute_click(sql_query, plot_code):
|
208 |
-
"""
|
209 |
-
Handles the "Execute Query" button click event.
|
210 |
-
"""
|
211 |
-
result_df, error_msg = execute_query(sql_query)
|
212 |
if error_msg:
|
213 |
-
|
214 |
-
|
215 |
-
fig, plot_error = generate_plot(plot_code, result_df)
|
216 |
-
if plot_error:
|
217 |
-
return result_df, None, plot_error
|
218 |
-
else:
|
219 |
-
return result_df, fig, ""
|
220 |
else:
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
|
|
228 |
if error_msg:
|
229 |
-
|
230 |
-
|
231 |
-
fig, plot_error = generate_plot(plot_code, result_df)
|
232 |
-
if plot_error:
|
233 |
-
error_msg = plot_error
|
234 |
-
else:
|
235 |
-
error_msg = ""
|
236 |
else:
|
237 |
-
|
238 |
-
|
239 |
-
return sql_query, plot_code, result_df, fig, error_msg
|
240 |
-
|
241 |
-
def on_example_sql_click(sql_query):
|
242 |
-
result_df, error_msg = execute_query(sql_query)
|
243 |
-
fig = None
|
244 |
-
plot_code = ""
|
245 |
-
if error_msg:
|
246 |
-
return sql_query, plot_code, None, None, error_msg
|
247 |
-
else:
|
248 |
-
return sql_query, plot_code, result_df, fig, ""
|
249 |
-
|
250 |
-
async def on_example1_click():
|
251 |
-
return await on_example_nl_click("Show awards over 1M in CA")
|
252 |
-
|
253 |
-
async def on_example2_click():
|
254 |
-
return await on_example_nl_click("List all contracts in New York")
|
255 |
-
|
256 |
-
async def on_example3_click():
|
257 |
-
return await on_example_nl_click("Show top 5 departments by award amount")
|
258 |
-
|
259 |
-
def on_example4_click():
|
260 |
-
return on_example_sql_click("SELECT * from contract_data LIMIT 10;")
|
261 |
|
262 |
-
btn_example1.click(fn=
|
263 |
-
btn_example2.click(fn=
|
264 |
-
btn_example3.click(fn=
|
265 |
-
btn_example4.click(fn=
|
266 |
|
267 |
-
|
268 |
-
|
269 |
|
270 |
# Launch the Gradio App
|
271 |
demo.launch()
|
|
|
3 |
import gradio as gr
|
4 |
import duckdb
|
5 |
from functools import lru_cache
|
|
|
|
|
6 |
import os
|
7 |
|
8 |
# =========================
|
|
|
42 |
def get_schema():
|
43 |
return schema
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
# =========================
|
46 |
# OpenAI API Integration
|
47 |
# =========================
|
|
|
53 |
]
|
54 |
|
55 |
try:
|
56 |
+
response = openai.ChatCompletion.create(
|
57 |
model="gpt-4o-mini",
|
58 |
messages=messages,
|
59 |
temperature=0,
|
|
|
64 |
except Exception as e:
|
65 |
return f"Error generating SQL query: {e}"
|
66 |
|
67 |
+
# =========================
|
68 |
+
# Database Interaction
|
69 |
+
# =========================
|
70 |
+
|
71 |
+
def execute_sql_query(sql_query):
|
72 |
+
try:
|
73 |
+
con = duckdb.connect()
|
74 |
+
con.execute(f"CREATE OR REPLACE VIEW contract_data AS SELECT * FROM '{dataset_path}'")
|
75 |
+
result_df = con.execute(sql_query).fetchdf()
|
76 |
+
con.close()
|
77 |
+
return result_df, ""
|
78 |
+
except Exception as e:
|
79 |
+
return None, f"Error executing query: {e}"
|
80 |
|
81 |
# =========================
|
82 |
# Gradio Application UI
|
|
|
93 |
1. **Describe the data you want**: e.g., `Show awards over 1M in CA`
|
94 |
2. **Use Example Queries**: Click on any example query button below to execute.
|
95 |
3. **Generate SQL**: Or, enter your own query and click "Generate SQL" to see the SQL query.
|
96 |
+
4. **Execute Query**: Run the query to view results.
|
97 |
5. **Dataset Schema**: See available columns and types in the "Schema" tab.
|
98 |
|
99 |
## Example Queries
|
|
|
106 |
placeholder='e.g., "What are the total awards over 1M in California?"',
|
107 |
lines=1
|
108 |
)
|
109 |
+
|
110 |
+
gr.Markdown("### Click on an example query:")
|
111 |
+
with gr.Row():
|
112 |
+
btn_example1 = gr.Button("Show awards over 1M in CA")
|
113 |
+
btn_example2 = gr.Button("List all contracts in New York")
|
114 |
+
btn_example3 = gr.Button("Show top 5 departments by award amount")
|
115 |
+
btn_example4 = gr.Button("Execute: SELECT * from contract_data LIMIT 10;")
|
116 |
+
|
117 |
btn_generate_sql = gr.Button("Generate SQL Query")
|
118 |
+
sql_query_out = gr.Code(label="Generated SQL Query", language="sql")
|
|
|
|
|
119 |
btn_execute_query = gr.Button("Execute Query")
|
120 |
error_out = gr.Markdown("", visible=False)
|
121 |
+
with gr.Column(scale=2):
|
122 |
+
results_out = gr.Dataframe(label="Query Results", interactive=False)
|
123 |
+
|
124 |
+
with gr.TabItem("Dataset Schema"):
|
125 |
+
gr.Markdown("### Dataset Schema")
|
126 |
+
schema_display = gr.JSON(label="Schema", value=get_schema())
|
127 |
|
128 |
# =========================
|
129 |
# Event Functions
|
130 |
# =========================
|
131 |
|
132 |
+
def on_generate_click(nl_query):
|
133 |
sql_query = parse_query(nl_query)
|
134 |
return sql_query
|
135 |
|
136 |
+
def on_execute_click(sql_query):
|
137 |
+
result_df, error_msg = execute_sql_query(sql_query)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
if error_msg:
|
139 |
+
error_out.update(f"**Error:** {error_msg}")
|
140 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
141 |
else:
|
142 |
+
error_out.update("")
|
143 |
+
return result_df
|
144 |
+
|
145 |
+
def on_example_click(query_text):
|
146 |
+
query.value = query_text
|
147 |
+
sql_query = parse_query(query_text)
|
148 |
+
sql_query_out.value = sql_query
|
149 |
+
result_df, error_msg = execute_sql_query(sql_query)
|
150 |
if error_msg:
|
151 |
+
error_out.update(f"**Error:** {error_msg}")
|
152 |
+
results_out.value = None
|
|
|
|
|
|
|
|
|
|
|
153 |
else:
|
154 |
+
error_out.update("")
|
155 |
+
results_out.value = result_df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
|
157 |
+
btn_example1.click(fn=lambda: on_example_click("Show awards over 1M in CA"), outputs=[])
|
158 |
+
btn_example2.click(fn=lambda: on_example_click("List all contracts in New York"), outputs=[])
|
159 |
+
btn_example3.click(fn=lambda: on_example_click("Show top 5 departments by award amount"), outputs=[])
|
160 |
+
btn_example4.click(fn=lambda: on_example_click("SELECT * from contract_data LIMIT 10;"), outputs=[])
|
161 |
|
162 |
+
btn_generate_sql.click(fn=on_generate_click, inputs=query, outputs=sql_query_out)
|
163 |
+
btn_execute_query.click(fn=on_execute_click, inputs=sql_query_out, outputs=results_out)
|
164 |
|
165 |
# Launch the Gradio App
|
166 |
demo.launch()
|