Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -84,124 +84,46 @@ def execute_sql_query(sql_query):
|
|
84 |
# Gradio Application UI
|
85 |
# =========================
|
86 |
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
font-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
.header {
|
95 |
-
background-color: #ff5a5f;
|
96 |
-
color: white;
|
97 |
-
padding: 20px;
|
98 |
-
text-align: center;
|
99 |
-
border-radius: 8px;
|
100 |
-
}
|
101 |
-
|
102 |
-
.button-primary {
|
103 |
-
background-color: #ff5a5f;
|
104 |
-
color: white;
|
105 |
-
}
|
106 |
-
|
107 |
-
.button-secondary {
|
108 |
-
background-color: #007A87;
|
109 |
-
color: white;
|
110 |
-
}
|
111 |
-
|
112 |
-
.footer {
|
113 |
-
text-align: center;
|
114 |
-
padding: 10px;
|
115 |
-
color: #555;
|
116 |
-
}
|
117 |
-
"""
|
118 |
-
|
119 |
-
with gr.Blocks(css=custom_css, theme=gr.themes.Default()) as demo:
|
120 |
-
# Header Section
|
121 |
-
with gr.Row():
|
122 |
-
gr.HTML(f"""
|
123 |
-
<div class="header">
|
124 |
-
<h1>US Government Contract Data Analyzer</h1>
|
125 |
-
<p>Convert natural language queries into SQL and explore contract data effortlessly.</p>
|
126 |
-
</div>
|
127 |
-
""")
|
128 |
-
|
129 |
-
# Main Content
|
130 |
with gr.Row():
|
131 |
-
with gr.Column(scale=1, min_width=
|
132 |
-
|
133 |
-
gr.Markdown("""
|
134 |
-
## How to Use
|
135 |
-
|
136 |
-
1. **Describe your query**: Enter a natural language description of the data you need.
|
137 |
-
2. **Select an example**: Choose from predefined example queries for inspiration.
|
138 |
-
3. **Generate SQL**: Click the button to convert your description into an SQL query.
|
139 |
-
4. **Execute**: Run the SQL query to view the results.
|
140 |
-
|
141 |
-
### Example Queries
|
142 |
-
""")
|
143 |
-
|
144 |
-
# Example Queries Dropdown
|
145 |
-
example_queries = [
|
146 |
-
"Show awards over 1M in CA",
|
147 |
-
"Retrieve the top 15 records from contract_data where basetype is Award Notice, awardee has at least 12 characters, and popcity has more than 5 characters. Exclude the fields sub_tier, popzip, awardnumber, basetype, popstate, active, popcountry, type, countrycode, and popstreetaddress",
|
148 |
-
"Show top 10 departments by award amount",
|
149 |
-
"SELECT department_ind_agency, CONCAT('$', ROUND(SUM(award), 0)) AS sum_award FROM contract_data GROUP BY department_ind_agency ORDER BY SUM(award) DESC LIMIT 25",
|
150 |
-
"SELECT awardnumber, awarddate, award, office, department_ind_agency, awardee FROM contract_data WHERE awardee IS NOT NULL AND award IS NOT NULL AND popcity IS NOT NULL AND award > 100000000 LIMIT 10;",
|
151 |
-
"SELECT awardee, CONCAT('$', ROUND(SUM(award), 0)) AS sum_award FROM contract_data GROUP BY awardee ORDER BY SUM(award) DESC LIMIT 25"
|
152 |
-
]
|
153 |
-
|
154 |
-
example_dropdown = gr.Dropdown(
|
155 |
-
label="Select an Example Query",
|
156 |
-
choices=example_queries,
|
157 |
-
value=example_queries[0],
|
158 |
-
interactive=True
|
159 |
-
)
|
160 |
-
|
161 |
-
# Copy Button for Example Queries
|
162 |
-
with gr.Row():
|
163 |
-
btn_load_example = gr.Button("Load Example", variant="primary")
|
164 |
-
btn_copy_example = gr.Button("Copy Query", variant="secondary")
|
165 |
-
|
166 |
-
# User Input
|
167 |
query_input = gr.Textbox(
|
168 |
-
label="
|
169 |
-
placeholder='e.g., "What are the total awards over 1M in California?"',
|
170 |
lines=2
|
171 |
)
|
172 |
|
173 |
-
|
174 |
-
|
175 |
-
btn_generate_sql = gr.Button("Generate SQL Query", variant="primary")
|
176 |
-
btn_execute_query = gr.Button("Execute Query", variant="secondary")
|
177 |
-
|
178 |
-
# SQL Query Output with Copy Button
|
179 |
-
with gr.Row():
|
180 |
-
sql_query_out = gr.Code(label="Generated SQL Query", language="sql", interactive=False)
|
181 |
-
btn_copy_sql = gr.Button("Copy SQL", variant="secondary")
|
182 |
|
183 |
-
|
184 |
-
error_out = gr.Markdown("", visible=False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
|
186 |
-
|
187 |
-
|
188 |
-
btn_copy_results = gr.Button("Copy Results", variant="secondary")
|
189 |
|
190 |
with gr.Column(scale=2):
|
191 |
-
|
192 |
-
results_out = gr.
|
193 |
-
|
194 |
-
# Dataset Schema Tab
|
195 |
-
with gr.Tab("Dataset Schema"):
|
196 |
-
with gr.Accordion("View Schema Details", open=True):
|
197 |
-
schema_display = gr.JSON(label="Schema", value=get_schema(), interactive=False)
|
198 |
-
|
199 |
-
# Footer
|
200 |
-
gr.HTML(f"""
|
201 |
-
<div class="footer">
|
202 |
-
<p>© 2024 Your Company Name. All rights reserved.</p>
|
203 |
-
</div>
|
204 |
-
""")
|
205 |
|
206 |
# =========================
|
207 |
# Event Functions
|
@@ -209,97 +131,59 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Default()) as demo:
|
|
209 |
|
210 |
def generate_sql(nl_query):
|
211 |
if not nl_query.strip():
|
212 |
-
return "", "Please enter a
|
213 |
sql_query, error = parse_query(nl_query)
|
214 |
if error:
|
215 |
-
return "", error
|
216 |
-
return sql_query, ""
|
217 |
|
218 |
def execute_query(sql_query):
|
219 |
if not sql_query.strip():
|
220 |
-
return None, "
|
221 |
result_df, error = execute_sql_query(sql_query)
|
222 |
if error:
|
223 |
-
return None, error
|
|
|
|
|
224 |
return result_df, ""
|
225 |
|
226 |
-
def
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
return
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
def copy_text(text):
|
243 |
-
return text # Gradio handles the copy functionality internally
|
244 |
|
245 |
# =========================
|
246 |
# Button Click Event Handlers
|
247 |
# =========================
|
248 |
|
249 |
-
# Load Example Query
|
250 |
-
btn_load_example.click(
|
251 |
-
fn=load_example,
|
252 |
-
inputs=example_dropdown,
|
253 |
-
outputs=[sql_query_out, error_out, results_out],
|
254 |
-
show_progress=True
|
255 |
-
)
|
256 |
-
|
257 |
-
# Copy Example Query to Input
|
258 |
-
btn_copy_example.click(
|
259 |
-
fn=lambda x: x,
|
260 |
-
inputs=example_dropdown,
|
261 |
-
outputs=query_input
|
262 |
-
)
|
263 |
-
|
264 |
-
# Generate SQL Query
|
265 |
btn_generate_sql.click(
|
266 |
fn=generate_sql,
|
267 |
inputs=query_input,
|
268 |
-
outputs=[sql_query_out, error_out
|
269 |
-
show_progress=True
|
270 |
)
|
271 |
|
272 |
-
# Execute SQL Query
|
273 |
btn_execute_query.click(
|
274 |
fn=execute_query,
|
275 |
inputs=sql_query_out,
|
276 |
-
outputs=[results_out, error_out]
|
277 |
-
show_progress=True
|
278 |
-
)
|
279 |
-
|
280 |
-
# Copy SQL Query
|
281 |
-
btn_copy_sql.click(
|
282 |
-
fn=copy_text,
|
283 |
-
inputs=sql_query_out,
|
284 |
-
outputs=None,
|
285 |
-
_js="(text) => { navigator.clipboard.writeText(text); }"
|
286 |
)
|
287 |
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
_js="""
|
294 |
-
(csv) => {
|
295 |
-
navigator.clipboard.writeText(csv).then(function() {
|
296 |
-
alert('Results copied to clipboard!');
|
297 |
-
}, function(err) {
|
298 |
-
alert('Could not copy results: ', err);
|
299 |
-
});
|
300 |
-
}
|
301 |
-
"""
|
302 |
-
)
|
303 |
|
304 |
-
# Launch the Gradio App
|
305 |
-
demo.launch(
|
|
|
84 |
# Gradio Application UI
|
85 |
# =========================
|
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 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
with gr.Row():
|
94 |
+
with gr.Column(scale=1, min_width=350):
|
95 |
+
gr.Markdown("### 🔍 Enter Your Query")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
query_input = gr.Textbox(
|
97 |
+
label="",
|
98 |
+
placeholder='e.g., "What are the total awards over $1M in California?"',
|
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 |
+
example_queries = [
|
110 |
+
"Show the top 10 departments by total award amount.",
|
111 |
+
"List contracts where the award amount exceeds $5,000,000.",
|
112 |
+
"Retrieve awards over $1M in California.",
|
113 |
+
"Find the top 5 awardees by number of contracts.",
|
114 |
+
"Display contracts awarded after 2020 in New York.",
|
115 |
+
"What is the total award amount by state?"
|
116 |
+
]
|
117 |
+
for i, query in enumerate(example_queries):
|
118 |
+
gr.Button(query, elem_id=f"example_{i}")
|
119 |
|
120 |
+
with gr.Accordion("Dataset Schema", open=False):
|
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
|
|
|
131 |
|
132 |
def generate_sql(nl_query):
|
133 |
if not nl_query.strip():
|
134 |
+
return "", "⚠️ Please enter a natural language query."
|
135 |
sql_query, error = parse_query(nl_query)
|
136 |
if error:
|
137 |
+
return "", f"❌ {error}"
|
138 |
+
return sql_query, ""
|
139 |
|
140 |
def execute_query(sql_query):
|
141 |
if not sql_query.strip():
|
142 |
+
return None, "⚠️ Please generate an SQL query first."
|
143 |
result_df, error = execute_sql_query(sql_query)
|
144 |
if error:
|
145 |
+
return None, f"❌ {error}"
|
146 |
+
if result_df.empty:
|
147 |
+
return None, "ℹ️ The query returned no results."
|
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 |
+
sql_query_out.value = ""
|
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 |
+
results_out.value = None
|
161 |
+
error_out.value = f"❌ {exec_error}"
|
162 |
+
return
|
163 |
+
results_out.value = result_df
|
164 |
+
error_out.value = ""
|
|
|
|
|
|
|
165 |
|
166 |
# =========================
|
167 |
# Button Click Event Handlers
|
168 |
# =========================
|
169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
btn_generate_sql.click(
|
171 |
fn=generate_sql,
|
172 |
inputs=query_input,
|
173 |
+
outputs=[sql_query_out, error_out]
|
|
|
174 |
)
|
175 |
|
|
|
176 |
btn_execute_query.click(
|
177 |
fn=execute_query,
|
178 |
inputs=sql_query_out,
|
179 |
+
outputs=[results_out, error_out]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
)
|
181 |
|
182 |
+
for i, query in enumerate(example_queries):
|
183 |
+
gr.get_component(f"example_{i}").click(
|
184 |
+
fn=lambda q=query: handle_example_click(q),
|
185 |
+
outputs=[]
|
186 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
187 |
|
188 |
+
# Launch the Gradio App
|
189 |
+
demo.queue().launch()
|