Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import pandas as pd
|
|
| 4 |
import base64
|
| 5 |
from io import BytesIO
|
| 6 |
import json
|
|
|
|
| 7 |
|
| 8 |
# π§ CONFIGURE: Replace with your EC2 public IP
|
| 9 |
API_URL = "http://18.191.172.85:5000/ask"
|
|
@@ -11,7 +12,7 @@ HEALTH_URL = "http://18.191.172.85:5000/health"
|
|
| 11 |
|
| 12 |
def test_connection():
|
| 13 |
try:
|
| 14 |
-
response = requests.get(HEALTH_URL, timeout=
|
| 15 |
if response.status_code == 200:
|
| 16 |
return f"β
Connection successful! Status: {response.status_code}\nResponse: {response.text}"
|
| 17 |
else:
|
|
@@ -19,37 +20,64 @@ def test_connection():
|
|
| 19 |
except requests.exceptions.ConnectionError:
|
| 20 |
return "β Connection failed. The server might be down or not accessible from Hugging Face."
|
| 21 |
except requests.exceptions.Timeout:
|
| 22 |
-
return "β±οΈ Connection timed out."
|
| 23 |
except Exception as e:
|
| 24 |
return f"π¨ Unexpected error: {str(e)}"
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
def query_database(question):
|
| 27 |
if not question.strip():
|
| 28 |
-
return "", None, None, "β οΈ Please enter a question."
|
| 29 |
|
| 30 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
response = requests.post(
|
| 32 |
API_URL,
|
| 33 |
json={"question": question},
|
| 34 |
-
timeout=
|
| 35 |
)
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
try:
|
| 38 |
result = response.json()
|
| 39 |
except json.JSONDecodeError:
|
| 40 |
-
return "", None, None, f"β Invalid response from server: {response.text}"
|
| 41 |
|
| 42 |
if response.status_code != 200:
|
| 43 |
error_msg = result.get("error", f"HTTP {response.status_code}")
|
| 44 |
-
return "", None, None, f"β Server error: {error_msg}"
|
| 45 |
|
| 46 |
if "error" in result:
|
| 47 |
-
return "", None, None, f"β {result['error']}"
|
| 48 |
|
| 49 |
sql = result.get("sql", "N/A")
|
| 50 |
rows = result.get("result", [])
|
| 51 |
chart_data = result.get("chart")
|
| 52 |
|
|
|
|
|
|
|
|
|
|
| 53 |
df = pd.DataFrame(rows) if rows else pd.DataFrame()
|
| 54 |
|
| 55 |
chart_image = None
|
|
@@ -57,17 +85,22 @@ def query_database(question):
|
|
| 57 |
try:
|
| 58 |
image_bytes = base64.b64decode(chart_data)
|
| 59 |
chart_image = BytesIO(image_bytes)
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
print(f"Error decoding chart: {e}")
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
except requests.exceptions.ConnectionError:
|
| 66 |
-
return "", None, None, "β Connection failed
|
| 67 |
except requests.exceptions.Timeout:
|
| 68 |
-
return "", None, None, "β±οΈ Request timed out. Try a simpler question."
|
| 69 |
except Exception as e:
|
| 70 |
-
return "", None, None, f"π¨ Unexpected error: {str(e)}"
|
| 71 |
|
| 72 |
# π¨ Theme: Enterprise Dark Blue
|
| 73 |
theme = gr.themes.Default(
|
|
@@ -105,6 +138,11 @@ with gr.Blocks(theme=theme, title="Enterprise SQL Assistant") as demo:
|
|
| 105 |
test_btn = gr.Button("Test Connection to EC2")
|
| 106 |
connection_status = gr.Textbox(label="Connection Status", interactive=False)
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
gr.Markdown("### π‘ Example Queries")
|
| 109 |
gr.Examples(
|
| 110 |
examples=[
|
|
@@ -113,7 +151,7 @@ with gr.Blocks(theme=theme, title="Enterprise SQL Assistant") as demo:
|
|
| 113 |
["Show members with their account balances"],
|
| 114 |
["Which member has the highest balance?"],
|
| 115 |
["Show transaction trends over time"],
|
| 116 |
-
["Count of members by status"],
|
| 117 |
["Show me the first 10 rows"],
|
| 118 |
["What is the average age of members?"]
|
| 119 |
],
|
|
@@ -143,6 +181,9 @@ with gr.Blocks(theme=theme, title="Enterprise SQL Assistant") as demo:
|
|
| 143 |
height=400,
|
| 144 |
elem_classes="chart-container"
|
| 145 |
)
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
# Events
|
| 148 |
test_btn.click(
|
|
@@ -150,13 +191,17 @@ with gr.Blocks(theme=theme, title="Enterprise SQL Assistant") as demo:
|
|
| 150 |
outputs=[connection_status]
|
| 151 |
)
|
| 152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
submit_btn.click(
|
| 154 |
fn=query_database,
|
| 155 |
inputs=question_input,
|
| 156 |
-
outputs=[sql_output, results_output, chart_output, status]
|
| 157 |
)
|
| 158 |
|
| 159 |
# Launch
|
| 160 |
if __name__ == "__main__":
|
| 161 |
-
demo.launch()
|
| 162 |
-
|
|
|
|
| 4 |
import base64
|
| 5 |
from io import BytesIO
|
| 6 |
import json
|
| 7 |
+
import time
|
| 8 |
|
| 9 |
# π§ CONFIGURE: Replace with your EC2 public IP
|
| 10 |
API_URL = "http://18.191.172.85:5000/ask"
|
|
|
|
| 12 |
|
| 13 |
def test_connection():
|
| 14 |
try:
|
| 15 |
+
response = requests.get(HEALTH_URL, timeout=30) # Increased to 30 seconds
|
| 16 |
if response.status_code == 200:
|
| 17 |
return f"β
Connection successful! Status: {response.status_code}\nResponse: {response.text}"
|
| 18 |
else:
|
|
|
|
| 20 |
except requests.exceptions.ConnectionError:
|
| 21 |
return "β Connection failed. The server might be down or not accessible from Hugging Face."
|
| 22 |
except requests.exceptions.Timeout:
|
| 23 |
+
return "β±οΈ Connection timed out after 30 seconds."
|
| 24 |
except Exception as e:
|
| 25 |
return f"π¨ Unexpected error: {str(e)}"
|
| 26 |
|
| 27 |
+
def test_chart_generation():
|
| 28 |
+
try:
|
| 29 |
+
response = requests.get("http://18.191.172.85:5000/test-chart", timeout=60) # Increased to 60 seconds
|
| 30 |
+
if response.status_code == 200:
|
| 31 |
+
result = response.json()
|
| 32 |
+
if 'chart' in result:
|
| 33 |
+
image_bytes = base64.b64decode(result['chart'])
|
| 34 |
+
chart_image = BytesIO(image_bytes)
|
| 35 |
+
return "β
Test chart generated successfully!", chart_image
|
| 36 |
+
else:
|
| 37 |
+
return f"β Error: {result.get('message', 'Unknown error')}", None
|
| 38 |
+
else:
|
| 39 |
+
return f"β HTTP Error: {response.status_code}", None
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return f"β Connection error: {str(e)}", None
|
| 42 |
+
|
| 43 |
def query_database(question):
|
| 44 |
if not question.strip():
|
| 45 |
+
return "", None, None, "β οΈ Please enter a question.", ""
|
| 46 |
|
| 47 |
try:
|
| 48 |
+
start_time = time.time()
|
| 49 |
+
print(f"Sending request to: {API_URL}")
|
| 50 |
+
print(f"Question: {question}")
|
| 51 |
+
|
| 52 |
response = requests.post(
|
| 53 |
API_URL,
|
| 54 |
json={"question": question},
|
| 55 |
+
timeout=300 # Increased timeout to 300 seconds (5 minutes)
|
| 56 |
)
|
| 57 |
|
| 58 |
+
elapsed_time = time.time() - start_time
|
| 59 |
+
print(f"Response status: {response.status_code}")
|
| 60 |
+
print(f"Response time: {elapsed_time:.2f} seconds")
|
| 61 |
+
|
| 62 |
try:
|
| 63 |
result = response.json()
|
| 64 |
except json.JSONDecodeError:
|
| 65 |
+
return "", None, None, f"β Invalid response from server: {response.text}", f"Request time: {elapsed_time:.2f}s"
|
| 66 |
|
| 67 |
if response.status_code != 200:
|
| 68 |
error_msg = result.get("error", f"HTTP {response.status_code}")
|
| 69 |
+
return "", None, None, f"β Server error: {error_msg}", f"Request time: {elapsed_time:.2f}s"
|
| 70 |
|
| 71 |
if "error" in result:
|
| 72 |
+
return "", None, None, f"β {result['error']}", f"Request time: {elapsed_time:.2f}s"
|
| 73 |
|
| 74 |
sql = result.get("sql", "N/A")
|
| 75 |
rows = result.get("result", [])
|
| 76 |
chart_data = result.get("chart")
|
| 77 |
|
| 78 |
+
print(f"Chart data received: {'Yes' if chart_data else 'No'}")
|
| 79 |
+
print(f"Chart data length: {len(chart_data) if chart_data else 0}")
|
| 80 |
+
|
| 81 |
df = pd.DataFrame(rows) if rows else pd.DataFrame()
|
| 82 |
|
| 83 |
chart_image = None
|
|
|
|
| 85 |
try:
|
| 86 |
image_bytes = base64.b64decode(chart_data)
|
| 87 |
chart_image = BytesIO(image_bytes)
|
| 88 |
+
print("Chart decoded successfully")
|
| 89 |
except Exception as e:
|
| 90 |
print(f"Error decoding chart: {e}")
|
| 91 |
|
| 92 |
+
details = f"Request time: {elapsed_time:.2f}s\n"
|
| 93 |
+
details += f"Rows returned: {len(rows)}\n"
|
| 94 |
+
details += f"Chart generated: {'Yes' if chart_data else 'No'}"
|
| 95 |
+
|
| 96 |
+
return sql, df, chart_image, "β
Query completed successfully!", details
|
| 97 |
|
| 98 |
+
except requests.exceptions.ConnectionError as e:
|
| 99 |
+
return "", None, None, f"β Connection failed: {str(e)}", "Connection error"
|
| 100 |
except requests.exceptions.Timeout:
|
| 101 |
+
return "", None, None, "β±οΈ Request timed out after 300 seconds. Try a simpler question.", "Timeout error"
|
| 102 |
except Exception as e:
|
| 103 |
+
return "", None, None, f"π¨ Unexpected error: {str(e)}", f"Error: {str(e)}"
|
| 104 |
|
| 105 |
# π¨ Theme: Enterprise Dark Blue
|
| 106 |
theme = gr.themes.Default(
|
|
|
|
| 138 |
test_btn = gr.Button("Test Connection to EC2")
|
| 139 |
connection_status = gr.Textbox(label="Connection Status", interactive=False)
|
| 140 |
|
| 141 |
+
gr.Markdown("### π¨ Chart Generation Test")
|
| 142 |
+
test_chart_btn = gr.Button("Test Chart Generation")
|
| 143 |
+
chart_test_status = gr.Textbox(label="Chart Test Status", interactive=False)
|
| 144 |
+
test_chart_output = gr.Image(label="Test Chart", type="pil")
|
| 145 |
+
|
| 146 |
gr.Markdown("### π‘ Example Queries")
|
| 147 |
gr.Examples(
|
| 148 |
examples=[
|
|
|
|
| 151 |
["Show members with their account balances"],
|
| 152 |
["Which member has the highest balance?"],
|
| 153 |
["Show transaction trends over time"],
|
| 154 |
+
["Count of members by status"], # This should generate a chart
|
| 155 |
["Show me the first 10 rows"],
|
| 156 |
["What is the average age of members?"]
|
| 157 |
],
|
|
|
|
| 181 |
height=400,
|
| 182 |
elem_classes="chart-container"
|
| 183 |
)
|
| 184 |
+
|
| 185 |
+
gr.Markdown("### π Request Details")
|
| 186 |
+
request_details = gr.Textbox(label="Request Details", interactive=False, lines=4)
|
| 187 |
|
| 188 |
# Events
|
| 189 |
test_btn.click(
|
|
|
|
| 191 |
outputs=[connection_status]
|
| 192 |
)
|
| 193 |
|
| 194 |
+
test_chart_btn.click(
|
| 195 |
+
fn=test_chart_generation,
|
| 196 |
+
outputs=[chart_test_status, test_chart_output]
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
submit_btn.click(
|
| 200 |
fn=query_database,
|
| 201 |
inputs=question_input,
|
| 202 |
+
outputs=[sql_output, results_output, chart_output, status, request_details]
|
| 203 |
)
|
| 204 |
|
| 205 |
# Launch
|
| 206 |
if __name__ == "__main__":
|
| 207 |
+
demo.launch()
|
|
|