Shreyass334 commited on
Commit
42ed51c
Β·
verified Β·
1 Parent(s): 98bf4ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -7
app.py CHANGED
@@ -7,7 +7,6 @@ import json
7
  import time
8
  import traceback
9
  from PIL import Image
10
-
11
  # πŸ”§ CONFIGURE: Your Flask API URL (Public URL)
12
  FLASK_API_URL = "http://52.15.175.95:5000"
13
 
@@ -95,6 +94,7 @@ def query_database(question, dashboard_mode=False, chart_type=None):
95
  print(f"SQL: {sql[:100] if sql else 'None'}...")
96
  print(f"Rows count: {row_count}")
97
  print(f"Visualization received: {'Yes' if visualization else 'No'}")
 
98
 
99
  # Create DataFrame
100
  df = pd.DataFrame(rows) if rows else pd.DataFrame()
@@ -102,9 +102,11 @@ def query_database(question, dashboard_mode=False, chart_type=None):
102
 
103
  # Process visualization
104
  chart_image = None
 
105
  chart_title = ""
106
  chart_type_result = ""
107
  chart_error = None
 
108
 
109
  if visualization:
110
  try:
@@ -124,15 +126,24 @@ def query_database(question, dashboard_mode=False, chart_type=None):
124
  image_bytes = base64.b64decode(chart_image_b64)
125
  chart_image = Image.open(BytesIO(image_bytes))
126
  print("Chart decoded successfully")
 
127
  except Exception as e:
128
  print(f"Error decoding chart: {e}")
129
  chart_error = f"Chart decoding error: {str(e)}"
130
 
 
 
 
 
 
 
131
  # Extract chart metadata
132
  chart_title = visualization.get("title", "")
133
  chart_type_result = visualization.get("type", "")
 
134
  print(f"Chart type: {chart_type_result}")
135
  print(f"Chart title: {chart_title}")
 
136
  elif isinstance(visualization, str):
137
  # Fallback for backward compatibility
138
  try:
@@ -142,6 +153,7 @@ def query_database(question, dashboard_mode=False, chart_type=None):
142
  image_bytes = base64.b64decode(visualization)
143
  chart_image = Image.open(BytesIO(image_bytes))
144
  print("Chart decoded successfully (fallback)")
 
145
  except Exception as e:
146
  print(f"Error decoding chart (fallback): {e}")
147
  chart_error = f"Chart decoding error: {str(e)}"
@@ -158,33 +170,35 @@ def query_database(question, dashboard_mode=False, chart_type=None):
158
  details += f"Chart type: {chart_type_result}\n"
159
  if chart_title:
160
  details += f"Chart title: {chart_title}\n"
 
 
161
  if dashboard_mode:
162
  details += f"Dashboard mode: Enabled\n"
163
  details += f"Response size: {len(response.text)} bytes"
164
 
165
  print(f"=== REQUEST COMPLETED SUCCESSFULLY ===")
166
 
167
- return sql, df, chart_image, f"βœ… Query completed successfully", details, "Success", chart_type_result, chart_title, chart_error
168
 
169
  except requests.exceptions.ConnectionError as e:
170
  error_msg = f"Connection failed: {str(e)}"
171
  print(f"CONNECTION ERROR: {error_msg}")
172
  print(f"Traceback: {traceback.format_exc()}")
173
- return "", None, None, f"❌ {error_msg}", "Connection error", "Connection error", "", "", ""
174
  except requests.exceptions.Timeout:
175
  error_msg = "Request timed out after 300 seconds"
176
  print(f"TIMEOUT ERROR: {error_msg}")
177
- return "", None, None, f"⏱️ {error_msg}", "Timeout error", "Timeout error", "", "", ""
178
  except requests.exceptions.RequestException as e:
179
  error_msg = f"Request exception: {str(e)}"
180
  print(f"REQUEST ERROR: {error_msg}")
181
  print(f"Traceback: {traceback.format_exc()}")
182
- return "", None, None, f"❌ {error_msg}", "Request error", "Request error", "", "", ""
183
  except Exception as e:
184
  error_msg = f"Unexpected error: {str(e)}"
185
  print(f"UNEXPECTED ERROR: {error_msg}")
186
  print(f"Traceback: {traceback.format_exc()}")
187
- return "", None, None, f"🚨 {error_msg}", f"Error: {str(e)}", "Unexpected error", "", "", ""
188
 
189
  def check_health():
190
  try:
@@ -462,10 +476,16 @@ with gr.Blocks(theme=theme, title="Enterprise SQL Assistant", css="""
462
  height=400,
463
  elem_classes="chart-container"
464
  )
 
 
 
 
 
465
  with gr.Column(scale=1):
466
  gr.Markdown("### πŸ“Š Chart Information")
467
  chart_type_output = gr.Markdown(label="Chart Type", elem_classes="chart-metadata")
468
  chart_title_output = gr.Markdown(label="Chart Title", elem_classes="chart-metadata")
 
469
  chart_error_output = gr.Markdown(label="Chart Error", elem_classes="chart-metadata")
470
 
471
  gr.Markdown("### πŸ“Š Request Details")
@@ -505,7 +525,7 @@ with gr.Blocks(theme=theme, title="Enterprise SQL Assistant", css="""
505
  submit_btn.click(
506
  fn=query_database,
507
  inputs=[question_input, dashboard_checkbox, chart_type_dropdown],
508
- outputs=[sql_output, results_output, chart_output, chatbot_output, request_details, error_details, chart_type_output, chart_title_output, chart_error_output]
509
  )
510
 
511
  # Launch
 
7
  import time
8
  import traceback
9
  from PIL import Image
 
10
  # πŸ”§ CONFIGURE: Your Flask API URL (Public URL)
11
  FLASK_API_URL = "http://52.15.175.95:5000"
12
 
 
94
  print(f"SQL: {sql[:100] if sql else 'None'}...")
95
  print(f"Rows count: {row_count}")
96
  print(f"Visualization received: {'Yes' if visualization else 'No'}")
97
+ print(f"Visualization details: {visualization}")
98
 
99
  # Create DataFrame
100
  df = pd.DataFrame(rows) if rows else pd.DataFrame()
 
102
 
103
  # Process visualization
104
  chart_image = None
105
+ chart_html = None
106
  chart_title = ""
107
  chart_type_result = ""
108
  chart_error = None
109
+ chart_format = ""
110
 
111
  if visualization:
112
  try:
 
126
  image_bytes = base64.b64decode(chart_image_b64)
127
  chart_image = Image.open(BytesIO(image_bytes))
128
  print("Chart decoded successfully")
129
+ chart_format = "png"
130
  except Exception as e:
131
  print(f"Error decoding chart: {e}")
132
  chart_error = f"Chart decoding error: {str(e)}"
133
 
134
+ # If no image, check for HTML fallback
135
+ if not chart_image and "html" in visualization:
136
+ chart_html = visualization["html"]
137
+ print("HTML chart fallback available")
138
+ chart_format = "html"
139
+
140
  # Extract chart metadata
141
  chart_title = visualization.get("title", "")
142
  chart_type_result = visualization.get("type", "")
143
+ chart_format = visualization.get("format", chart_format)
144
  print(f"Chart type: {chart_type_result}")
145
  print(f"Chart title: {chart_title}")
146
+ print(f"Chart format: {chart_format}")
147
  elif isinstance(visualization, str):
148
  # Fallback for backward compatibility
149
  try:
 
153
  image_bytes = base64.b64decode(visualization)
154
  chart_image = Image.open(BytesIO(image_bytes))
155
  print("Chart decoded successfully (fallback)")
156
+ chart_format = "png"
157
  except Exception as e:
158
  print(f"Error decoding chart (fallback): {e}")
159
  chart_error = f"Chart decoding error: {str(e)}"
 
170
  details += f"Chart type: {chart_type_result}\n"
171
  if chart_title:
172
  details += f"Chart title: {chart_title}\n"
173
+ if chart_format:
174
+ details += f"Chart format: {chart_format}\n"
175
  if dashboard_mode:
176
  details += f"Dashboard mode: Enabled\n"
177
  details += f"Response size: {len(response.text)} bytes"
178
 
179
  print(f"=== REQUEST COMPLETED SUCCESSFULLY ===")
180
 
181
+ return sql, df, chart_image, chart_html, f"βœ… Query completed successfully", details, "Success", chart_type_result, chart_title, chart_error
182
 
183
  except requests.exceptions.ConnectionError as e:
184
  error_msg = f"Connection failed: {str(e)}"
185
  print(f"CONNECTION ERROR: {error_msg}")
186
  print(f"Traceback: {traceback.format_exc()}")
187
+ return "", None, None, None, f"❌ {error_msg}", "Connection error", "Connection error", "", "", ""
188
  except requests.exceptions.Timeout:
189
  error_msg = "Request timed out after 300 seconds"
190
  print(f"TIMEOUT ERROR: {error_msg}")
191
+ return "", None, None, None, f"⏱️ {error_msg}", "Timeout error", "Timeout error", "", "", ""
192
  except requests.exceptions.RequestException as e:
193
  error_msg = f"Request exception: {str(e)}"
194
  print(f"REQUEST ERROR: {error_msg}")
195
  print(f"Traceback: {traceback.format_exc()}")
196
+ return "", None, None, None, f"❌ {error_msg}", "Request error", "Request error", "", "", ""
197
  except Exception as e:
198
  error_msg = f"Unexpected error: {str(e)}"
199
  print(f"UNEXPECTED ERROR: {error_msg}")
200
  print(f"Traceback: {traceback.format_exc()}")
201
+ return "", None, None, None, f"🚨 {error_msg}", f"Error: {str(e)}", "Unexpected error", "", "", ""
202
 
203
  def check_health():
204
  try:
 
476
  height=400,
477
  elem_classes="chart-container"
478
  )
479
+ # Add HTML component for fallback
480
+ html_output = gr.HTML(
481
+ label="Interactive Chart",
482
+ visible=False
483
+ )
484
  with gr.Column(scale=1):
485
  gr.Markdown("### πŸ“Š Chart Information")
486
  chart_type_output = gr.Markdown(label="Chart Type", elem_classes="chart-metadata")
487
  chart_title_output = gr.Markdown(label="Chart Title", elem_classes="chart-metadata")
488
+ chart_format_output = gr.Markdown(label="Chart Format", elem_classes="chart-metadata")
489
  chart_error_output = gr.Markdown(label="Chart Error", elem_classes="chart-metadata")
490
 
491
  gr.Markdown("### πŸ“Š Request Details")
 
525
  submit_btn.click(
526
  fn=query_database,
527
  inputs=[question_input, dashboard_checkbox, chart_type_dropdown],
528
+ outputs=[sql_output, results_output, chart_output, html_output, chatbot_output, request_details, error_details, chart_type_output, chart_title_output, chart_format_output, chart_error_output]
529
  )
530
 
531
  # Launch