Shreyass334 commited on
Commit
1a294ac
Β·
verified Β·
1 Parent(s): 5af5ad4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -14
app.py CHANGED
@@ -8,8 +8,8 @@ import time
8
  import traceback
9
  from PIL import Image
10
 
11
- # πŸ”§ CONFIGURE: Replace with your Django server URL
12
- DJANGO_API_URL = "http://your-django-server.com/api/openai-prompt/"
13
 
14
  def query_database(question):
15
  if not question.strip():
@@ -24,15 +24,15 @@ def query_database(question):
24
  print("="*50)
25
 
26
  # Prepare the request
27
- payload = {"prompt": question}
28
 
29
- print(f"Request URL: {DJANGO_API_URL}")
30
  print(f"Request payload: {payload}")
31
 
32
  # Make the request
33
  print("Sending request...")
34
  response = requests.post(
35
- DJANGO_API_URL,
36
  json=payload,
37
  timeout=300
38
  )
@@ -61,18 +61,11 @@ def query_database(question):
61
 
62
  # Check HTTP status
63
  if response.status_code != 200:
64
- error_msg = result.get("message", f"HTTP {response.status_code}")
65
  print(f"ERROR: HTTP status {response.status_code}: {error_msg}")
66
  return "", None, None, f"❌ Server error: {error_msg}", f"Request time: {elapsed_time:.2f}s", "HTTP error"
67
 
68
- # Check for application error
69
- if "status" in result and result["status"] == "error":
70
- error_msg = result.get("message", "Unknown error")
71
- print(f"ERROR: Application error: {error_msg}")
72
- return "", None, None, f"❌ {error_msg}", f"Request time: {elapsed_time:.2f}s", "Application error"
73
-
74
  # Extract data
75
- conversational_response = result.get("response", "")
76
  sql = result.get("sql", "")
77
  rows = result.get("result", [])
78
  chart_data = result.get("chart")
@@ -106,7 +99,7 @@ def query_database(question):
106
 
107
  print(f"=== REQUEST COMPLETED SUCCESSFULLY ===")
108
 
109
- return sql, df, chart_image, conversational_response, details, "Success"
110
 
111
  except requests.exceptions.ConnectionError as e:
112
  error_msg = f"Connection failed: {str(e)}"
@@ -128,6 +121,29 @@ def query_database(question):
128
  print(f"Traceback: {traceback.format_exc()}")
129
  return "", None, None, f"🚨 {error_msg}", f"Error: {str(e)}", "Unexpected error"
130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
  # 🎨 Theme: Enterprise Dark Blue
132
  theme = gr.themes.Default(
133
  primary_hue="blue",
@@ -233,6 +249,10 @@ with gr.Blocks(theme=theme, title="Enterprise SQL Assistant", css="""
233
  example6 = gr.Button("Count of members by status", elem_classes=["example-btn"])
234
  example7 = gr.Button("Show me the first 10 rows", elem_classes=["example-btn"])
235
  example8 = gr.Button("What is the average age of members?", elem_classes=["example-btn"])
 
 
 
 
236
 
237
  with gr.Column(scale=2):
238
  gr.Markdown("### πŸ€– AI Assistant Response")
@@ -271,6 +291,13 @@ with gr.Blocks(theme=theme, title="Enterprise SQL Assistant", css="""
271
  return example_text
272
 
273
  # Events
 
 
 
 
 
 
 
274
  # Example query buttons
275
  for example_btn in [example1, example2, example3, example4, example5, example6, example7, example8]:
276
  example_btn.click(
 
8
  import traceback
9
  from PIL import Image
10
 
11
+ # πŸ”§ CONFIGURE: Your Flask API URL
12
+ FLASK_API_URL = "http://3.14.153.135:5000"
13
 
14
  def query_database(question):
15
  if not question.strip():
 
24
  print("="*50)
25
 
26
  # Prepare the request
27
+ payload = {"question": question}
28
 
29
+ print(f"Request URL: {FLASK_API_URL}/ask")
30
  print(f"Request payload: {payload}")
31
 
32
  # Make the request
33
  print("Sending request...")
34
  response = requests.post(
35
+ f"{FLASK_API_URL}/ask",
36
  json=payload,
37
  timeout=300
38
  )
 
61
 
62
  # Check HTTP status
63
  if response.status_code != 200:
64
+ error_msg = result.get("error", f"HTTP {response.status_code}")
65
  print(f"ERROR: HTTP status {response.status_code}: {error_msg}")
66
  return "", None, None, f"❌ Server error: {error_msg}", f"Request time: {elapsed_time:.2f}s", "HTTP error"
67
 
 
 
 
 
 
 
68
  # Extract data
 
69
  sql = result.get("sql", "")
70
  rows = result.get("result", [])
71
  chart_data = result.get("chart")
 
99
 
100
  print(f"=== REQUEST COMPLETED SUCCESSFULLY ===")
101
 
102
+ return sql, df, chart_image, f"βœ… Query completed successfully", details, "Success"
103
 
104
  except requests.exceptions.ConnectionError as e:
105
  error_msg = f"Connection failed: {str(e)}"
 
121
  print(f"Traceback: {traceback.format_exc()}")
122
  return "", None, None, f"🚨 {error_msg}", f"Error: {str(e)}", "Unexpected error"
123
 
124
+ def check_health():
125
+ try:
126
+ print("Checking API health...")
127
+ response = requests.get(f"{FLASK_API_URL}/health", timeout=10)
128
+
129
+ if response.status_code == 200:
130
+ health_data = response.json()
131
+ status = health_data.get('status', 'unknown')
132
+ model = health_data.get('model', 'unknown')
133
+ tables = health_data.get('tables', [])
134
+ data_rows = health_data.get('data_rows', 0)
135
+
136
+ health_msg = f"βœ… API Status: {status.upper()}\n"
137
+ health_msg += f"πŸ€– Model: {model}\n"
138
+ health_msg += f"πŸ“Š Tables: {', '.join(tables) if tables else 'None'}\n"
139
+ health_msg += f"πŸ“ˆ Data Rows: {data_rows:,}"
140
+
141
+ return health_msg, "success"
142
+ else:
143
+ return f"❌ API returned status {response.status_code}", "error"
144
+ except Exception as e:
145
+ return f"❌ Health check failed: {str(e)}", "error"
146
+
147
  # 🎨 Theme: Enterprise Dark Blue
148
  theme = gr.themes.Default(
149
  primary_hue="blue",
 
249
  example6 = gr.Button("Count of members by status", elem_classes=["example-btn"])
250
  example7 = gr.Button("Show me the first 10 rows", elem_classes=["example-btn"])
251
  example8 = gr.Button("What is the average age of members?", elem_classes=["example-btn"])
252
+
253
+ gr.Markdown("### 🩺 API Health Check")
254
+ health_btn = gr.Button("Check API Health", variant="secondary")
255
+ health_output = gr.Markdown(label="API Status")
256
 
257
  with gr.Column(scale=2):
258
  gr.Markdown("### πŸ€– AI Assistant Response")
 
291
  return example_text
292
 
293
  # Events
294
+ # Health check button
295
+ health_btn.click(
296
+ fn=check_health,
297
+ inputs=[],
298
+ outputs=[health_output, error_details]
299
+ )
300
+
301
  # Example query buttons
302
  for example_btn in [example1, example2, example3, example4, example5, example6, example7, example8]:
303
  example_btn.click(