Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,69 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
import base64
|
| 6 |
from io import BytesIO
|
|
|
|
| 7 |
|
| 8 |
# π§ CONFIGURE: Replace with your EC2 public IP
|
| 9 |
API_URL = "http://18.191.172.85:5000/ask"
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def query_database(question):
|
| 13 |
if not question.strip():
|
| 14 |
-
return "", None, None, "Please enter a question."
|
| 15 |
|
| 16 |
try:
|
| 17 |
-
response = requests.post(
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
if "error" in result:
|
| 21 |
return "", None, None, f"β {result['error']}"
|
| 22 |
|
| 23 |
sql = result.get("sql", "N/A")
|
| 24 |
rows = result.get("result", [])
|
| 25 |
-
chart_data = result.get("chart")
|
| 26 |
|
| 27 |
df = pd.DataFrame(rows) if rows else pd.DataFrame()
|
| 28 |
|
| 29 |
-
# Decode chart
|
| 30 |
chart_image = None
|
| 31 |
if chart_data:
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
return sql, df, chart_image, ""
|
| 36 |
|
| 37 |
except requests.exceptions.ConnectionError:
|
| 38 |
-
return "", None, None, "β Connection failed. Is your API running?"
|
| 39 |
except requests.exceptions.Timeout:
|
| 40 |
return "", None, None, "β±οΈ Request timed out. Try a simpler question."
|
| 41 |
except Exception as e:
|
|
@@ -56,8 +84,8 @@ theme = gr.themes.Default(
|
|
| 56 |
with gr.Blocks(theme=theme, title="Enterprise SQL Assistant") as demo:
|
| 57 |
gr.HTML("""
|
| 58 |
<div style="text-align: center; padding: 20px;">
|
| 59 |
-
<h1 style="color: #3B82F6;">π Enterprise SQL Assistant</h1>
|
| 60 |
-
<p style="color: #9CA3AF; font-size: 1.1em;">
|
| 61 |
Ask questions about your data. Get SQL, results, and insights.
|
| 62 |
</p>
|
| 63 |
</div>
|
|
@@ -73,6 +101,10 @@ with gr.Blocks(theme=theme, title="Enterprise SQL Assistant") as demo:
|
|
| 73 |
)
|
| 74 |
submit_btn = gr.Button("π Generate SQL & Results", variant="primary")
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
gr.Markdown("### π‘ Example Queries")
|
| 77 |
gr.Examples(
|
| 78 |
examples=[
|
|
@@ -80,7 +112,10 @@ with gr.Blocks(theme=theme, title="Enterprise SQL Assistant") as demo:
|
|
| 80 |
["What is the total transaction amount?"],
|
| 81 |
["Show members with their account balances"],
|
| 82 |
["Which member has the highest balance?"],
|
| 83 |
-
["Show transaction trends over time"]
|
|
|
|
|
|
|
|
|
|
| 84 |
],
|
| 85 |
inputs=question_input,
|
| 86 |
label=""
|
|
@@ -110,6 +145,11 @@ with gr.Blocks(theme=theme, title="Enterprise SQL Assistant") as demo:
|
|
| 110 |
)
|
| 111 |
|
| 112 |
# Events
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
submit_btn.click(
|
| 114 |
fn=query_database,
|
| 115 |
inputs=question_input,
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
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"
|
| 10 |
+
HEALTH_URL = "http://18.191.172.85:5000/health"
|
| 11 |
+
|
| 12 |
+
def test_connection():
|
| 13 |
+
try:
|
| 14 |
+
response = requests.get(HEALTH_URL, timeout=10)
|
| 15 |
+
if response.status_code == 200:
|
| 16 |
+
return f"β
Connection successful! Status: {response.status_code}\nResponse: {response.text}"
|
| 17 |
+
else:
|
| 18 |
+
return f"β οΈ Connection returned status: {response.status_code}\nResponse: {response.text}"
|
| 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=60
|
| 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
|
| 56 |
if chart_data:
|
| 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 |
+
return sql, df, chart_image, "β
Query completed successfully!"
|
| 64 |
|
| 65 |
except requests.exceptions.ConnectionError:
|
| 66 |
+
return "", None, None, "β Connection failed. Is your API running and accessible from Hugging Face?"
|
| 67 |
except requests.exceptions.Timeout:
|
| 68 |
return "", None, None, "β±οΈ Request timed out. Try a simpler question."
|
| 69 |
except Exception as e:
|
|
|
|
| 84 |
with gr.Blocks(theme=theme, title="Enterprise SQL Assistant") as demo:
|
| 85 |
gr.HTML("""
|
| 86 |
<div style="text-align: center; padding: 20px;">
|
| 87 |
+
<h1 style="color: #3B82F6; margin-bottom: 5px;">π Enterprise SQL Assistant</h1>
|
| 88 |
+
<p style="color: #9CA3AF; font-size: 1.1em; margin-top: 0;">
|
| 89 |
Ask questions about your data. Get SQL, results, and insights.
|
| 90 |
</p>
|
| 91 |
</div>
|
|
|
|
| 101 |
)
|
| 102 |
submit_btn = gr.Button("π Generate SQL & Results", variant="primary")
|
| 103 |
|
| 104 |
+
gr.Markdown("### π§ Connection Test")
|
| 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=[
|
|
|
|
| 112 |
["What is the total transaction amount?"],
|
| 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 |
],
|
| 120 |
inputs=question_input,
|
| 121 |
label=""
|
|
|
|
| 145 |
)
|
| 146 |
|
| 147 |
# Events
|
| 148 |
+
test_btn.click(
|
| 149 |
+
fn=test_connection,
|
| 150 |
+
outputs=[connection_status]
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
submit_btn.click(
|
| 154 |
fn=query_database,
|
| 155 |
inputs=question_input,
|