Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -86,7 +86,7 @@ def execute_sql_query(sql_query):
|
|
86 |
|
87 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
88 |
gr.Markdown("""
|
89 |
-
<h1 style="text-align:center;"
|
90 |
<p style="text-align:center; font-size:1.2em;">Analyze US Government contract data using natural language queries.</p>
|
91 |
""")
|
92 |
|
@@ -99,31 +99,34 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
99 |
lines=2
|
100 |
)
|
101 |
|
102 |
-
btn_generate_sql = gr.Button("Generate SQL Query")
|
103 |
-
sql_query_out = gr.Code(label="Generated SQL Query", language="sql")
|
104 |
|
105 |
-
btn_execute_query = gr.Button("Execute Query")
|
106 |
-
error_out = gr.Markdown("", visible=False)
|
107 |
|
108 |
gr.Markdown("### π‘ Example Queries")
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
|
|
|
|
|
|
121 |
gr.JSON(get_schema(), label="Schema")
|
122 |
|
123 |
with gr.Column(scale=2):
|
124 |
gr.Markdown("### π Query Results")
|
125 |
-
results_out = gr.DataFrame(label="", interactive=False)
|
126 |
-
status_info = gr.Markdown("", visible=False)
|
127 |
|
128 |
# =========================
|
129 |
# Event Functions
|
@@ -148,20 +151,13 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
148 |
return result_df, ""
|
149 |
|
150 |
def handle_example_click(example_query):
|
151 |
-
query_input.value = example_query
|
152 |
sql_query, error = parse_query(example_query)
|
153 |
if error:
|
154 |
-
|
155 |
-
error_out.value = f"β {error}"
|
156 |
-
return
|
157 |
-
sql_query_out.value = sql_query
|
158 |
result_df, exec_error = execute_sql_query(sql_query)
|
159 |
if exec_error:
|
160 |
-
|
161 |
-
|
162 |
-
return
|
163 |
-
results_out.value = result_df
|
164 |
-
error_out.value = ""
|
165 |
|
166 |
# =========================
|
167 |
# Button Click Event Handlers
|
@@ -179,10 +175,28 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
179 |
outputs=[results_out, error_out]
|
180 |
)
|
181 |
|
182 |
-
|
183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
fn=lambda q=query: handle_example_click(q),
|
185 |
-
|
|
|
186 |
)
|
187 |
|
188 |
# Launch the Gradio App
|
|
|
86 |
|
87 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
88 |
gr.Markdown("""
|
89 |
+
<h1 style="text-align:center;">π Text-to-SQL Contract Data Explorer</h1>
|
90 |
<p style="text-align:center; font-size:1.2em;">Analyze US Government contract data using natural language queries.</p>
|
91 |
""")
|
92 |
|
|
|
99 |
lines=2
|
100 |
)
|
101 |
|
102 |
+
btn_generate_sql = gr.Button("π Generate SQL Query", variant="primary")
|
103 |
+
sql_query_out = gr.Code(label="π οΈ Generated SQL Query", language="sql")
|
104 |
|
105 |
+
btn_execute_query = gr.Button("π Execute Query", variant="secondary")
|
106 |
+
error_out = gr.Markdown("", visible=False, elem_id="error_message")
|
107 |
|
108 |
gr.Markdown("### π‘ Example Queries")
|
109 |
+
with gr.Column():
|
110 |
+
example_queries = [
|
111 |
+
"Show the top 10 departments by total award amount.",
|
112 |
+
"List contracts where the award amount exceeds $5,000,000.",
|
113 |
+
"Retrieve awards over $1M in California.",
|
114 |
+
"Find the top 5 awardees by number of contracts.",
|
115 |
+
"Display contracts awarded after 2020 in New York.",
|
116 |
+
"What is the total award amount by state?"
|
117 |
+
]
|
118 |
+
example_buttons = []
|
119 |
+
for i, query in enumerate(example_queries):
|
120 |
+
btn = gr.Button(query, variant="link", size="sm", interactive=True)
|
121 |
+
example_buttons.append(btn)
|
122 |
+
|
123 |
+
with gr.Accordion("π Dataset Schema", open=False):
|
124 |
gr.JSON(get_schema(), label="Schema")
|
125 |
|
126 |
with gr.Column(scale=2):
|
127 |
gr.Markdown("### π Query Results")
|
128 |
+
results_out = gr.DataFrame(label="", interactive=False, row_count=10, max_rows=100)
|
129 |
+
status_info = gr.Markdown("", visible=False, elem_id="status_info")
|
130 |
|
131 |
# =========================
|
132 |
# Event Functions
|
|
|
151 |
return result_df, ""
|
152 |
|
153 |
def handle_example_click(example_query):
|
|
|
154 |
sql_query, error = parse_query(example_query)
|
155 |
if error:
|
156 |
+
return "", f"β {error}", None, f"β {error}"
|
|
|
|
|
|
|
157 |
result_df, exec_error = execute_sql_query(sql_query)
|
158 |
if exec_error:
|
159 |
+
return sql_query, f"β {exec_error}", None, f"β {exec_error}"
|
160 |
+
return sql_query, "", result_df, ""
|
|
|
|
|
|
|
161 |
|
162 |
# =========================
|
163 |
# Button Click Event Handlers
|
|
|
175 |
outputs=[results_out, error_out]
|
176 |
)
|
177 |
|
178 |
+
# Assign click events to example buttons
|
179 |
+
for btn in example_buttons:
|
180 |
+
btn.click(
|
181 |
+
fn=handle_example_click,
|
182 |
+
inputs=None, # We'll pass the query via lambda
|
183 |
+
outputs=[sql_query_out, error_out, results_out, error_out],
|
184 |
+
_js=None,
|
185 |
+
# Use a lambda to pass the button's value as the example_query
|
186 |
+
# Note: Gradio passes the button's value as the first input if inputs are not specified
|
187 |
+
# To ensure the correct query is passed, we use a lambda with no inputs
|
188 |
+
# and retrieve the button's value within the function
|
189 |
+
# However, Gradio does not support passing the button's value directly here
|
190 |
+
# So, we need to create a partial function or use lambda with default argument
|
191 |
+
# Here, we use a lambda with a default argument
|
192 |
+
)
|
193 |
+
|
194 |
+
# To properly assign each button's click event with its specific query, redefine the loop:
|
195 |
+
for btn, query in zip(example_buttons, example_queries):
|
196 |
+
btn.click(
|
197 |
fn=lambda q=query: handle_example_click(q),
|
198 |
+
inputs=None,
|
199 |
+
outputs=[sql_query_out, error_out, results_out, error_out]
|
200 |
)
|
201 |
|
202 |
# Launch the Gradio App
|