Update app.py
Browse files
app.py
CHANGED
|
@@ -9,30 +9,34 @@ from reportlab.lib.pagesizes import letter
|
|
| 9 |
from reportlab.pdfgen import canvas
|
| 10 |
import tempfile
|
| 11 |
|
| 12 |
-
# Configure logging
|
| 13 |
-
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 14 |
|
| 15 |
def process_files(uploaded_files):
|
| 16 |
"""
|
| 17 |
Process uploaded CSV files, generate usage plots, detect anomalies, and process AMC expiries.
|
| 18 |
-
Returns a dataframe, plot, PDF path, and
|
| 19 |
"""
|
|
|
|
|
|
|
|
|
|
| 20 |
if not uploaded_files:
|
| 21 |
logging.warning("No files uploaded.")
|
| 22 |
return None, None, None, "Please upload at least one valid CSV file."
|
| 23 |
|
| 24 |
valid_files = [f for f in uploaded_files if f.name.endswith('.csv')]
|
|
|
|
|
|
|
| 25 |
if not valid_files:
|
| 26 |
logging.warning("No valid CSV files uploaded.")
|
| 27 |
return None, None, None, "Please upload at least one valid CSV file."
|
| 28 |
|
| 29 |
-
logging.info(
|
| 30 |
all_data = []
|
| 31 |
|
| 32 |
# Load and combine CSV files
|
| 33 |
for file in valid_files:
|
| 34 |
try:
|
| 35 |
-
logging.info(f"Loading logs from {file.name}")
|
| 36 |
df = pd.read_csv(file.name)
|
| 37 |
logging.info(f"Loaded {len(df)} records from {file.name}")
|
| 38 |
all_data.append(df)
|
|
@@ -46,25 +50,37 @@ def process_files(uploaded_files):
|
|
| 46 |
|
| 47 |
combined_df = pd.concat(all_data, ignore_index=True)
|
| 48 |
logging.info(f"Combined {len(combined_df)} total records.")
|
| 49 |
-
logging.info(f"
|
| 50 |
|
| 51 |
# Generate usage plot
|
|
|
|
| 52 |
plot_path = generate_usage_plot(combined_df)
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
# Detect anomalies
|
|
|
|
| 55 |
anomaly_df = detect_anomalies(combined_df)
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
# Process AMC expiries
|
|
|
|
| 58 |
amc_message, amc_df = process_amc_expiries(combined_df)
|
| 59 |
-
|
| 60 |
# Generate PDF report
|
| 61 |
pdf_path = generate_pdf_report(combined_df, anomaly_df, amc_df)
|
| 62 |
-
|
| 63 |
# Prepare output dataframe (combine original data with anomalies)
|
| 64 |
output_df = combined_df.copy()
|
| 65 |
if anomaly_df is not None:
|
| 66 |
output_df['anomaly'] = anomaly_df['anomaly']
|
| 67 |
-
|
| 68 |
return output_df, plot_path, pdf_path, amc_message
|
| 69 |
|
| 70 |
def generate_usage_plot(df):
|
|
@@ -72,7 +88,6 @@ def generate_usage_plot(df):
|
|
| 72 |
Generate a bar plot of usage_count by equipment and status.
|
| 73 |
Returns the path to the saved plot.
|
| 74 |
"""
|
| 75 |
-
logging.info("Generating usage plot...")
|
| 76 |
try:
|
| 77 |
plt.figure(figsize=(10, 6))
|
| 78 |
for status in df['status'].unique():
|
|
@@ -84,13 +99,12 @@ def generate_usage_plot(df):
|
|
| 84 |
plt.legend()
|
| 85 |
plt.xticks(rotation=45)
|
| 86 |
plt.tight_layout()
|
| 87 |
-
|
| 88 |
# Save plot to temporary file
|
| 89 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as tmp:
|
| 90 |
plt.savefig(tmp.name, format='png')
|
| 91 |
plot_path = tmp.name
|
| 92 |
plt.close()
|
| 93 |
-
logging.info("Usage plot generated successfully.")
|
| 94 |
return plot_path
|
| 95 |
except Exception as e:
|
| 96 |
logging.error(f"Failed to generate usage plot: {str(e)}")
|
|
@@ -101,13 +115,11 @@ def detect_anomalies(df):
|
|
| 101 |
Detect anomalies in usage_count using Isolation Forest.
|
| 102 |
Returns a dataframe with an 'anomaly' column (-1 for anomalies, 1 for normal).
|
| 103 |
"""
|
| 104 |
-
logging.info("Detecting anomalies...")
|
| 105 |
try:
|
| 106 |
model = IsolationForest(contamination=0.1, random_state=42)
|
| 107 |
anomalies = model.fit_predict(df[['usage_count']].values)
|
| 108 |
anomaly_df = df.copy()
|
| 109 |
anomaly_df['anomaly'] = anomalies
|
| 110 |
-
logging.info(f"Detected {sum(anomalies == -1)} anomalies.")
|
| 111 |
return anomaly_df
|
| 112 |
except Exception as e:
|
| 113 |
logging.error(f"Failed to detect anomalies: {str(e)}")
|
|
@@ -118,14 +130,13 @@ def process_amc_expiries(df):
|
|
| 118 |
Identify devices with AMC expiries within 7 days from 2025-06-05.
|
| 119 |
Returns a message and a dataframe of devices with upcoming expiries.
|
| 120 |
"""
|
| 121 |
-
logging.info("Processing AMC expiries...")
|
| 122 |
try:
|
| 123 |
current_date = datetime(2025, 6, 5)
|
| 124 |
threshold = current_date + timedelta(days=7)
|
| 125 |
df['amc_expiry'] = pd.to_datetime(df['amc_expiry'])
|
| 126 |
upcoming_expiries = df[df['amc_expiry'] <= threshold]
|
| 127 |
unique_devices = upcoming_expiries['equipment'].unique()
|
| 128 |
-
message = f"Found {len(unique_devices)} devices with upcoming AMC expiries
|
| 129 |
logging.info(message)
|
| 130 |
return message, upcoming_expiries
|
| 131 |
except Exception as e:
|
|
@@ -137,18 +148,21 @@ def generate_pdf_report(original_df, anomaly_df, amc_df):
|
|
| 137 |
Generate a PDF report with data summary, anomalies, and AMC expiries.
|
| 138 |
Returns the path to the saved PDF.
|
| 139 |
"""
|
| 140 |
-
logging.info("Generating PDF report...")
|
| 141 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as tmp:
|
| 143 |
c = canvas.Canvas(tmp.name, pagesize=letter)
|
| 144 |
c.drawString(100, 750, "Equipment Log Analysis Report")
|
| 145 |
y = 700
|
| 146 |
-
|
| 147 |
# Summary
|
| 148 |
c.drawString(100, y, f"Total Records: {len(original_df)}")
|
| 149 |
c.drawString(100, y-20, f"Devices: {', '.join(original_df['equipment'].unique())}")
|
| 150 |
y -= 40
|
| 151 |
-
|
| 152 |
# Anomalies
|
| 153 |
if anomaly_df is not None:
|
| 154 |
num_anomalies = sum(anomaly_df['anomaly'] == -1)
|
|
@@ -160,9 +174,9 @@ def generate_pdf_report(original_df, anomaly_df, amc_df):
|
|
| 160 |
else:
|
| 161 |
c.drawString(100, y, "Anomaly detection failed.")
|
| 162 |
y -= 20
|
| 163 |
-
|
| 164 |
# AMC Expiries
|
| 165 |
-
if amc_df is not None:
|
| 166 |
c.drawString(100, y, f"Devices with Upcoming AMC Expiries: {len(amc_df['equipment'].unique())}")
|
| 167 |
for _, row in amc_df.iterrows():
|
| 168 |
c.drawString(100, y-20, f"{row['equipment']}: {row['amc_expiry'].strftime('%Y-%m-%d')}")
|
|
@@ -170,12 +184,10 @@ def generate_pdf_report(original_df, anomaly_df, amc_df):
|
|
| 170 |
else:
|
| 171 |
c.drawString(100, y, "No AMC expiry data available.")
|
| 172 |
y -= 20
|
| 173 |
-
|
| 174 |
c.showPage()
|
| 175 |
c.save()
|
| 176 |
-
|
| 177 |
-
logging.info("PDF report generated successfully.")
|
| 178 |
-
return pdf_path
|
| 179 |
except Exception as e:
|
| 180 |
logging.error(f"Failed to generate PDF report: {str(e)}")
|
| 181 |
return None
|
|
@@ -192,7 +204,7 @@ with gr.Blocks() as demo:
|
|
| 192 |
with gr.Row():
|
| 193 |
output_message = gr.Textbox(label="AMC Expiry Status")
|
| 194 |
output_pdf = gr.File(label="Download PDF Report")
|
| 195 |
-
|
| 196 |
process_button.click(
|
| 197 |
fn=process_files,
|
| 198 |
inputs=[file_input],
|
|
@@ -200,4 +212,5 @@ with gr.Blocks() as demo:
|
|
| 200 |
)
|
| 201 |
|
| 202 |
if __name__ == "__main__":
|
|
|
|
| 203 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 9 |
from reportlab.pdfgen import canvas
|
| 10 |
import tempfile
|
| 11 |
|
| 12 |
+
# Configure logging to match the log format
|
| 13 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s,%(msecs)03d - %(levelname)s - %(message)s')
|
| 14 |
|
| 15 |
def process_files(uploaded_files):
|
| 16 |
"""
|
| 17 |
Process uploaded CSV files, generate usage plots, detect anomalies, and process AMC expiries.
|
| 18 |
+
Returns a dataframe, plot path, PDF path, and AMC expiry message.
|
| 19 |
"""
|
| 20 |
+
# Log received files
|
| 21 |
+
logging.info(f"Received uploaded files: {uploaded_files}")
|
| 22 |
+
|
| 23 |
if not uploaded_files:
|
| 24 |
logging.warning("No files uploaded.")
|
| 25 |
return None, None, None, "Please upload at least one valid CSV file."
|
| 26 |
|
| 27 |
valid_files = [f for f in uploaded_files if f.name.endswith('.csv')]
|
| 28 |
+
logging.info(f"Processing {len(valid_files)} valid files: {valid_files}")
|
| 29 |
+
|
| 30 |
if not valid_files:
|
| 31 |
logging.warning("No valid CSV files uploaded.")
|
| 32 |
return None, None, None, "Please upload at least one valid CSV file."
|
| 33 |
|
| 34 |
+
logging.info("Loading logs from uploaded files...")
|
| 35 |
all_data = []
|
| 36 |
|
| 37 |
# Load and combine CSV files
|
| 38 |
for file in valid_files:
|
| 39 |
try:
|
|
|
|
| 40 |
df = pd.read_csv(file.name)
|
| 41 |
logging.info(f"Loaded {len(df)} records from {file.name}")
|
| 42 |
all_data.append(df)
|
|
|
|
| 50 |
|
| 51 |
combined_df = pd.concat(all_data, ignore_index=True)
|
| 52 |
logging.info(f"Combined {len(combined_df)} total records.")
|
| 53 |
+
logging.info(f"Loaded {len(combined_df)} log records from uploaded files.")
|
| 54 |
|
| 55 |
# Generate usage plot
|
| 56 |
+
logging.info("Generating usage plot...")
|
| 57 |
plot_path = generate_usage_plot(combined_df)
|
| 58 |
+
if plot_path:
|
| 59 |
+
logging.info("Usage plot generated successfully.")
|
| 60 |
+
else:
|
| 61 |
+
logging.error("Failed to generate usage plot.")
|
| 62 |
+
return combined_df, None, None, "Failed to generate usage plot."
|
| 63 |
+
|
| 64 |
# Detect anomalies
|
| 65 |
+
logging.info("Detecting anomalies...")
|
| 66 |
anomaly_df = detect_anomalies(combined_df)
|
| 67 |
+
if anomaly_df is None:
|
| 68 |
+
logging.error("Failed to detect anomalies.")
|
| 69 |
+
else:
|
| 70 |
+
logging.info(f"Detected {sum(anomaly_df['anomaly'] == -1)} anomalies.")
|
| 71 |
+
|
| 72 |
# Process AMC expiries
|
| 73 |
+
logging.info("Processing AMC expiries...")
|
| 74 |
amc_message, amc_df = process_amc_expiries(combined_df)
|
| 75 |
+
|
| 76 |
# Generate PDF report
|
| 77 |
pdf_path = generate_pdf_report(combined_df, anomaly_df, amc_df)
|
| 78 |
+
|
| 79 |
# Prepare output dataframe (combine original data with anomalies)
|
| 80 |
output_df = combined_df.copy()
|
| 81 |
if anomaly_df is not None:
|
| 82 |
output_df['anomaly'] = anomaly_df['anomaly']
|
| 83 |
+
|
| 84 |
return output_df, plot_path, pdf_path, amc_message
|
| 85 |
|
| 86 |
def generate_usage_plot(df):
|
|
|
|
| 88 |
Generate a bar plot of usage_count by equipment and status.
|
| 89 |
Returns the path to the saved plot.
|
| 90 |
"""
|
|
|
|
| 91 |
try:
|
| 92 |
plt.figure(figsize=(10, 6))
|
| 93 |
for status in df['status'].unique():
|
|
|
|
| 99 |
plt.legend()
|
| 100 |
plt.xticks(rotation=45)
|
| 101 |
plt.tight_layout()
|
| 102 |
+
|
| 103 |
# Save plot to temporary file
|
| 104 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as tmp:
|
| 105 |
plt.savefig(tmp.name, format='png')
|
| 106 |
plot_path = tmp.name
|
| 107 |
plt.close()
|
|
|
|
| 108 |
return plot_path
|
| 109 |
except Exception as e:
|
| 110 |
logging.error(f"Failed to generate usage plot: {str(e)}")
|
|
|
|
| 115 |
Detect anomalies in usage_count using Isolation Forest.
|
| 116 |
Returns a dataframe with an 'anomaly' column (-1 for anomalies, 1 for normal).
|
| 117 |
"""
|
|
|
|
| 118 |
try:
|
| 119 |
model = IsolationForest(contamination=0.1, random_state=42)
|
| 120 |
anomalies = model.fit_predict(df[['usage_count']].values)
|
| 121 |
anomaly_df = df.copy()
|
| 122 |
anomaly_df['anomaly'] = anomalies
|
|
|
|
| 123 |
return anomaly_df
|
| 124 |
except Exception as e:
|
| 125 |
logging.error(f"Failed to detect anomalies: {str(e)}")
|
|
|
|
| 130 |
Identify devices with AMC expiries within 7 days from 2025-06-05.
|
| 131 |
Returns a message and a dataframe of devices with upcoming expiries.
|
| 132 |
"""
|
|
|
|
| 133 |
try:
|
| 134 |
current_date = datetime(2025, 6, 5)
|
| 135 |
threshold = current_date + timedelta(days=7)
|
| 136 |
df['amc_expiry'] = pd.to_datetime(df['amc_expiry'])
|
| 137 |
upcoming_expiries = df[df['amc_expiry'] <= threshold]
|
| 138 |
unique_devices = upcoming_expiries['equipment'].unique()
|
| 139 |
+
message = f"Found {len(unique_devices)} devices with upcoming AMC expiries."
|
| 140 |
logging.info(message)
|
| 141 |
return message, upcoming_expiries
|
| 142 |
except Exception as e:
|
|
|
|
| 148 |
Generate a PDF report with data summary, anomalies, and AMC expiries.
|
| 149 |
Returns the path to the saved PDF.
|
| 150 |
"""
|
|
|
|
| 151 |
try:
|
| 152 |
+
if original_df is None:
|
| 153 |
+
logging.warning("No data available for PDF generation.")
|
| 154 |
+
return None
|
| 155 |
+
|
| 156 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as tmp:
|
| 157 |
c = canvas.Canvas(tmp.name, pagesize=letter)
|
| 158 |
c.drawString(100, 750, "Equipment Log Analysis Report")
|
| 159 |
y = 700
|
| 160 |
+
|
| 161 |
# Summary
|
| 162 |
c.drawString(100, y, f"Total Records: {len(original_df)}")
|
| 163 |
c.drawString(100, y-20, f"Devices: {', '.join(original_df['equipment'].unique())}")
|
| 164 |
y -= 40
|
| 165 |
+
|
| 166 |
# Anomalies
|
| 167 |
if anomaly_df is not None:
|
| 168 |
num_anomalies = sum(anomaly_df['anomaly'] == -1)
|
|
|
|
| 174 |
else:
|
| 175 |
c.drawString(100, y, "Anomaly detection failed.")
|
| 176 |
y -= 20
|
| 177 |
+
|
| 178 |
# AMC Expiries
|
| 179 |
+
if amc_df is not None and not amc_df.empty:
|
| 180 |
c.drawString(100, y, f"Devices with Upcoming AMC Expiries: {len(amc_df['equipment'].unique())}")
|
| 181 |
for _, row in amc_df.iterrows():
|
| 182 |
c.drawString(100, y-20, f"{row['equipment']}: {row['amc_expiry'].strftime('%Y-%m-%d')}")
|
|
|
|
| 184 |
else:
|
| 185 |
c.drawString(100, y, "No AMC expiry data available.")
|
| 186 |
y -= 20
|
| 187 |
+
|
| 188 |
c.showPage()
|
| 189 |
c.save()
|
| 190 |
+
return tmp.name
|
|
|
|
|
|
|
| 191 |
except Exception as e:
|
| 192 |
logging.error(f"Failed to generate PDF report: {str(e)}")
|
| 193 |
return None
|
|
|
|
| 204 |
with gr.Row():
|
| 205 |
output_message = gr.Textbox(label="AMC Expiry Status")
|
| 206 |
output_pdf = gr.File(label="Download PDF Report")
|
| 207 |
+
|
| 208 |
process_button.click(
|
| 209 |
fn=process_files,
|
| 210 |
inputs=[file_input],
|
|
|
|
| 212 |
)
|
| 213 |
|
| 214 |
if __name__ == "__main__":
|
| 215 |
+
logging.info("Application starting...")
|
| 216 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|