Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -150,5 +150,122 @@ with gr.Blocks() as demo:
|
|
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 |
# Launch the Gradio App
|
154 |
demo.launch()
|
|
|
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 |
+
return None, None, error_msg
|
214 |
+
if plot_code.strip():
|
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 |
+
return result_df, None, ""
|
222 |
+
|
223 |
+
# Functions for example query buttons
|
224 |
+
async def on_example_nl_click(query_text):
|
225 |
+
sql_query, plot_code = await generate_sql_and_plot_code(query_text)
|
226 |
+
result_df, error_msg = execute_query(sql_query)
|
227 |
+
fig = None
|
228 |
+
if error_msg:
|
229 |
+
return sql_query, plot_code, None, None, error_msg
|
230 |
+
if plot_code.strip():
|
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 |
+
fig = None
|
238 |
+
error_msg = ""
|
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=on_example1_click, inputs=[], outputs=[sql_out, plot_code_out, results_out, plot_out, error_out])
|
263 |
+
btn_example2.click(fn=on_example2_click, inputs=[], outputs=[sql_out, plot_code_out, results_out, plot_out, error_out])
|
264 |
+
btn_example3.click(fn=on_example3_click, inputs=[], outputs=[sql_out, plot_code_out, results_out, plot_out, error_out])
|
265 |
+
btn_example4.click(fn=on_example4_click, inputs=[], outputs=[sql_out, plot_code_out, results_out, plot_out, error_out])
|
266 |
+
|
267 |
+
btn_generate.click(fn=on_generate_click, inputs=query, outputs=[sql_out, plot_code_out])
|
268 |
+
btn_execute.click(fn=on_execute_click, inputs=[sql_out, plot_code_out], outputs=[results_out, plot_out, error_out])
|
269 |
+
|
270 |
# Launch the Gradio App
|
271 |
demo.launch()
|