Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -70,16 +70,16 @@ def upload_csv(file):
|
|
| 70 |
date_ranges = ['All']
|
| 71 |
debug_msg += "Warning: Could not determine date range due to invalid timestamps.\n"
|
| 72 |
else:
|
| 73 |
-
|
| 74 |
-
|
| 75 |
date_ranges = ['All', f"{min_date_str} to {max_date_str}"]
|
| 76 |
-
debug_msg += f"Date Range: {
|
| 77 |
|
| 78 |
-
# Automatically trigger
|
| 79 |
debug_msg += "Triggering initial visualization with default filters...\n"
|
| 80 |
try:
|
| 81 |
-
result =
|
| 82 |
-
device_cards, plot_daily,
|
| 83 |
debug_msg += f"Initial Filter Result: {filter_msg}\n"
|
| 84 |
except Exception as e:
|
| 85 |
debug_msg += f"Initial Filter Error: {str(e)}\n"
|
|
@@ -87,9 +87,9 @@ def upload_csv(file):
|
|
| 87 |
|
| 88 |
return labs, types, date_ranges, debug_msg, "All", "All", "All", device_cards, plot_daily, plot_uptime, anomaly_text
|
| 89 |
except Exception as e:
|
| 90 |
-
return ["All"], ["All"], ["All"], f"{debug_msg}Failed to process CSV: {str(e)}", "All", "All", "All", None, None, None, None
|
| 91 |
|
| 92 |
-
def
|
| 93 |
global df
|
| 94 |
if df.empty:
|
| 95 |
return None, None, None, None, "No data available."
|
|
@@ -102,7 +102,7 @@ def filter_and_display(selected_lab, selected_type, selected_date_range):
|
|
| 102 |
error_msg += f"Initial DataFrame: {len(filtered_df)} rows\n"
|
| 103 |
|
| 104 |
if selected_lab != "All":
|
| 105 |
-
filtered_df = filtered_df[filtered_df["Lab
|
| 106 |
error_msg += f"After Lab filter ({selected_lab}): {len(filtered_df)} rows\n"
|
| 107 |
if selected_type != "All":
|
| 108 |
filtered_df = filtered_df[filtered_df["Type"] == selected_type]
|
|
@@ -112,21 +112,21 @@ def filter_and_display(selected_lab, selected_type, selected_date_range):
|
|
| 112 |
start_date, end_date = selected_date_range.split(" to ")
|
| 113 |
start_date = pd.to_datetime(start_date)
|
| 114 |
end_date = pd.to_datetime(end_date) + timedelta(days=1) # Include end date
|
| 115 |
-
filtered_df = filtered_df[(filtered_df["Timestamp"] >= start_date) & (filtered_df[
|
| 116 |
error_msg += f"After Date Range filter ({start_date} to {end_date}): {len(filtered_df)} rows\n"
|
| 117 |
except Exception as e:
|
| 118 |
error_msg += f"Error parsing date range: {str(e)}\n"
|
| 119 |
|
| 120 |
if filtered_df.empty:
|
| 121 |
-
return None, None, None, None, f"{error_msg}No data matches the selected filters
|
| 122 |
|
| 123 |
# Debug: Log the filtered DataFrame
|
| 124 |
-
error_msg += f"Filtered DataFrame:\n{filtered_df.to_string()}\n
|
| 125 |
|
| 126 |
# Device Cards (as a table)
|
| 127 |
-
device_cards = filtered_df[['DeviceID', 'Lab', 'Type', 'UsageCount', 'Timestamp']].sort_values(by='Timestamp')
|
| 128 |
|
| 129 |
-
# Daily Log Trends
|
| 130 |
try:
|
| 131 |
if df['Timestamp'].isna().all():
|
| 132 |
error_msg += "Warning: All timestamps are invalid. Skipping Daily Log Trends.\n"
|
|
@@ -232,7 +232,7 @@ def filter_and_display(selected_lab, selected_type, selected_date_range):
|
|
| 232 |
error_msg += f"Error generating Anomaly Alerts: {str(e)}\n"
|
| 233 |
anomaly_text = "Error generating anomaly alerts."
|
| 234 |
|
| 235 |
-
return device_cards, buf1, buf2, anomaly_text, f"{error_msg}
|
| 236 |
|
| 237 |
def download_pdf(selected_lab, selected_type, selected_date_range):
|
| 238 |
global df
|
|
@@ -299,13 +299,13 @@ with gr.Blocks() as demo:
|
|
| 299 |
csv_input.change(
|
| 300 |
fn=upload_csv,
|
| 301 |
inputs=csv_input,
|
| 302 |
-
outputs=[lab_dropdown, type_dropdown, date_dropdown, error_box, lab_dropdown, type_dropdown, date_dropdown, device_cards, plot_daily, plot_uptime, anomaly_output]
|
| 303 |
)
|
| 304 |
|
| 305 |
submit_btn.click(
|
| 306 |
fn=filter_and_visualize,
|
| 307 |
inputs=[lab_dropdown, type_dropdown, date_dropdown],
|
| 308 |
-
outputs=[device_cards, plot_daily, plot_uptime, anomaly_output, error_box]
|
| 309 |
)
|
| 310 |
|
| 311 |
download_btn.click(
|
|
|
|
| 70 |
date_ranges = ['All']
|
| 71 |
debug_msg += "Warning: Could not determine date range due to invalid timestamps.\n"
|
| 72 |
else:
|
| 73 |
+
min_date_str = min_date.strftime('%Y-%m-%d')
|
| 74 |
+
max_date_str = max_date.strftime('%Y-%m-%d')
|
| 75 |
date_ranges = ['All', f"{min_date_str} to {max_date_str}"]
|
| 76 |
+
debug_msg += f"Date Range: {min_date_str} to {max_date_str}\n"
|
| 77 |
|
| 78 |
+
# Automatically trigger filter_and_visualize after upload with default filters
|
| 79 |
debug_msg += "Triggering initial visualization with default filters...\n"
|
| 80 |
try:
|
| 81 |
+
result = filter_and_visualize("All", "All", "All")
|
| 82 |
+
device_cards, plot_daily, plot_uptime, anomaly_text, filter_msg = result
|
| 83 |
debug_msg += f"Initial Filter Result: {filter_msg}\n"
|
| 84 |
except Exception as e:
|
| 85 |
debug_msg += f"Initial Filter Error: {str(e)}\n"
|
|
|
|
| 87 |
|
| 88 |
return labs, types, date_ranges, debug_msg, "All", "All", "All", device_cards, plot_daily, plot_uptime, anomaly_text
|
| 89 |
except Exception as e:
|
| 90 |
+
return ["All"], ["All"], ["All"], f"{debug_msg}Failed to process CSV: {str(e)}", "All", "All", "All", None, None, None, None
|
| 91 |
|
| 92 |
+
def filter_and_visualize(selected_lab, selected_type, selected_date_range):
|
| 93 |
global df
|
| 94 |
if df.empty:
|
| 95 |
return None, None, None, None, "No data available."
|
|
|
|
| 102 |
error_msg += f"Initial DataFrame: {len(filtered_df)} rows\n"
|
| 103 |
|
| 104 |
if selected_lab != "All":
|
| 105 |
+
filtered_df = filtered_df[filtered_df["Lab"] == selected_lab]
|
| 106 |
error_msg += f"After Lab filter ({selected_lab}): {len(filtered_df)} rows\n"
|
| 107 |
if selected_type != "All":
|
| 108 |
filtered_df = filtered_df[filtered_df["Type"] == selected_type]
|
|
|
|
| 112 |
start_date, end_date = selected_date_range.split(" to ")
|
| 113 |
start_date = pd.to_datetime(start_date)
|
| 114 |
end_date = pd.to_datetime(end_date) + timedelta(days=1) # Include end date
|
| 115 |
+
filtered_df = filtered_df[(filtered_df["Timestamp"] >= start_date) & (filtered_df["Timestamp"] < end_date)]
|
| 116 |
error_msg += f"After Date Range filter ({start_date} to {end_date}): {len(filtered_df)} rows\n"
|
| 117 |
except Exception as e:
|
| 118 |
error_msg += f"Error parsing date range: {str(e)}\n"
|
| 119 |
|
| 120 |
if filtered_df.empty:
|
| 121 |
+
return None, None, None, None, f"{error_msg}No data matches the selected filters."
|
| 122 |
|
| 123 |
# Debug: Log the filtered DataFrame
|
| 124 |
+
error_msg += f"Filtered DataFrame:\n{filtered_df.to_string()}\n"
|
| 125 |
|
| 126 |
# Device Cards (as a table)
|
| 127 |
+
device_cards = filtered_df[['DeviceID', 'Lab', 'Type', 'UsageCount', 'Timestamp']].sort_values(by='Timestamp', ascending=False)
|
| 128 |
|
| 129 |
+
# Daily Log Trends (Line Chart)
|
| 130 |
try:
|
| 131 |
if df['Timestamp'].isna().all():
|
| 132 |
error_msg += "Warning: All timestamps are invalid. Skipping Daily Log Trends.\n"
|
|
|
|
| 232 |
error_msg += f"Error generating Anomaly Alerts: {str(e)}\n"
|
| 233 |
anomaly_text = "Error generating anomaly alerts."
|
| 234 |
|
| 235 |
+
return device_cards, buf1, buf2, anomaly_text, f"{error_msg}Filters applied successfully."
|
| 236 |
|
| 237 |
def download_pdf(selected_lab, selected_type, selected_date_range):
|
| 238 |
global df
|
|
|
|
| 299 |
csv_input.change(
|
| 300 |
fn=upload_csv,
|
| 301 |
inputs=csv_input,
|
| 302 |
+
outputs=[lab_dropdown, type_dropdown, date_dropdown, error_box, lab_dropdown, type_dropdown, date_dropdown, device_cards, plot_daily, plot_uptime, anomaly_output]
|
| 303 |
)
|
| 304 |
|
| 305 |
submit_btn.click(
|
| 306 |
fn=filter_and_visualize,
|
| 307 |
inputs=[lab_dropdown, type_dropdown, date_dropdown],
|
| 308 |
+
outputs=[device_cards, plot_daily, plot_uptime, anomaly_output, error_box]
|
| 309 |
)
|
| 310 |
|
| 311 |
download_btn.click(
|