Shreyass334 commited on
Commit
70ea730
Β·
verified Β·
1 Parent(s): 5b9139a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -14
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
- # Example: "http://3.8.123.45:5000/ask"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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(API_URL, json={"question": question}, timeout=30)
18
- result = response.json()
 
 
 
 
 
 
 
 
 
 
 
 
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") # base64 PNG
26
 
27
  df = pd.DataFrame(rows) if rows else pd.DataFrame()
28
 
29
- # Decode chart
30
  chart_image = None
31
  if chart_data:
32
- image_bytes = base64.b64decode(chart_data)
33
- chart_image = BytesIO(image_bytes)
 
 
 
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,