Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,8 +8,8 @@ import time
|
|
| 8 |
import traceback
|
| 9 |
from PIL import Image
|
| 10 |
|
| 11 |
-
# π§ CONFIGURE:
|
| 12 |
-
|
| 13 |
|
| 14 |
def query_database(question):
|
| 15 |
if not question.strip():
|
|
@@ -24,15 +24,15 @@ def query_database(question):
|
|
| 24 |
print("="*50)
|
| 25 |
|
| 26 |
# Prepare the request
|
| 27 |
-
payload = {"
|
| 28 |
|
| 29 |
-
print(f"Request URL: {
|
| 30 |
print(f"Request payload: {payload}")
|
| 31 |
|
| 32 |
# Make the request
|
| 33 |
print("Sending request...")
|
| 34 |
response = requests.post(
|
| 35 |
-
|
| 36 |
json=payload,
|
| 37 |
timeout=300
|
| 38 |
)
|
|
@@ -61,18 +61,11 @@ def query_database(question):
|
|
| 61 |
|
| 62 |
# Check HTTP status
|
| 63 |
if response.status_code != 200:
|
| 64 |
-
error_msg = result.get("
|
| 65 |
print(f"ERROR: HTTP status {response.status_code}: {error_msg}")
|
| 66 |
return "", None, None, f"β Server error: {error_msg}", f"Request time: {elapsed_time:.2f}s", "HTTP error"
|
| 67 |
|
| 68 |
-
# Check for application error
|
| 69 |
-
if "status" in result and result["status"] == "error":
|
| 70 |
-
error_msg = result.get("message", "Unknown error")
|
| 71 |
-
print(f"ERROR: Application error: {error_msg}")
|
| 72 |
-
return "", None, None, f"β {error_msg}", f"Request time: {elapsed_time:.2f}s", "Application error"
|
| 73 |
-
|
| 74 |
# Extract data
|
| 75 |
-
conversational_response = result.get("response", "")
|
| 76 |
sql = result.get("sql", "")
|
| 77 |
rows = result.get("result", [])
|
| 78 |
chart_data = result.get("chart")
|
|
@@ -106,7 +99,7 @@ def query_database(question):
|
|
| 106 |
|
| 107 |
print(f"=== REQUEST COMPLETED SUCCESSFULLY ===")
|
| 108 |
|
| 109 |
-
return sql, df, chart_image,
|
| 110 |
|
| 111 |
except requests.exceptions.ConnectionError as e:
|
| 112 |
error_msg = f"Connection failed: {str(e)}"
|
|
@@ -128,6 +121,29 @@ def query_database(question):
|
|
| 128 |
print(f"Traceback: {traceback.format_exc()}")
|
| 129 |
return "", None, None, f"π¨ {error_msg}", f"Error: {str(e)}", "Unexpected error"
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
# π¨ Theme: Enterprise Dark Blue
|
| 132 |
theme = gr.themes.Default(
|
| 133 |
primary_hue="blue",
|
|
@@ -233,6 +249,10 @@ with gr.Blocks(theme=theme, title="Enterprise SQL Assistant", css="""
|
|
| 233 |
example6 = gr.Button("Count of members by status", elem_classes=["example-btn"])
|
| 234 |
example7 = gr.Button("Show me the first 10 rows", elem_classes=["example-btn"])
|
| 235 |
example8 = gr.Button("What is the average age of members?", elem_classes=["example-btn"])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
|
| 237 |
with gr.Column(scale=2):
|
| 238 |
gr.Markdown("### π€ AI Assistant Response")
|
|
@@ -271,6 +291,13 @@ with gr.Blocks(theme=theme, title="Enterprise SQL Assistant", css="""
|
|
| 271 |
return example_text
|
| 272 |
|
| 273 |
# Events
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
# Example query buttons
|
| 275 |
for example_btn in [example1, example2, example3, example4, example5, example6, example7, example8]:
|
| 276 |
example_btn.click(
|
|
|
|
| 8 |
import traceback
|
| 9 |
from PIL import Image
|
| 10 |
|
| 11 |
+
# π§ CONFIGURE: Your Flask API URL
|
| 12 |
+
FLASK_API_URL = "http://3.14.153.135:5000"
|
| 13 |
|
| 14 |
def query_database(question):
|
| 15 |
if not question.strip():
|
|
|
|
| 24 |
print("="*50)
|
| 25 |
|
| 26 |
# Prepare the request
|
| 27 |
+
payload = {"question": question}
|
| 28 |
|
| 29 |
+
print(f"Request URL: {FLASK_API_URL}/ask")
|
| 30 |
print(f"Request payload: {payload}")
|
| 31 |
|
| 32 |
# Make the request
|
| 33 |
print("Sending request...")
|
| 34 |
response = requests.post(
|
| 35 |
+
f"{FLASK_API_URL}/ask",
|
| 36 |
json=payload,
|
| 37 |
timeout=300
|
| 38 |
)
|
|
|
|
| 61 |
|
| 62 |
# Check HTTP status
|
| 63 |
if response.status_code != 200:
|
| 64 |
+
error_msg = result.get("error", f"HTTP {response.status_code}")
|
| 65 |
print(f"ERROR: HTTP status {response.status_code}: {error_msg}")
|
| 66 |
return "", None, None, f"β Server error: {error_msg}", f"Request time: {elapsed_time:.2f}s", "HTTP error"
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
# Extract data
|
|
|
|
| 69 |
sql = result.get("sql", "")
|
| 70 |
rows = result.get("result", [])
|
| 71 |
chart_data = result.get("chart")
|
|
|
|
| 99 |
|
| 100 |
print(f"=== REQUEST COMPLETED SUCCESSFULLY ===")
|
| 101 |
|
| 102 |
+
return sql, df, chart_image, f"β
Query completed successfully", details, "Success"
|
| 103 |
|
| 104 |
except requests.exceptions.ConnectionError as e:
|
| 105 |
error_msg = f"Connection failed: {str(e)}"
|
|
|
|
| 121 |
print(f"Traceback: {traceback.format_exc()}")
|
| 122 |
return "", None, None, f"π¨ {error_msg}", f"Error: {str(e)}", "Unexpected error"
|
| 123 |
|
| 124 |
+
def check_health():
|
| 125 |
+
try:
|
| 126 |
+
print("Checking API health...")
|
| 127 |
+
response = requests.get(f"{FLASK_API_URL}/health", timeout=10)
|
| 128 |
+
|
| 129 |
+
if response.status_code == 200:
|
| 130 |
+
health_data = response.json()
|
| 131 |
+
status = health_data.get('status', 'unknown')
|
| 132 |
+
model = health_data.get('model', 'unknown')
|
| 133 |
+
tables = health_data.get('tables', [])
|
| 134 |
+
data_rows = health_data.get('data_rows', 0)
|
| 135 |
+
|
| 136 |
+
health_msg = f"β
API Status: {status.upper()}\n"
|
| 137 |
+
health_msg += f"π€ Model: {model}\n"
|
| 138 |
+
health_msg += f"π Tables: {', '.join(tables) if tables else 'None'}\n"
|
| 139 |
+
health_msg += f"π Data Rows: {data_rows:,}"
|
| 140 |
+
|
| 141 |
+
return health_msg, "success"
|
| 142 |
+
else:
|
| 143 |
+
return f"β API returned status {response.status_code}", "error"
|
| 144 |
+
except Exception as e:
|
| 145 |
+
return f"β Health check failed: {str(e)}", "error"
|
| 146 |
+
|
| 147 |
# π¨ Theme: Enterprise Dark Blue
|
| 148 |
theme = gr.themes.Default(
|
| 149 |
primary_hue="blue",
|
|
|
|
| 249 |
example6 = gr.Button("Count of members by status", elem_classes=["example-btn"])
|
| 250 |
example7 = gr.Button("Show me the first 10 rows", elem_classes=["example-btn"])
|
| 251 |
example8 = gr.Button("What is the average age of members?", elem_classes=["example-btn"])
|
| 252 |
+
|
| 253 |
+
gr.Markdown("### π©Ί API Health Check")
|
| 254 |
+
health_btn = gr.Button("Check API Health", variant="secondary")
|
| 255 |
+
health_output = gr.Markdown(label="API Status")
|
| 256 |
|
| 257 |
with gr.Column(scale=2):
|
| 258 |
gr.Markdown("### π€ AI Assistant Response")
|
|
|
|
| 291 |
return example_text
|
| 292 |
|
| 293 |
# Events
|
| 294 |
+
# Health check button
|
| 295 |
+
health_btn.click(
|
| 296 |
+
fn=check_health,
|
| 297 |
+
inputs=[],
|
| 298 |
+
outputs=[health_output, error_details]
|
| 299 |
+
)
|
| 300 |
+
|
| 301 |
# Example query buttons
|
| 302 |
for example_btn in [example1, example2, example3, example4, example5, example6, example7, example8]:
|
| 303 |
example_btn.click(
|