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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -1
app.py CHANGED
@@ -131,9 +131,13 @@ def query_database(question, chart_type=None):
131
  # Try to fetch the chart
132
  chart_response = requests.get(full_chart_url, timeout=30)
133
 
 
 
 
134
  if chart_response.status_code == 200:
135
  # Check content type to determine how to handle
136
- content_type = chart_response.headers.get("content-type", "")
 
137
 
138
  if "image" in content_type:
139
  # It's an image, convert to PIL Image
@@ -147,6 +151,16 @@ def query_database(question, chart_type=None):
147
  chart_format = "html"
148
  show_html = True
149
  print("Chart HTML loaded successfully")
 
 
 
 
 
 
 
 
 
 
150
 
151
  # Set chart metadata
152
  chart_type_result = metadata.get("chart_type", "unknown")
@@ -156,17 +170,75 @@ def query_database(question, chart_type=None):
156
  print(f"ERROR: {chart_error}")
157
  except Exception as e:
158
  print(f"Error processing chart: {e}")
 
159
  chart_error = f"Chart processing error: {str(e)}"
160
  else:
161
  print("No chart URL provided")
162
  chart_error = "No chart available"
163
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
  # Prepare details
165
  details = f"Request time: {elapsed_time:.2f}s\n"
166
  details += f"Status code: {response.status_code}\n"
167
  details += f"Rows returned: {row_count}\n"
168
  details += f"Columns: {', '.join(metadata.get('columns', []))}\n"
169
  details += f"Database type: {metadata.get('database_type', 'unknown')}\n"
 
 
170
  if chart_type_result:
171
  details += f"Chart type: {chart_type_result}\n"
172
  if chart_title:
 
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
 
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")
 
170
  print(f"ERROR: {chart_error}")
171
  except Exception as e:
172
  print(f"Error processing chart: {e}")
173
+ traceback.print_exc()
174
  chart_error = f"Chart processing error: {str(e)}"
175
  else:
176
  print("No chart URL provided")
177
  chart_error = "No chart available"
178
 
179
+ # If we still don't have a chart but have data, try to create a simple chart
180
+ if not chart_image and not chart_html and not chart_error and not df.empty:
181
+ try:
182
+ # Create a simple bar chart from the data
183
+ import matplotlib.pyplot as plt
184
+ plt.figure(figsize=(10, 6))
185
+
186
+ # If we have at least two columns, use the first two
187
+ if len(df.columns) >= 2:
188
+ x_col = df.columns[0]
189
+ y_col = df.columns[1]
190
+
191
+ # Check if y column is numeric
192
+ if pd.api.types.is_numeric_dtype(df[y_col]):
193
+ df_sorted = df.sort_values(by=y_col, ascending=False).head(10)
194
+ plt.bar(df_sorted[x_col].astype(str), df_sorted[y_col])
195
+ plt.xticks(rotation=45, ha='right')
196
+ plt.xlabel(x_col)
197
+ plt.ylabel(y_col)
198
+ plt.title(f"{y_col} by {x_col}")
199
+ plt.tight_layout()
200
+
201
+ # Convert to PIL Image
202
+ buf = BytesIO()
203
+ plt.savefig(buf, format='png')
204
+ buf.seek(0)
205
+ chart_image = Image.open(buf)
206
+ chart_format = "png"
207
+ show_image = True
208
+ chart_type_result = "bar"
209
+ chart_title = f"Bar chart: {y_col} by {x_col}"
210
+ print("Created fallback bar chart")
211
+ else:
212
+ # If only one column, create a simple value chart
213
+ col = df.columns[0]
214
+ plt.plot(df.index, df[col])
215
+ plt.xlabel("Index")
216
+ plt.ylabel(col)
217
+ plt.title(f"Values of {col}")
218
+ plt.tight_layout()
219
+
220
+ # Convert to PIL Image
221
+ buf = BytesIO()
222
+ plt.savefig(buf, format='png')
223
+ buf.seek(0)
224
+ chart_image = Image.open(buf)
225
+ chart_format = "png"
226
+ show_image = True
227
+ chart_type_result = "line"
228
+ chart_title = f"Line chart: {col}"
229
+ print("Created fallback line chart")
230
+ except Exception as e:
231
+ print(f"Error creating fallback chart: {e}")
232
+ chart_error = f"Could not create chart: {str(e)}"
233
+
234
  # Prepare details
235
  details = f"Request time: {elapsed_time:.2f}s\n"
236
  details += f"Status code: {response.status_code}\n"
237
  details += f"Rows returned: {row_count}\n"
238
  details += f"Columns: {', '.join(metadata.get('columns', []))}\n"
239
  details += f"Database type: {metadata.get('database_type', 'unknown')}\n"
240
+ if chart_url:
241
+ details += f"Chart URL: {chart_url}\n"
242
  if chart_type_result:
243
  details += f"Chart type: {chart_type_result}\n"
244
  if chart_title: