Update app.py
Browse files
app.py
CHANGED
@@ -67,7 +67,7 @@ def process_sql_file(file_path):
|
|
67 |
if statement.strip():
|
68 |
conn.execute(text(statement))
|
69 |
|
70 |
-
return True, "SQL file successfully executed!
|
71 |
|
72 |
except Exception as e:
|
73 |
return False, f"Error processing SQL file: {str(e)}"
|
@@ -91,7 +91,7 @@ def process_csv_file(file_path):
|
|
91 |
records = df.to_dict('records')
|
92 |
insert_rows_into_table(records, table)
|
93 |
|
94 |
-
return True, "CSV file successfully loaded!
|
95 |
|
96 |
except Exception as e:
|
97 |
return False, f"Error processing CSV file: {str(e)}"
|
@@ -180,9 +180,6 @@ def query_sql(user_query: str) -> str:
|
|
180 |
return result
|
181 |
|
182 |
def update_schema():
|
183 |
-
"""
|
184 |
-
Updates the schema display.
|
185 |
-
"""
|
186 |
schema = get_table_schema()
|
187 |
if schema:
|
188 |
return f"### Current Schema:\n```\n{schema}\n```"
|
@@ -190,7 +187,6 @@ def update_schema():
|
|
190 |
|
191 |
# Create the Gradio interface
|
192 |
with gr.Blocks() as demo:
|
193 |
-
# Upload Interface Components
|
194 |
with gr.Group() as upload_group:
|
195 |
gr.Markdown("""
|
196 |
# Data Query Interface
|
@@ -217,120 +213,83 @@ with gr.Blocks() as demo:
|
|
217 |
file_types=[".csv", ".sql"],
|
218 |
type="filepath"
|
219 |
)
|
220 |
-
|
221 |
-
continue_btn = gr.Button("Continue", visible=False)
|
222 |
|
223 |
-
# Query Interface Components
|
224 |
with gr.Group(visible=False) as query_group:
|
225 |
-
gr.Markdown("## Data Query Interface")
|
226 |
-
|
227 |
-
# Data Display Section
|
228 |
-
gr.Markdown("### Current Data")
|
229 |
-
data_table = gr.Dataframe(
|
230 |
-
value=get_data_table(),
|
231 |
-
label="Data Table",
|
232 |
-
interactive=False
|
233 |
-
)
|
234 |
-
|
235 |
-
schema_display = gr.Markdown(value="Loading schema...")
|
236 |
-
|
237 |
-
# Query Section
|
238 |
with gr.Row():
|
239 |
-
with gr.Column():
|
240 |
-
user_input = gr.Textbox(
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
|
|
|
|
246 |
interactive=False
|
247 |
)
|
248 |
|
249 |
-
|
250 |
-
|
251 |
-
refresh_schema_btn = gr.Button("Refresh Schema")
|
252 |
-
back_btn = gr.Button("Upload New File")
|
253 |
|
254 |
-
def handle_upload(
|
255 |
-
|
|
|
|
|
|
|
256 |
if success:
|
257 |
df = get_data_table()
|
258 |
schema = get_table_schema()
|
259 |
-
return
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
269 |
|
270 |
-
def
|
271 |
df = get_data_table()
|
272 |
schema = get_table_schema()
|
273 |
-
return {
|
274 |
-
upload_group: gr.update(visible=False),
|
275 |
-
query_group: gr.update(visible=True),
|
276 |
-
data_table: df,
|
277 |
-
schema_display: f"### Current Schema:\n```\n{schema}\n```" if schema else "No schema available"
|
278 |
-
}
|
279 |
-
|
280 |
-
def switch_to_upload():
|
281 |
-
return {
|
282 |
-
upload_group: gr.update(visible=True),
|
283 |
-
query_group: gr.update(visible=False),
|
284 |
-
continue_btn: gr.update(visible=False),
|
285 |
-
upload_status: gr.update(value="")
|
286 |
-
}
|
287 |
|
288 |
# Event handlers
|
289 |
file_input.upload(
|
290 |
fn=handle_upload,
|
|
|
291 |
outputs=[
|
292 |
-
|
293 |
-
continue_btn,
|
294 |
data_table,
|
295 |
-
schema_display
|
296 |
-
|
297 |
-
)
|
298 |
-
|
299 |
-
continue_btn.click(
|
300 |
-
fn=switch_to_query,
|
301 |
-
outputs=[
|
302 |
upload_group,
|
303 |
-
query_group
|
304 |
-
data_table,
|
305 |
-
schema_display
|
306 |
-
]
|
307 |
-
)
|
308 |
-
|
309 |
-
back_btn.click(
|
310 |
-
fn=switch_to_upload,
|
311 |
-
outputs=[
|
312 |
-
upload_group,
|
313 |
-
query_group,
|
314 |
-
continue_btn,
|
315 |
-
upload_status
|
316 |
]
|
317 |
)
|
318 |
|
319 |
user_input.change(
|
320 |
fn=query_sql,
|
321 |
-
inputs=
|
322 |
-
outputs=
|
323 |
-
)
|
324 |
-
|
325 |
-
refresh_table_btn.click(
|
326 |
-
fn=get_data_table,
|
327 |
-
outputs=[data_table]
|
328 |
)
|
329 |
|
330 |
-
|
331 |
-
fn=
|
332 |
-
outputs=[schema_display]
|
333 |
)
|
334 |
|
335 |
if __name__ == "__main__":
|
336 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
67 |
if statement.strip():
|
68 |
conn.execute(text(statement))
|
69 |
|
70 |
+
return True, "SQL file successfully executed!"
|
71 |
|
72 |
except Exception as e:
|
73 |
return False, f"Error processing SQL file: {str(e)}"
|
|
|
91 |
records = df.to_dict('records')
|
92 |
insert_rows_into_table(records, table)
|
93 |
|
94 |
+
return True, "CSV file successfully loaded!"
|
95 |
|
96 |
except Exception as e:
|
97 |
return False, f"Error processing CSV file: {str(e)}"
|
|
|
180 |
return result
|
181 |
|
182 |
def update_schema():
|
|
|
|
|
|
|
183 |
schema = get_table_schema()
|
184 |
if schema:
|
185 |
return f"### Current Schema:\n```\n{schema}\n```"
|
|
|
187 |
|
188 |
# Create the Gradio interface
|
189 |
with gr.Blocks() as demo:
|
|
|
190 |
with gr.Group() as upload_group:
|
191 |
gr.Markdown("""
|
192 |
# Data Query Interface
|
|
|
213 |
file_types=[".csv", ".sql"],
|
214 |
type="filepath"
|
215 |
)
|
216 |
+
status = gr.Textbox(label="Status", interactive=False)
|
|
|
217 |
|
|
|
218 |
with gr.Group(visible=False) as query_group:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
with gr.Row():
|
220 |
+
with gr.Column(scale=1):
|
221 |
+
user_input = gr.Textbox(label="Ask a question about the data")
|
222 |
+
query_output = gr.Textbox(label="Result")
|
223 |
+
|
224 |
+
with gr.Column(scale=2):
|
225 |
+
gr.Markdown("### Current Data")
|
226 |
+
data_table = gr.Dataframe(
|
227 |
+
value=None,
|
228 |
+
label="Data Table",
|
229 |
interactive=False
|
230 |
)
|
231 |
|
232 |
+
schema_display = gr.Markdown(value="Loading schema...")
|
233 |
+
refresh_btn = gr.Button("Refresh Data")
|
|
|
|
|
234 |
|
235 |
+
def handle_upload(file_obj):
|
236 |
+
if file_obj is None:
|
237 |
+
return "Please upload a file.", None, None, "No schema available", gr.update(visible=True), gr.update(visible=False)
|
238 |
+
|
239 |
+
success, message = process_uploaded_file(file_obj)
|
240 |
if success:
|
241 |
df = get_data_table()
|
242 |
schema = get_table_schema()
|
243 |
+
return (
|
244 |
+
message,
|
245 |
+
df,
|
246 |
+
schema,
|
247 |
+
f"### Current Schema:\n```\n{schema}\n```" if schema else "No schema available",
|
248 |
+
gr.update(visible=False),
|
249 |
+
gr.update(visible=True)
|
250 |
+
)
|
251 |
+
return (
|
252 |
+
message,
|
253 |
+
None,
|
254 |
+
None,
|
255 |
+
"No schema available",
|
256 |
+
gr.update(visible=True),
|
257 |
+
gr.update(visible=False)
|
258 |
+
)
|
259 |
|
260 |
+
def refresh_data():
|
261 |
df = get_data_table()
|
262 |
schema = get_table_schema()
|
263 |
+
return df, f"### Current Schema:\n```\n{schema}\n```" if schema else "No schema available"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
264 |
|
265 |
# Event handlers
|
266 |
file_input.upload(
|
267 |
fn=handle_upload,
|
268 |
+
inputs=file_input,
|
269 |
outputs=[
|
270 |
+
status,
|
|
|
271 |
data_table,
|
272 |
+
schema_display,
|
273 |
+
schema_display,
|
|
|
|
|
|
|
|
|
|
|
274 |
upload_group,
|
275 |
+
query_group
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
276 |
]
|
277 |
)
|
278 |
|
279 |
user_input.change(
|
280 |
fn=query_sql,
|
281 |
+
inputs=user_input,
|
282 |
+
outputs=query_output
|
|
|
|
|
|
|
|
|
|
|
283 |
)
|
284 |
|
285 |
+
refresh_btn.click(
|
286 |
+
fn=refresh_data,
|
287 |
+
outputs=[data_table, schema_display]
|
288 |
)
|
289 |
|
290 |
if __name__ == "__main__":
|
291 |
+
demo.launch(
|
292 |
+
server_name="0.0.0.0",
|
293 |
+
server_port=7860,
|
294 |
+
ssr=False
|
295 |
+
)
|