Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,7 @@ FLASK_API_URL = "http://52.15.175.95:5000"
|
|
| 12 |
|
| 13 |
def query_database(question, dashboard_mode=False, chart_type=None):
|
| 14 |
if not question.strip():
|
| 15 |
-
return "", None, None, "⚠️ Please enter a question.", "", "", "", "", ""
|
| 16 |
|
| 17 |
try:
|
| 18 |
start_time = time.time()
|
|
@@ -59,7 +59,7 @@ def query_database(question, dashboard_mode=False, chart_type=None):
|
|
| 59 |
if not response.text:
|
| 60 |
error_msg = "Empty response from server"
|
| 61 |
print(f"ERROR: {error_msg}")
|
| 62 |
-
return "", None, None, f"❌ {error_msg}", f"Request time: {elapsed_time:.2f}s", "Empty response", "", "", ""
|
| 63 |
|
| 64 |
# Try to parse JSON response
|
| 65 |
try:
|
|
@@ -69,7 +69,7 @@ def query_database(question, dashboard_mode=False, chart_type=None):
|
|
| 69 |
error_msg = f"Invalid JSON response: {str(e)}"
|
| 70 |
print(f"ERROR: {error_msg}")
|
| 71 |
print(f"Response text (first 1000 chars): {response.text[:1000]}")
|
| 72 |
-
return "", None, None, f"❌ {error_msg}", f"Request time: {elapsed_time:.2f}s", "JSON parse error", "", "", ""
|
| 73 |
|
| 74 |
# Check HTTP status
|
| 75 |
if response.status_code != 200:
|
|
@@ -80,9 +80,9 @@ def query_database(question, dashboard_mode=False, chart_type=None):
|
|
| 80 |
if response.status_code == 500:
|
| 81 |
error_details = f"Server Error (500): {error_msg}\n"
|
| 82 |
error_details += f"Response: {json.dumps(result, indent=2)}"
|
| 83 |
-
return "", None, None, f"❌ Server error: {error_msg}", f"Request time: {elapsed_time:.2f}s", error_details, "", "", ""
|
| 84 |
|
| 85 |
-
return "", None, None, f"❌ Server error: {error_msg}", f"Request time: {elapsed_time:.2f}s", "HTTP error", "", "", ""
|
| 86 |
|
| 87 |
# Extract data
|
| 88 |
sql = result.get("sql", "")
|
|
@@ -107,6 +107,8 @@ def query_database(question, dashboard_mode=False, chart_type=None):
|
|
| 107 |
chart_type_result = ""
|
| 108 |
chart_error = None
|
| 109 |
chart_format = ""
|
|
|
|
|
|
|
| 110 |
|
| 111 |
if visualization:
|
| 112 |
try:
|
|
@@ -127,6 +129,8 @@ def query_database(question, dashboard_mode=False, chart_type=None):
|
|
| 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)}"
|
|
@@ -136,6 +140,8 @@ def query_database(question, dashboard_mode=False, chart_type=None):
|
|
| 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", "")
|
|
@@ -154,6 +160,8 @@ def query_database(question, dashboard_mode=False, chart_type=None):
|
|
| 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)}"
|
|
@@ -178,27 +186,27 @@ def query_database(question, dashboard_mode=False, chart_type=None):
|
|
| 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,
|
| 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,
|
| 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,
|
| 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,
|
| 202 |
|
| 203 |
def check_health():
|
| 204 |
try:
|
|
@@ -525,7 +533,7 @@ with gr.Blocks(theme=theme, title="Enterprise SQL Assistant", css="""
|
|
| 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
|
|
|
|
| 12 |
|
| 13 |
def query_database(question, dashboard_mode=False, chart_type=None):
|
| 14 |
if not question.strip():
|
| 15 |
+
return "", None, None, "⚠️ Please enter a question.", "", "", "", "", "", ""
|
| 16 |
|
| 17 |
try:
|
| 18 |
start_time = time.time()
|
|
|
|
| 59 |
if not response.text:
|
| 60 |
error_msg = "Empty response from server"
|
| 61 |
print(f"ERROR: {error_msg}")
|
| 62 |
+
return "", None, None, "⚠️ Please enter a question.", "", f"❌ {error_msg}", f"Request time: {elapsed_time:.2f}s", "Empty response", "", "", "", ""
|
| 63 |
|
| 64 |
# Try to parse JSON response
|
| 65 |
try:
|
|
|
|
| 69 |
error_msg = f"Invalid JSON response: {str(e)}"
|
| 70 |
print(f"ERROR: {error_msg}")
|
| 71 |
print(f"Response text (first 1000 chars): {response.text[:1000]}")
|
| 72 |
+
return "", None, None, "⚠️ Please enter a question.", "", f"❌ {error_msg}", f"Request time: {elapsed_time:.2f}s", "JSON parse error", "", "", "", ""
|
| 73 |
|
| 74 |
# Check HTTP status
|
| 75 |
if response.status_code != 200:
|
|
|
|
| 80 |
if response.status_code == 500:
|
| 81 |
error_details = f"Server Error (500): {error_msg}\n"
|
| 82 |
error_details += f"Response: {json.dumps(result, indent=2)}"
|
| 83 |
+
return "", None, None, "⚠️ Please enter a question.", "", f"❌ Server error: {error_msg}", f"Request time: {elapsed_time:.2f}s", error_details, "", "", "", ""
|
| 84 |
|
| 85 |
+
return "", None, None, "⚠️ Please enter a question.", "", f"❌ Server error: {error_msg}", f"Request time: {elapsed_time:.2f}s", "HTTP error", "", "", "", ""
|
| 86 |
|
| 87 |
# Extract data
|
| 88 |
sql = result.get("sql", "")
|
|
|
|
| 107 |
chart_type_result = ""
|
| 108 |
chart_error = None
|
| 109 |
chart_format = ""
|
| 110 |
+
show_image = True
|
| 111 |
+
show_html = False
|
| 112 |
|
| 113 |
if visualization:
|
| 114 |
try:
|
|
|
|
| 129 |
chart_image = Image.open(BytesIO(image_bytes))
|
| 130 |
print("Chart decoded successfully")
|
| 131 |
chart_format = "png"
|
| 132 |
+
show_image = True
|
| 133 |
+
show_html = False
|
| 134 |
except Exception as e:
|
| 135 |
print(f"Error decoding chart: {e}")
|
| 136 |
chart_error = f"Chart decoding error: {str(e)}"
|
|
|
|
| 140 |
chart_html = visualization["html"]
|
| 141 |
print("HTML chart fallback available")
|
| 142 |
chart_format = "html"
|
| 143 |
+
show_image = False
|
| 144 |
+
show_html = True
|
| 145 |
|
| 146 |
# Extract chart metadata
|
| 147 |
chart_title = visualization.get("title", "")
|
|
|
|
| 160 |
chart_image = Image.open(BytesIO(image_bytes))
|
| 161 |
print("Chart decoded successfully (fallback)")
|
| 162 |
chart_format = "png"
|
| 163 |
+
show_image = True
|
| 164 |
+
show_html = False
|
| 165 |
except Exception as e:
|
| 166 |
print(f"Error decoding chart (fallback): {e}")
|
| 167 |
chart_error = f"Chart decoding error: {str(e)}"
|
|
|
|
| 186 |
|
| 187 |
print(f"=== REQUEST COMPLETED SUCCESSFULLY ===")
|
| 188 |
|
| 189 |
+
return sql, df, chart_image, chart_html, show_image, show_html, f"✅ Query completed successfully", details, "Success", chart_type_result, chart_title, chart_format, chart_error
|
| 190 |
|
| 191 |
except requests.exceptions.ConnectionError as e:
|
| 192 |
error_msg = f"Connection failed: {str(e)}"
|
| 193 |
print(f"CONNECTION ERROR: {error_msg}")
|
| 194 |
print(f"Traceback: {traceback.format_exc()}")
|
| 195 |
+
return "", None, None, "⚠️ Please enter a question.", "", True, False, f"❌ {error_msg}", "Connection error", "Connection error", "", "", "", ""
|
| 196 |
except requests.exceptions.Timeout:
|
| 197 |
error_msg = "Request timed out after 300 seconds"
|
| 198 |
print(f"TIMEOUT ERROR: {error_msg}")
|
| 199 |
+
return "", None, None, "⚠️ Please enter a question.", "", True, False, f"⏱️ {error_msg}", "Timeout error", "Timeout error", "", "", "", ""
|
| 200 |
except requests.exceptions.RequestException as e:
|
| 201 |
error_msg = f"Request exception: {str(e)}"
|
| 202 |
print(f"REQUEST ERROR: {error_msg}")
|
| 203 |
print(f"Traceback: {traceback.format_exc()}")
|
| 204 |
+
return "", None, None, "⚠️ Please enter a question.", "", True, False, f"❌ {error_msg}", "Request error", "Request error", "", "", "", ""
|
| 205 |
except Exception as e:
|
| 206 |
error_msg = f"Unexpected error: {str(e)}"
|
| 207 |
print(f"UNEXPECTED ERROR: {error_msg}")
|
| 208 |
print(f"Traceback: {traceback.format_exc()}")
|
| 209 |
+
return "", None, None, "⚠️ Please enter a question.", "", True, False, f"🚨 {error_msg}", f"Error: {str(e)}", "Unexpected error", "", "", "", ""
|
| 210 |
|
| 211 |
def check_health():
|
| 212 |
try:
|
|
|
|
| 533 |
submit_btn.click(
|
| 534 |
fn=query_database,
|
| 535 |
inputs=[question_input, dashboard_checkbox, chart_type_dropdown],
|
| 536 |
+
outputs=[sql_output, results_output, chart_output, html_output, gr.Number(visible=False, value=1), gr.Number(visible=False, value=0), chatbot_output, request_details, error_details, chart_type_output, chart_title_output, chart_format_output, chart_error_output]
|
| 537 |
)
|
| 538 |
|
| 539 |
# Launch
|