RathodHarish commited on
Commit
77f7dff
·
verified ·
1 Parent(s): 6177ec8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -12
app.py CHANGED
@@ -1,10 +1,12 @@
1
  import gradio as gr
2
  import pandas as pd
3
  import matplotlib.pyplot as plt
4
- from fpdf import FPDF
 
5
  import io
6
  from datetime import datetime, timedelta
7
  import sys
 
8
 
9
  # Check library versions for debugging
10
  debug_msg = "Library Versions:\n"
@@ -13,7 +15,8 @@ try:
13
  debug_msg += f"Gradio: {gr.__version__}\n"
14
  debug_msg += f"Pandas: {pd.__version__}\n"
15
  debug_msg += f"Matplotlib: {matplotlib.__version__}\n"
16
- debug_msg += f"FPDF: {FPDF.FPDF_VERSION}\n"
 
17
  except Exception as e:
18
  debug_msg += f"Error checking library versions: {str(e)}\n"
19
 
@@ -63,7 +66,8 @@ def upload_csv(file):
63
  if timestamps_invalid:
64
  debug_msg_local += "Warning: All Timestamp values are invalid or unparseable. Date range filtering will be disabled.\n"
65
  except Exception as e:
66
- return ["All"], ["All"], ["All"], f"{debug_msg_local}Error: Failed to parse Timestamp column: {str(e)}", "All", "All", "All", None, None, None, None
 
67
 
68
  # Extract unique values for dropdowns
69
  debug_msg_local += "Extracting unique values for dropdowns...\n"
@@ -93,12 +97,13 @@ def upload_csv(file):
93
  device_cards, plot_daily, plot_uptime, anomaly_text, filter_msg = filter_and_visualize("All", "All", "All")
94
  debug_msg_local += f"Initial Filter Result: {filter_msg}\n"
95
  except Exception as e:
96
- debug_msg_local += f"Initial Filter Error: {str(e)}\n"
97
  device_cards, plot_daily, plot_uptime, anomaly_text = None, None, None, None
98
 
99
  return labs, types, date_ranges, debug_msg_local, "All", "All", "All", device_cards, plot_daily, plot_uptime, anomaly_text
100
  except Exception as e:
101
- return ["All"], ["All"], ["All"], f"{debug_msg_local}Failed to process CSV: {str(e)}", "All", "All", "All", None, None, None, None
 
102
 
103
  def filter_and_visualize(selected_lab, selected_type, selected_date_range):
104
  global df
@@ -128,7 +133,7 @@ def filter_and_visualize(selected_lab, selected_type, selected_date_range):
128
  filtered_df = filtered_df[(filtered_df["Timestamp"] >= start_date) & (filtered_df["Timestamp"] < end_date)]
129
  error_msg += f"After Date Range filter ({start_date} to {end_date}): {len(filtered_df)} rows\n"
130
  except Exception as e:
131
- error_msg += f"Error parsing date range: {str(e)}\n"
132
 
133
  if filtered_df.empty:
134
  return None, None, None, None, f"{error_msg}No data matches the selected filters."
@@ -175,7 +180,7 @@ def filter_and_visualize(selected_lab, selected_type, selected_date_range):
175
  plt.close()
176
  plot_daily.seek(0)
177
  except Exception as e:
178
- error_msg += f"Error generating Daily Log Trends: {str(e)}\n"
179
  plt.figure(figsize=(8, 4))
180
  plt.title("Daily Log Trends - Error")
181
  plt.xlabel("Date")
@@ -224,7 +229,7 @@ def filter_and_visualize(selected_lab, selected_type, selected_date_range):
224
  plt.close()
225
  plot_uptime.seek(0)
226
  except Exception as e:
227
- error_msg += f"Error generating Weekly Uptime %: {str(e)}\n"
228
  plt.figure(figsize=(8, 4))
229
  plt.title("Weekly Uptime % - Error")
230
  plt.xlabel("Date")
@@ -242,12 +247,12 @@ def filter_and_visualize(selected_lab, selected_type, selected_date_range):
242
  else:
243
  anomaly_text = "Anomalies Detected:\n" + anomalies[['DeviceID', 'Lab', 'Type', 'Status', 'UsageCount']].to_string(index=False)
244
  except Exception as e:
245
- error_msg += f"Error generating Anomaly Alerts: {str(e)}\n"
246
  anomaly_text = "Error generating anomaly alerts."
247
 
248
  return device_cards, plot_daily, plot_uptime, anomaly_text, f"{error_msg}Filters applied successfully."
249
  except Exception as e:
250
- error_msg += f"Unexpected error in filter_and_visualize: {str(e)}\n"
251
  plt.figure(figsize=(8, 4))
252
  plt.title("Daily Log Trends - Error")
253
  plt.xlabel("Date")
@@ -303,7 +308,7 @@ def download_pdf(selected_lab, selected_type, selected_date_range):
303
  output.seek(0)
304
  return output
305
  except Exception as e:
306
- print(f"Error in download_pdf: {str(e)}")
307
  return None
308
 
309
  # Build the Gradio interface
@@ -355,4 +360,4 @@ try:
355
 
356
  demo.launch()
357
  except Exception as e:
358
- print(f"Error launching Gradio interface: {str(e)}")
 
1
  import gradio as gr
2
  import pandas as pd
3
  import matplotlib.pyplot as plt
4
+ import numpy as np
5
+ from fpdf2 import FPDF
6
  import io
7
  from datetime import datetime, timedelta
8
  import sys
9
+ import traceback
10
 
11
  # Check library versions for debugging
12
  debug_msg = "Library Versions:\n"
 
15
  debug_msg += f"Gradio: {gr.__version__}\n"
16
  debug_msg += f"Pandas: {pd.__version__}\n"
17
  debug_msg += f"Matplotlib: {matplotlib.__version__}\n"
18
+ debug_msg += f"NumPy: {np.__version__}\n"
19
+ debug_msg += f"FPDF2: {FPDF.__version__}\n"
20
  except Exception as e:
21
  debug_msg += f"Error checking library versions: {str(e)}\n"
22
 
 
66
  if timestamps_invalid:
67
  debug_msg_local += "Warning: All Timestamp values are invalid or unparseable. Date range filtering will be disabled.\n"
68
  except Exception as e:
69
+ debug_msg_local += f"Error parsing Timestamp column: {str(e)}\n{traceback.format_exc()}\n"
70
+ return ["All"], ["All"], ["All"], debug_msg_local, "All", "All", "All", None, None, None, None
71
 
72
  # Extract unique values for dropdowns
73
  debug_msg_local += "Extracting unique values for dropdowns...\n"
 
97
  device_cards, plot_daily, plot_uptime, anomaly_text, filter_msg = filter_and_visualize("All", "All", "All")
98
  debug_msg_local += f"Initial Filter Result: {filter_msg}\n"
99
  except Exception as e:
100
+ debug_msg_local += f"Initial Filter Error: {str(e)}\n{traceback.format_exc()}\n"
101
  device_cards, plot_daily, plot_uptime, anomaly_text = None, None, None, None
102
 
103
  return labs, types, date_ranges, debug_msg_local, "All", "All", "All", device_cards, plot_daily, plot_uptime, anomaly_text
104
  except Exception as e:
105
+ debug_msg_local += f"Failed to process CSV: {str(e)}\n{traceback.format_exc()}\n"
106
+ return ["All"], ["All"], ["All"], debug_msg_local, "All", "All", "All", None, None, None, None
107
 
108
  def filter_and_visualize(selected_lab, selected_type, selected_date_range):
109
  global df
 
133
  filtered_df = filtered_df[(filtered_df["Timestamp"] >= start_date) & (filtered_df["Timestamp"] < end_date)]
134
  error_msg += f"After Date Range filter ({start_date} to {end_date}): {len(filtered_df)} rows\n"
135
  except Exception as e:
136
+ error_msg += f"Error parsing date range: {str(e)}\n{traceback.format_exc()}\n"
137
 
138
  if filtered_df.empty:
139
  return None, None, None, None, f"{error_msg}No data matches the selected filters."
 
180
  plt.close()
181
  plot_daily.seek(0)
182
  except Exception as e:
183
+ error_msg += f"Error generating Daily Log Trends: {str(e)}\n{traceback.format_exc()}\n"
184
  plt.figure(figsize=(8, 4))
185
  plt.title("Daily Log Trends - Error")
186
  plt.xlabel("Date")
 
229
  plt.close()
230
  plot_uptime.seek(0)
231
  except Exception as e:
232
+ error_msg += f"Error generating Weekly Uptime %: {str(e)}\n{traceback.format_exc()}\n"
233
  plt.figure(figsize=(8, 4))
234
  plt.title("Weekly Uptime % - Error")
235
  plt.xlabel("Date")
 
247
  else:
248
  anomaly_text = "Anomalies Detected:\n" + anomalies[['DeviceID', 'Lab', 'Type', 'Status', 'UsageCount']].to_string(index=False)
249
  except Exception as e:
250
+ error_msg += f"Error generating Anomaly Alerts: {str(e)}\n{traceback.format_exc()}\n"
251
  anomaly_text = "Error generating anomaly alerts."
252
 
253
  return device_cards, plot_daily, plot_uptime, anomaly_text, f"{error_msg}Filters applied successfully."
254
  except Exception as e:
255
+ error_msg += f"Unexpected error in filter_and_visualize: {str(e)}\n{traceback.format_exc()}\n"
256
  plt.figure(figsize=(8, 4))
257
  plt.title("Daily Log Trends - Error")
258
  plt.xlabel("Date")
 
308
  output.seek(0)
309
  return output
310
  except Exception as e:
311
+ print(f"Error in download_pdf: {str(e)}\n{traceback.format_exc()}")
312
  return None
313
 
314
  # Build the Gradio interface
 
360
 
361
  demo.launch()
362
  except Exception as e:
363
+ print(f"Error launching Gradio interface: {str(e)}\n{traceback.format_exc()}")