DecisionMinds commited on
Commit
bdcb373
·
verified ·
1 Parent(s): 25dc7dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -42
app.py CHANGED
@@ -10,7 +10,7 @@ from PIL import Image
10
  import urllib.parse
11
 
12
  # 🔧 CONFIGURE: Your Flask API URL (Updated to use your IP and port)
13
- FLASK_API_URL = "http://52.14.207.6:8000"
14
 
15
  def query_database(question, chart_type=None):
16
  if not question.strip():
@@ -115,6 +115,7 @@ def query_database(question, chart_type=None):
115
  chart_format = ""
116
  show_image = False
117
  show_html = False
 
118
 
119
  # Process chart if URL is provided
120
  if chart_url:
@@ -127,47 +128,78 @@ def query_database(question, chart_type=None):
127
  full_chart_url = f"{FLASK_API_URL}{chart_url}"
128
 
129
  print(f"Full chart URL: {full_chart_url}")
 
130
 
131
- # Try to fetch the chart
132
- chart_response = requests.get(full_chart_url, timeout=30)
133
-
134
- print(f"Chart response status: {chart_response.status_code}")
135
- print(f"Chart response headers: {dict(chart_response.headers)}")
136
-
137
- if chart_response.status_code == 200:
138
- # Check content type to determine how to handle
139
- content_type = chart_response.headers.get("content-type", "").lower()
140
- print(f"Chart content type: {content_type}")
141
-
142
- if "image" in content_type:
143
- # It's an image, convert to PIL Image
144
- chart_image = Image.open(BytesIO(chart_response.content))
145
- chart_format = content_type.split("/")[-1]
146
- show_image = True
147
- print("Chart image loaded successfully")
148
- elif "html" in content_type:
149
- # It's an HTML visualization
150
- chart_html = chart_response.text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
  chart_format = "html"
152
  show_html = True
153
- print("Chart HTML loaded successfully")
154
- else:
155
- # Try to handle as image anyway
156
- try:
157
- chart_image = Image.open(BytesIO(chart_response.content))
158
- chart_format = "image"
159
- show_image = True
160
- print("Chart loaded as image (fallback)")
161
- except:
162
- chart_error = f"Unsupported chart format: {content_type}"
163
- print(f"ERROR: {chart_error}")
164
-
165
- # Set chart metadata
166
- chart_type_result = metadata.get("chart_type", "unknown")
167
- chart_title = f"Visualization for: {question[:50]}{'...' if len(question) > 50 else ''}"
168
- else:
169
- chart_error = f"Failed to load chart: HTTP {chart_response.status_code}"
170
- print(f"ERROR: {chart_error}")
171
  except Exception as e:
172
  print(f"Error processing chart: {e}")
173
  traceback.print_exc()
@@ -249,7 +281,7 @@ def query_database(question, chart_type=None):
249
 
250
  print(f"=== REQUEST COMPLETED SUCCESSFULLY ===")
251
 
252
- return sql, df, chart_image, chart_html, show_image, show_html, explanation, details, "Success", chart_type_result, chart_title, chart_format, chart_error
253
 
254
  except requests.exceptions.ConnectionError as e:
255
  error_msg = f"Connection failed: {str(e)}"
@@ -535,7 +567,7 @@ with gr.Blocks(theme=theme, title="Enterprise SQL Assistant", css="""
535
  height=400,
536
  elem_classes="chart-container"
537
  )
538
- # Add HTML component for fallback
539
  html_output = gr.HTML(
540
  label="Interactive Chart",
541
  visible=False
@@ -545,6 +577,7 @@ with gr.Blocks(theme=theme, title="Enterprise SQL Assistant", css="""
545
  chart_type_output = gr.Markdown(label="Chart Type", elem_classes="chart-metadata")
546
  chart_title_output = gr.Markdown(label="Chart Title", elem_classes="chart-metadata")
547
  chart_format_output = gr.Markdown(label="Chart Format", elem_classes="chart-metadata")
 
548
  chart_error_output = gr.Markdown(label="Chart Error", elem_classes="chart-metadata")
549
 
550
  gr.Markdown("### 📊 Request Details")
@@ -584,7 +617,7 @@ with gr.Blocks(theme=theme, title="Enterprise SQL Assistant", css="""
584
  submit_btn.click(
585
  fn=query_database,
586
  inputs=[question_input, chart_type_dropdown],
587
- outputs=[sql_output, results_output, chart_output, html_output, gr.Number(visible=False, value=1), gr.Number(visible=False, value=0), chatbot_output, request_details, error_details, chart_type_output, chart_title_output, chart_format_output, chart_error_output]
588
  )
589
 
590
  # Launch
 
10
  import urllib.parse
11
 
12
  # 🔧 CONFIGURE: Your Flask API URL (Updated to use your IP and port)
13
+ FLASK_API_URL = "http://18.188.163.85:8000"
14
 
15
  def query_database(question, chart_type=None):
16
  if not question.strip():
 
115
  chart_format = ""
116
  show_image = False
117
  show_html = False
118
+ chart_link = ""
119
 
120
  # Process chart if URL is provided
121
  if chart_url:
 
128
  full_chart_url = f"{FLASK_API_URL}{chart_url}"
129
 
130
  print(f"Full chart URL: {full_chart_url}")
131
+ chart_link = full_chart_url
132
 
133
+ # Try to determine chart type from URL
134
+ if chart_url.endswith('.html'):
135
+ # It's an HTML chart, create an iframe
136
+ chart_html = f"""
137
+ <div style="width: 100%; height: 500px; overflow: hidden;">
138
+ <iframe src="{full_chart_url}" style="width: 100%; height: 100%; border: none;"></iframe>
139
+ </div>
140
+ """
141
+ chart_format = "html"
142
+ show_html = True
143
+ show_image = False
144
+ print("Chart will be displayed as HTML iframe")
145
+ else:
146
+ # Try to fetch the chart to determine its type
147
+ try:
148
+ chart_response = requests.get(full_chart_url, timeout=30)
149
+
150
+ if chart_response.status_code == 200:
151
+ # Check content type to determine how to handle
152
+ content_type = chart_response.headers.get("content-type", "").lower()
153
+ print(f"Chart content type: {content_type}")
154
+
155
+ if "image" in content_type:
156
+ # It's an image, convert to PIL Image
157
+ chart_image = Image.open(BytesIO(chart_response.content))
158
+ chart_format = content_type.split("/")[-1]
159
+ show_image = True
160
+ show_html = False
161
+ print("Chart loaded as image")
162
+ elif "html" in content_type:
163
+ # It's an HTML visualization
164
+ chart_html = f"""
165
+ <div style="width: 100%; height: 500px; overflow: hidden;">
166
+ <iframe src="{full_chart_url}" style="width: 100%; height: 100%; border: none;"></iframe>
167
+ </div>
168
+ """
169
+ chart_format = "html"
170
+ show_html = True
171
+ show_image = False
172
+ print("Chart loaded as HTML iframe")
173
+ else:
174
+ # Default to iframe for unknown types
175
+ chart_html = f"""
176
+ <div style="width: 100%; height: 500px; overflow: hidden;">
177
+ <iframe src="{full_chart_url}" style="width: 100%; height: 100%; border: none;"></iframe>
178
+ </div>
179
+ """
180
+ chart_format = "html"
181
+ show_html = True
182
+ show_image = False
183
+ print("Chart loaded as HTML iframe (fallback)")
184
+ else:
185
+ chart_error = f"Failed to load chart: HTTP {chart_response.status_code}"
186
+ print(f"ERROR: {chart_error}")
187
+ except Exception as e:
188
+ print(f"Error fetching chart: {e}")
189
+ # Default to iframe even if fetching fails
190
+ chart_html = f"""
191
+ <div style="width: 100%; height: 500px; overflow: hidden;">
192
+ <iframe src="{full_chart_url}" style="width: 100%; height: 100%; border: none;"></iframe>
193
+ </div>
194
+ """
195
  chart_format = "html"
196
  show_html = True
197
+ show_image = False
198
+ print("Chart loaded as HTML iframe (fetch error fallback)")
199
+
200
+ # Set chart metadata
201
+ chart_type_result = metadata.get("chart_type", "unknown")
202
+ chart_title = f"Visualization for: {question[:50]}{'...' if len(question) > 50 else ''}"
 
 
 
 
 
 
 
 
 
 
 
 
203
  except Exception as e:
204
  print(f"Error processing chart: {e}")
205
  traceback.print_exc()
 
281
 
282
  print(f"=== REQUEST COMPLETED SUCCESSFULLY ===")
283
 
284
+ return sql, df, chart_image, chart_html, show_image, show_html, explanation, details, "Success", chart_type_result, chart_title, chart_format, chart_error, chart_link
285
 
286
  except requests.exceptions.ConnectionError as e:
287
  error_msg = f"Connection failed: {str(e)}"
 
567
  height=400,
568
  elem_classes="chart-container"
569
  )
570
+ # Add HTML component for iframe
571
  html_output = gr.HTML(
572
  label="Interactive Chart",
573
  visible=False
 
577
  chart_type_output = gr.Markdown(label="Chart Type", elem_classes="chart-metadata")
578
  chart_title_output = gr.Markdown(label="Chart Title", elem_classes="chart-metadata")
579
  chart_format_output = gr.Markdown(label="Chart Format", elem_classes="chart-metadata")
580
+ chart_link_output = gr.Markdown(label="Chart Link", elem_classes="chart-metadata")
581
  chart_error_output = gr.Markdown(label="Chart Error", elem_classes="chart-metadata")
582
 
583
  gr.Markdown("### 📊 Request Details")
 
617
  submit_btn.click(
618
  fn=query_database,
619
  inputs=[question_input, chart_type_dropdown],
620
+ outputs=[sql_output, results_output, chart_output, html_output, gr.Number(visible=False, value=1), gr.Number(visible=False, value=0), chatbot_output, request_details, error_details, chart_type_output, chart_title_output, chart_format_output, chart_error_output, chart_link_output]
621
  )
622
 
623
  # Launch