SVashishta1 commited on
Commit
1822204
·
1 Parent(s): 9a4cc90

Feature: Add SQL query, results, and analysis to visualization responses

Browse files
Files changed (1) hide show
  1. app.py +35 -12
app.py CHANGED
@@ -271,25 +271,47 @@ def process_text_query(query, history):
271
  # Close the connection
272
  conn.close()
273
 
274
- # Generate visualization if requested
275
- if is_visualization:
276
- viz_html = generate_visualization(result_df, query)
277
- if viz_html:
278
- # Add the visualization to history
279
- history[-1][1] = viz_html
280
- return viz_html, history
281
 
282
- # If no visualization or visualization failed, generate text response
283
  data_summary = result_df.to_string()
284
- response = llm.invoke(interpret_prompt.format(
285
  question=query,
286
  sql_query=sql_query,
287
  data_summary=data_summary
288
  )).content
289
 
290
- # Add the response to history
291
- history[-1][1] = response
292
- return response, history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293
 
294
  except Exception as e:
295
  error_msg = f"Error processing query: {str(e)}"
@@ -904,6 +926,7 @@ with gr.Blocks(title="LLM Powered Database Chatbot") as demo:
904
  # Update the visualization state
905
  current_viz = viz_html
906
 
 
907
  return "", new_history, current_viz
908
 
909
  # Update the button click handlers
 
271
  # Close the connection
272
  conn.close()
273
 
274
+ # Format the dataframe as a string table for display
275
+ df_str = result_df.to_string()
 
 
 
 
 
276
 
277
+ # Generate text response
278
  data_summary = result_df.to_string()
279
+ analysis = llm.invoke(interpret_prompt.format(
280
  question=query,
281
  sql_query=sql_query,
282
  data_summary=data_summary
283
  )).content
284
 
285
+ # Create a comprehensive response that includes:
286
+ # 1. SQL Query
287
+ # 2. Results as a table
288
+ # 3. Analysis of the results
289
+ comprehensive_response = f"""
290
+ ### SQL Query:
291
+ ```sql
292
+ {sql_query}
293
+ ```
294
+
295
+ ### Results:
296
+ ```
297
+ {df_str}
298
+ ```
299
+
300
+ ### Analysis:
301
+ {analysis}
302
+ """
303
+
304
+ # Generate visualization if requested
305
+ if is_visualization:
306
+ viz_html = generate_visualization(result_df, query)
307
+ if viz_html:
308
+ # Add the visualization to history
309
+ history[-1][1] = comprehensive_response
310
+ return viz_html, history
311
+
312
+ # If no visualization or visualization failed, return text response
313
+ history[-1][1] = comprehensive_response
314
+ return comprehensive_response, history
315
 
316
  except Exception as e:
317
  error_msg = f"Error processing query: {str(e)}"
 
926
  # Update the visualization state
927
  current_viz = viz_html
928
 
929
+ # Return the updated state
930
  return "", new_history, current_viz
931
 
932
  # Update the button click handlers