Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,13 +25,13 @@ def query_database(question, dashboard_mode=False, chart_type=None):
|
|
| 25 |
print(f"Chart type: {chart_type}")
|
| 26 |
print("="*50)
|
| 27 |
|
| 28 |
-
# Prepare the request
|
| 29 |
payload = {
|
| 30 |
"question": question,
|
| 31 |
"visualize": True # Always request visualization
|
| 32 |
}
|
| 33 |
|
| 34 |
-
# Add chart_type if specified
|
| 35 |
if chart_type and chart_type != "auto":
|
| 36 |
payload["viz_type"] = chart_type
|
| 37 |
|
|
@@ -89,7 +89,7 @@ def query_database(question, dashboard_mode=False, chart_type=None):
|
|
| 89 |
df = pd.DataFrame(rows) if rows else pd.DataFrame()
|
| 90 |
print(f"DataFrame shape: {df.shape}")
|
| 91 |
|
| 92 |
-
# Process visualization
|
| 93 |
chart_image = None
|
| 94 |
chart_title = ""
|
| 95 |
chart_type_result = ""
|
|
@@ -106,6 +106,9 @@ def query_database(question, dashboard_mode=False, chart_type=None):
|
|
| 106 |
# Extract the base64 image string from the structured visualization data
|
| 107 |
chart_image_b64 = visualization.get("image")
|
| 108 |
if chart_image_b64:
|
|
|
|
|
|
|
|
|
|
| 109 |
image_bytes = base64.b64decode(chart_image_b64)
|
| 110 |
chart_image = Image.open(BytesIO(image_bytes))
|
| 111 |
print("Chart decoded successfully")
|
|
@@ -118,6 +121,9 @@ def query_database(question, dashboard_mode=False, chart_type=None):
|
|
| 118 |
elif isinstance(visualization, str):
|
| 119 |
# Fallback for backward compatibility
|
| 120 |
try:
|
|
|
|
|
|
|
|
|
|
| 121 |
image_bytes = base64.b64decode(visualization)
|
| 122 |
chart_image = Image.open(BytesIO(image_bytes))
|
| 123 |
print("Chart decoded successfully (fallback)")
|
|
@@ -172,7 +178,7 @@ def check_health():
|
|
| 172 |
if response.status_code == 200:
|
| 173 |
health_data = response.json()
|
| 174 |
status = health_data.get('status', 'unknown')
|
| 175 |
-
tables = health_data.get('tables', [])
|
| 176 |
|
| 177 |
health_msg = f"✅ API Status: {status.upper()}\n"
|
| 178 |
health_msg += f"📊 Tables: {', '.join(tables) if tables else 'None'}\n"
|
|
@@ -186,6 +192,7 @@ def check_health():
|
|
| 186 |
def get_schema():
|
| 187 |
try:
|
| 188 |
print("Fetching database schema...")
|
|
|
|
| 189 |
response = requests.get(f"{FLASK_API_URL}/tables", timeout=10)
|
| 190 |
|
| 191 |
if response.status_code == 200:
|
|
@@ -323,7 +330,8 @@ with gr.Blocks(theme=theme, title="Enterprise SQL Assistant", css="""
|
|
| 323 |
chart_type_dropdown = gr.Dropdown(
|
| 324 |
label="Chart Type (Optional)",
|
| 325 |
choices=[
|
| 326 |
-
"auto", "bar", "line", "scatter", "pie", "histogram"
|
|
|
|
| 327 |
],
|
| 328 |
value="auto",
|
| 329 |
info="Force a specific chart type"
|
|
|
|
| 25 |
print(f"Chart type: {chart_type}")
|
| 26 |
print("="*50)
|
| 27 |
|
| 28 |
+
# Prepare the request - FIXED: Use correct parameter names
|
| 29 |
payload = {
|
| 30 |
"question": question,
|
| 31 |
"visualize": True # Always request visualization
|
| 32 |
}
|
| 33 |
|
| 34 |
+
# Add chart_type if specified - FIXED: Use correct parameter name
|
| 35 |
if chart_type and chart_type != "auto":
|
| 36 |
payload["viz_type"] = chart_type
|
| 37 |
|
|
|
|
| 89 |
df = pd.DataFrame(rows) if rows else pd.DataFrame()
|
| 90 |
print(f"DataFrame shape: {df.shape}")
|
| 91 |
|
| 92 |
+
# Process visualization - FIXED: Handle base64 prefix
|
| 93 |
chart_image = None
|
| 94 |
chart_title = ""
|
| 95 |
chart_type_result = ""
|
|
|
|
| 106 |
# Extract the base64 image string from the structured visualization data
|
| 107 |
chart_image_b64 = visualization.get("image")
|
| 108 |
if chart_image_b64:
|
| 109 |
+
# FIXED: Remove base64 prefix if present
|
| 110 |
+
if chart_image_b64.startswith("data:image/"):
|
| 111 |
+
chart_image_b64 = chart_image_b64.split(",")[1]
|
| 112 |
image_bytes = base64.b64decode(chart_image_b64)
|
| 113 |
chart_image = Image.open(BytesIO(image_bytes))
|
| 114 |
print("Chart decoded successfully")
|
|
|
|
| 121 |
elif isinstance(visualization, str):
|
| 122 |
# Fallback for backward compatibility
|
| 123 |
try:
|
| 124 |
+
# FIXED: Remove base64 prefix if present
|
| 125 |
+
if visualization.startswith("data:image/"):
|
| 126 |
+
visualization = visualization.split(",")[1]
|
| 127 |
image_bytes = base64.b64decode(visualization)
|
| 128 |
chart_image = Image.open(BytesIO(image_bytes))
|
| 129 |
print("Chart decoded successfully (fallback)")
|
|
|
|
| 178 |
if response.status_code == 200:
|
| 179 |
health_data = response.json()
|
| 180 |
status = health_data.get('status', 'unknown')
|
| 181 |
+
tables = health_data.get('tables', []) # FIXED: This is now correct
|
| 182 |
|
| 183 |
health_msg = f"✅ API Status: {status.upper()}\n"
|
| 184 |
health_msg += f"📊 Tables: {', '.join(tables) if tables else 'None'}\n"
|
|
|
|
| 192 |
def get_schema():
|
| 193 |
try:
|
| 194 |
print("Fetching database schema...")
|
| 195 |
+
# FIXED: Use correct endpoint
|
| 196 |
response = requests.get(f"{FLASK_API_URL}/tables", timeout=10)
|
| 197 |
|
| 198 |
if response.status_code == 200:
|
|
|
|
| 330 |
chart_type_dropdown = gr.Dropdown(
|
| 331 |
label="Chart Type (Optional)",
|
| 332 |
choices=[
|
| 333 |
+
"auto", "bar", "line", "scatter", "pie", "histogram",
|
| 334 |
+
"time_series", "correlation" # FIXED: Added missing chart types
|
| 335 |
],
|
| 336 |
value="auto",
|
| 337 |
info="Force a specific chart type"
|