Nlp-to-SQL / app.py
DecisionMinds's picture
Update app.py
71770bc verified
Raw History Blame Contribute Delete
21.4 kB
import gradio as gr
import requests
import pandas as pd
import base64
from io import BytesIO
import json
import time
import traceback
import urllib.parse
# πŸ”§ CONFIGURE: Your Flask API URL (Updated to use your IP and port)
FLASK_API_URL = "http://18.222.27.210:8000"
def query_database(question, chart_type=None):
if not question.strip():
return "", None, "⚠️ Please enter a question.", "", "", "", "", "", "", ""
try:
start_time = time.time()
print("\n" + "="*50)
print("=== NEW QUERY REQUEST ===")
print(f"Time: {time.strftime('%Y-%m-%d %H:%M:%S')}")
print(f"Question: {question}")
print(f"Chart type: {chart_type}")
print("="*50)
# Prepare the request - updated to match Flask API expectations
payload = {
"query": question
}
# Add chart_type if specified (if supported by backend)
if chart_type and chart_type != "auto":
payload["chart_type"] = chart_type
print(f"Request URL: {FLASK_API_URL}/query")
print(f"Request payload: {payload}")
# Make the request
print("Sending request...")
response = requests.post(
f"{FLASK_API_URL}/query",
json=payload,
timeout=600 # Increased timeout to 10 minutes for big queries
)
elapsed_time = time.time() - start_time
print(f"\n=== RESPONSE RECEIVED ===")
print(f"Response time: {elapsed_time:.2f} seconds")
print(f"Response status: {response.status_code}")
print(f"Response headers: {dict(response.headers)}")
# Check if response is empty
if not response.text:
error_msg = "Empty response from server"
print(f"ERROR: {error_msg}")
return "", None, "⚠️ Please enter a question.", "", "", "", "", "", "", ""
# Try to parse JSON response
try:
result = response.json()
print(f"Parsed JSON successfully: {type(result)}")
except json.JSONDecodeError as e:
error_msg = f"Invalid JSON response: {str(e)}"
print(f"ERROR: {error_msg}")
print(f"Response text (first 1000 chars): {response.text[:1000]}")
return "", None, "⚠️ Please enter a question.", "", "", "", "", "", "", ""
# Check HTTP status
if response.status_code != 200:
error_msg = result.get("error", f"HTTP {response.status_code}")
print(f"ERROR: HTTP status {response.status_code}: {error_msg}")
# For 500 errors, show more details
if response.status_code == 500:
error_details = f"Server Error (500): {error_msg}\n"
error_details += f"Response: {json.dumps(result, indent=2)}"
return "", None, "⚠️ Please enter a question.", "", "", "", error_details, "", "", ""
return "", None, "⚠️ Please enter a question.", "", "", "", f"HTTP error: {error_msg}", "", "", ""
# Check if the request was successful
# Note: Flask API doesn't necessarily return a "success" field
# We'll consider it successful if we have results or SQL
if not (result.get("results") or result.get("sql")):
error_msg = result.get("error", "Unknown error")
print(f"ERROR: API returned unsuccessful response: {error_msg}")
return "", None, "⚠️ Please enter a question.", "", "", "", f"API error: {error_msg}", "", "", ""
# Extract data - Updated to match Flask API response structure
sql = result.get("sql", "")
results = result.get("results", [])
chart_url = result.get("chart", "")
explanation = result.get("explanation", "")
metadata = result.get("metadata", {})
# Extract row count from metadata
row_count = metadata.get("row_count", 0)
print(f"\n=== EXTRACTED DATA ===")
print(f"SQL: {sql[:100] if sql else 'None'}...")
print(f"Row count: {row_count}")
print(f"Chart URL: {chart_url}")
print(f"Explanation: {explanation[:100] if explanation else 'None'}...")
# Create DataFrame
df = pd.DataFrame(results) if results else pd.DataFrame()
print(f"DataFrame shape: {df.shape}")
# Process visualization - only HTML charts
chart_html = None
chart_title = ""
chart_type_result = ""
chart_error = None
chart_format = ""
chart_link = ""
# Process chart if URL is provided
if chart_url:
try:
# Check if it's a full URL or a relative path
if chart_url.startswith("http"):
full_chart_url = chart_url
else:
# It's a relative path, construct full URL
full_chart_url = f"{FLASK_API_URL}{chart_url}"
print(f"Full chart URL: {full_chart_url}")
chart_link = full_chart_url
# Create iframe for HTML chart
chart_html = f"""
<div style="width: 100%; height: 500px; overflow: hidden;">
<iframe src="{full_chart_url}" style="width: 100%; height: 100%; border: none;"></iframe>
</div>
"""
chart_format = "html"
print("Chart will be displayed as HTML iframe")
# Set chart metadata
chart_type_result = metadata.get("chart_type", "unknown")
chart_title = f"Visualization for: {question[:50]}{'...' if len(question) > 50 else ''}"
except Exception as e:
print(f"Error processing chart: {e}")
traceback.print_exc()
chart_error = f"Chart processing error: {str(e)}"
else:
print("No chart URL provided")
chart_error = "No chart available"
# Prepare details
details = f"Request time: {elapsed_time:.2f}s\n"
details += f"Status code: {response.status_code}\n"
details += f"Rows returned: {row_count}\n"
details += f"Columns: {', '.join(metadata.get('columns', []))}\n"
details += f"Database type: {metadata.get('database_type', 'unknown')}\n"
if chart_url:
details += f"Chart URL: {chart_url}\n"
if chart_type_result:
details += f"Chart type: {chart_type_result}\n"
if chart_title:
details += f"Chart title: {chart_title}\n"
if chart_format:
details += f"Chart format: {chart_format}\n"
details += f"Response size: {len(response.text)} bytes"
print(f"=== REQUEST COMPLETED SUCCESSFULLY ===")
return sql, df, chart_html, explanation, details, "Success", chart_type_result, chart_title, chart_format, chart_error, chart_link
except requests.exceptions.ConnectionError as e:
error_msg = f"Connection failed: {str(e)}"
print(f"CONNECTION ERROR: {error_msg}")
print(f"Traceback: {traceback.format_exc()}")
return "", None, "⚠️ Please enter a question.", "", f"❌ {error_msg}", "Connection error", "Connection error", "", "", "", ""
except requests.exceptions.Timeout:
error_msg = "Request timed out after 600 seconds"
print(f"TIMEOUT ERROR: {error_msg}")
return "", None, "⚠️ Please enter a question.", "", f"⏱️ {error_msg}", "Timeout error", "Timeout error", "", "", "", ""
except requests.exceptions.RequestException as e:
error_msg = f"Request exception: {str(e)}"
print(f"REQUEST ERROR: {error_msg}")
print(f"Traceback: {traceback.format_exc()}")
return "", None, "⚠️ Please enter a question.", "", f"❌ {error_msg}", "Request error", "Request error", "", "", "", ""
except Exception as e:
error_msg = f"Unexpected error: {str(e)}"
print(f"UNEXPECTED ERROR: {error_msg}")
print(f"Traceback: {traceback.format_exc()}")
return "", None, "⚠️ Please enter a question.", "", f"🚨 {error_msg}", f"Error: {str(e)}", "Unexpected error", "", "", "", ""
def check_health():
try:
print("Checking API health...")
print(f"API URL: {FLASK_API_URL}/health")
response = requests.get(f"{FLASK_API_URL}/health", timeout=10)
print(f"Health check response status: {response.status_code}")
print(f"Health check response headers: {dict(response.headers)}")
print(f"Health check response text: {response.text}")
if response.status_code == 200:
try:
health_data = response.json()
print(f"Parsed health data: {health_data}")
status = health_data.get('status', 'unknown')
db_type = health_data.get('database_type', 'unknown')
query_engine_initialized = health_data.get('query_engine_initialized', False)
health_msg = f"βœ… API Status: {status.upper()}\n"
health_msg += f"πŸ—„οΈ Database Type: {db_type}\n"
health_msg += f"πŸ€– Query Engine: {'Initialized' if query_engine_initialized else 'Not Initialized'}"
return health_msg, "success"
except json.JSONDecodeError as e:
error_msg = f"Failed to parse health check response: {str(e)}"
print(f"JSON PARSE ERROR: {error_msg}")
return f"❌ {error_msg}", "error"
else:
return f"❌ API returned status {response.status_code}\nResponse: {response.text}", "error"
except requests.exceptions.ConnectionError as e:
error_msg = f"Connection to API failed: {str(e)}"
print(f"CONNECTION ERROR: {error_msg}")
print(f"Traceback: {traceback.format_exc()}")
return f"❌ {error_msg}", "error"
except requests.exceptions.Timeout:
error_msg = "Health check request timed out"
print(f"TIMEOUT ERROR: {error_msg}")
return f"❌ {error_msg}", "error"
except Exception as e:
error_msg = f"Health check failed: {str(e)}"
print(f"HEALTH CHECK ERROR: {error_msg}")
print(f"Traceback: {traceback.format_exc()}")
return f"❌ {error_msg}", "error"
def get_schema():
try:
print("Fetching database schema...")
print(f"API URL: {FLASK_API_URL}/schema")
response = requests.get(f"{FLASK_API_URL}/schema", timeout=10)
print(f"Schema response status: {response.status_code}")
print(f"Schema response text: {response.text}")
if response.status_code == 200:
try:
schema_data = response.json()
print(f"Parsed schema data: {type(schema_data)}")
schema_text = "## Database Schema\n\n"
if isinstance(schema_data, dict):
for table_name, columns in schema_data.items():
schema_text += f"### {table_name}\n"
schema_text += "| Column | Type |\n|--------|------|\n"
for col in columns:
schema_text += f"| {col.get('name', 'Unknown')} | {col.get('type', 'Unknown')} |\n"
schema_text += "\n"
else:
schema_text += "Schema data is not in expected format."
return schema_text, "success"
except json.JSONDecodeError as e:
error_msg = f"Failed to parse schema response: {str(e)}"
print(f"JSON PARSE ERROR: {error_msg}")
return f"❌ {error_msg}", "error"
else:
return f"❌ API returned status {response.status_code}\nResponse: {response.text}", "error"
except requests.exceptions.ConnectionError as e:
error_msg = f"Connection to API failed: {str(e)}"
print(f"CONNECTION ERROR: {error_msg}")
print(f"Traceback: {traceback.format_exc()}")
return f"❌ {error_msg}", "error"
except requests.exceptions.Timeout:
error_msg = "Schema request timed out"
print(f"TIMEOUT ERROR: {error_msg}")
return f"❌ {error_msg}", "error"
except Exception as e:
error_msg = f"Failed to fetch schema: {str(e)}"
print(f"SCHEMA FETCH ERROR: {error_msg}")
print(f"Traceback: {traceback.format_exc()}")
return f"❌ {error_msg}", "error"
# 🎨 Theme: Enterprise Dark Blue
theme = gr.themes.Default(
primary_hue="blue",
secondary_hue="gray",
neutral_hue="slate",
font=["Inter", "sans-serif"]
).set(
body_background_fill="*neutral_950",
background_fill_secondary="*neutral_900",
button_primary_background_fill="*primary_600",
button_primary_background_fill_hover="*primary_700",
button_secondary_background_fill="*neutral_800",
button_secondary_background_fill_hover="*neutral_700",
block_title_text_color="*primary_400",
block_label_text_color="*neutral_300",
input_background_fill="*neutral_800",
input_border_color="*neutral_700"
)
# πŸš€ UI Layout
with gr.Blocks(theme=theme, title="Enterprise SQL Assistant", css="""
.example-btn {
background-color: #1e40af;
color: white;
border-radius: 8px;
padding: 8px 12px;
margin: 4px;
font-size: 0.9em;
border: none;
cursor: pointer;
transition: all 0.2s;
}
.example-btn:hover {
background-color: #1d4ed8;
transform: translateY(-1px);
}
.chatbot-container {
border: 1px solid #334155;
border-radius: 8px;
padding: 15px;
background-color: #1e293b;
min-height: 200px;
max-height: 400px;
overflow-y: auto;
}
.status-success {
color: #10b981;
font-weight: bold;
}
.status-error {
color: #ef4444;
font-weight: bold;
}
.status-warning {
color: #f59e0b;
font-weight: bold;
}
.chart-container {
border: 1px solid #334155;
border-radius: 8px;
padding: 10px;
background-color: #1e293b;
height: 500px;
}
.chart-metadata {
background-color: #1e293b;
border: 1px solid #334155;
border-radius: 8px;
padding: 10px;
margin-bottom: 10px;
}
.schema-container {
background-color: #1e293b;
border: 1px solid #334155;
border-radius: 8px;
padding: 15px;
max-height: 400px;
overflow-y: auto;
}
/* Fix input text color */
.gradio-container input[type="text"],
.gradio-container textarea {
color: #f3f4f6 !important;
}
/* Fix placeholder color */
.gradio-container input::placeholder,
.gradio-container textarea::placeholder {
color: #9ca3af !important;
}
""") as demo:
gr.HTML("""
<div style="text-align: center; padding: 20px;">
<h1 style="color: #3B82F6; margin-bottom: 5px;">πŸ“Š Enterprise SQL Assistant</h1>
<p style="color: #9CA3AF; font-size: 1.1em; margin-top: 0;">
Ask questions about your data. Get SQL, results, and insights.
</p>
</div>
""")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### πŸ” Ask a Question")
question_input = gr.Textbox(
placeholder="E.g., Show me the first 5 products",
label="Natural Language Query",
lines=3
)
with gr.Row():
chart_type_dropdown = gr.Dropdown(
label="Chart Type (Optional)",
choices=[
"auto", "bar", "line", "scatter", "pie", "histogram",
"time_series", "correlation"
],
value="auto",
info="Force a specific chart type"
)
submit_btn = gr.Button("πŸš€ Generate SQL & Results", variant="primary", size="lg")
gr.Markdown("### πŸ’‘ Example Queries")
with gr.Row():
with gr.Column():
example1 = gr.Button("Show me the first 5 products", elem_classes=["example-btn"])
example2 = gr.Button("List all customers from the Northeast region", elem_classes=["example-btn"])
example3 = gr.Button("Show all completed orders", elem_classes=["example-btn"])
example4 = gr.Button("What are the total sales by region?", elem_classes=["example-btn"])
with gr.Column():
example5 = gr.Button("How many products are in each category?", elem_classes=["example-btn"])
example6 = gr.Button("Show order history for customer John Smith", elem_classes=["example-btn"])
example7 = gr.Button("What is the total revenue from electronics category?", elem_classes=["example-btn"])
example8 = gr.Button("Which products have the highest stock quantity?", elem_classes=["example-btn"])
with gr.Accordion("Advanced Options", open=False):
gr.Markdown("### 🩺 System Information")
with gr.Row():
health_btn = gr.Button("Check API Health", variant="secondary", size="sm")
schema_btn = gr.Button("Get Database Schema", variant="secondary", size="sm")
health_output = gr.Markdown(label="API Status")
schema_output = gr.Markdown(label="Database Schema", elem_classes="schema-container")
with gr.Column(scale=2):
gr.Markdown("### πŸ€– AI Assistant Response")
chatbot_output = gr.Markdown(
label="AI Response",
elem_classes=["chatbot-container"]
)
with gr.Tabs():
with gr.Tab("SQL Query"):
sql_output = gr.Code(label="", language="sql")
with gr.Tab("Data Results"):
results_output = gr.Dataframe(
label="Query Results",
interactive=False,
wrap=True
)
with gr.Tab("Visual Insights"):
chart_output = gr.HTML(
label="Chart",
elem_classes="chart-container"
)
with gr.Row():
with gr.Column():
gr.Markdown("### πŸ“Š Chart Information")
chart_type_output = gr.Markdown(label="Chart Type", elem_classes="chart-metadata")
chart_title_output = gr.Markdown(label="Chart Title", elem_classes="chart-metadata")
chart_format_output = gr.Markdown(label="Chart Format", elem_classes="chart-metadata")
chart_link_output = gr.Markdown(label="Chart Link", elem_classes="chart-metadata")
chart_error_output = gr.Markdown(label="Chart Error", elem_classes="chart-metadata")
gr.Markdown("### πŸ“Š Request Details")
request_details = gr.Textbox(label="Request Details", interactive=False, lines=6)
gr.Markdown("### πŸ” Error Details")
error_details = gr.Textbox(
label="Error Details",
interactive=False,
lines=4,
elem_classes=["status-error"]
)
# Function to handle example query clicks
def set_example_query(example_text):
return example_text
# Events
# Health check button
health_btn.click(
fn=check_health,
inputs=[],
outputs=[health_output, error_details]
)
# Schema button
schema_btn.click(
fn=get_schema,
inputs=[],
outputs=[schema_output, error_details]
)
# Example query buttons
for example_btn in [example1, example2, example3, example4, example5, example6, example7, example8]:
example_btn.click(
fn=set_example_query,
inputs=[example_btn],
outputs=[question_input]
)
# Submit button
submit_btn.click(
fn=query_database,
inputs=[question_input, chart_type_dropdown],
outputs=[
sql_output,
results_output,
chart_output,
chatbot_output,
request_details,
error_details,
chart_type_output,
chart_title_output,
chart_format_output,
chart_error_output,
chart_link_output
],
show_progress=True # This will show a progress bar during execution
)
# Launch
if __name__ == "__main__":
demo.launch()