lavanya121 commited on
Commit
1472f2c
·
verified ·
1 Parent(s): 699fb49

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -66
app.py CHANGED
@@ -6,6 +6,11 @@ import plotly.express as px
6
  from sklearn.ensemble import IsolationForest
7
  from transformers import pipeline
8
  import torch
 
 
 
 
 
9
 
10
  # Configure logging for debugging
11
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
@@ -32,7 +37,7 @@ def summarize_logs(df, progress=gr.Progress()):
32
  return summary
33
  except Exception as e:
34
  logging.error(f"Summary generation failed: {str(e)}")
35
- return "Failed to generate summary."
36
 
37
  # Anomaly Detection using Isolation Forest
38
  def detect_anomalies(df, progress=gr.Progress()):
@@ -50,7 +55,7 @@ def detect_anomalies(df, progress=gr.Progress()):
50
  anomalies = df[df["anomaly"] == -1][["device_id", "usage_hours", "downtime", "timestamp"]]
51
  if anomalies.empty:
52
  return "No anomalies detected."
53
- anomaly_lines = ["**Detected Anomalies:**"]
54
  for idx, row in anomalies.head(5).iterrows():
55
  anomaly_lines.append(f"- Device ID: {row['device_id']}, Usage Hours: {row['usage_hours']}, Downtime: {row['downtime']}, Timestamp: {row['timestamp']}")
56
  anomaly_list = "\n".join(anomaly_lines)
@@ -73,7 +78,7 @@ def check_amc_reminders(df, current_date, progress=gr.Progress()):
73
  reminders = df[(df["days_to_amc"] >= 0) & (df["days_to_amc"] <= 30)][["device_id", "amc_date"]]
74
  if reminders.empty:
75
  return "No AMC reminders due within the next 30 days."
76
- reminder_lines = ["**Upcoming AMC Reminders:**"]
77
  for idx, row in reminders.head(5).iterrows():
78
  reminder_lines.append(f"- Device ID: {row['device_id']}, AMC Date: {row['amc_date']}")
79
  reminder_list = "\n".join(reminder_lines)
@@ -127,51 +132,55 @@ def create_usage_chart(df, progress=gr.Progress()):
127
  logging.error(f"Failed to create usage chart: {str(e)}")
128
  return None
129
 
130
- # Generate LaTeX PDF content
131
  def generate_pdf_content(summary, preview, anomalies, amc_reminders, insights):
132
- latex_content = r"""
133
- \documentclass[a4paper,11pt]{article}
134
- \usepackage[utf8]{inputenc}
135
- \usepackage[T1]{fontenc}
136
- \usepackage{lmodern}
137
- \usepackage{geometry}
138
- \geometry{margin=1in}
139
- \usepackage{enumitem}
140
- \usepackage{parskip}
141
- \setlength{\parskip}{0.5em}
142
- \setlength{\parindent}{0em}
143
- \usepackage{titling}
144
- \usepackage{xcolor彼此
145
-
146
- System: color}
147
- \usepackage{noto}
148
-
149
- \title{LabOps Log Analysis Report}
150
- \author{AI-Powered Analysis}
151
- \date{\today}
152
-
153
- \begin{document}
154
-
155
- \maketitle
156
-
157
- \section*{Summary Report}
158
- """ + summary.replace("**", r"\textbf{").replace(":", r":}").replace("\n", r"\\") + r"""
159
-
160
- \section*{Log Preview}
161
- """ + preview.replace("**", r"\textbf{").replace(":", r":}").replace("\n", r"\\") + r"""
162
-
163
- \section*{Anomaly Detection}
164
- """ + anomalies.replace("**", r"\textbf{").replace(":", r":}").replace("\n", r"\\") + r"""
165
-
166
- \section*{AMC Reminders}
167
- """ + amc_reminders.replace("**", r"\textbf{").replace(":", r":}").replace("\n", r"\\") + r"""
168
-
169
- \section*{Dashboard Insights}
170
- """ + insights.replace("**", r"\textbf{").replace(":", r":}").replace("\n", r"\\") + r"""
171
-
172
- \end{document}
173
- """
174
- return latex_content
 
 
 
 
175
 
176
  # Main Gradio function
177
  async def process_logs(file_obj, progress=gr.Progress()):
@@ -190,7 +199,7 @@ async def process_logs(file_obj, progress=gr.Progress()):
190
 
191
  progress(0.05, "Loading CSV file...")
192
  try:
193
- usecols = ["device_id", "log_type", "status", "timestamp", "usage_hours", "downtime", "amc_date"]
194
  dtypes = {
195
  "device_id": "string",
196
  "log_type": "string",
@@ -199,7 +208,12 @@ async def process_logs(file_obj, progress=gr.Progress()):
199
  "downtime": "float32",
200
  "amc_date": "string"
201
  }
202
- df = pd.read_csv(file_name, usecols=usecols, dtype=dtypes)
 
 
 
 
 
203
  logging.info(f"File loaded successfully with {len(df)} rows")
204
  except Exception as e:
205
  logging.error(f"Failed to load CSV: {str(e)}")
@@ -218,39 +232,33 @@ async def process_logs(file_obj, progress=gr.Progress()):
218
 
219
  # Step 1: Summary Report
220
  progress(0.2, "Generating summary...")
221
- summary = f"**Step 1: Summary Report** \n{summarize_logs(df, progress)}"
222
 
223
  # Step 2: Log Preview
224
  progress(0.3, "Previewing logs...")
225
  if not df.empty:
226
- preview_lines = ["**Step 2: Log Preview (First 5 Rows)**"]
227
  for idx, row in df.head().iterrows():
228
- preview_lines.append(f"**Row {idx + 1}:** Device ID: {row['device_id']}, Log Type: {row['log_type']}, Status: {row['status']}, Timestamp: {row['timestamp']}, Usage Hours: {row['usage_hours']}, Downtime: {row['downtime']}, AMC Date: {row['amc_date']}")
229
  preview = "\n".join(preview_lines)
230
  else:
231
- preview = "**Step 2: Log Preview** \nNo data available."
232
 
233
  # Step 3: Usage Chart
234
  chart = create_usage_chart(df, progress)
235
 
236
  # Step 4: Anomaly Detection
237
- anomalies = f"**Step 3: Anomaly Detection** \n{detect_anomalies(df, progress)}"
238
 
239
  # Step 5: AMC Reminders
240
- amc_reminders = f"**Step 4: AMC Reminders** \n{check_amc_reminders(df, datetime.now(), progress)}"
241
 
242
  # Step 6: Dashboard Insights
243
- insights = f"**Step 5: Dashboard Insights (AI)** \n{generate_dashboard_insights(df, progress)}"
244
 
245
  # Generate PDF
246
  progress(0.95, "Generating PDF report...")
247
- try:
248
- latex_content = generate_pdf_content(summary, preview, anomalies, amc_reminders, insights)
249
- pdf_file = gr.File(value=latex_content, file_types=[".pdf"], label="Download Analysis Report")
250
- logging.info("PDF content generated successfully")
251
- except Exception as e:
252
- logging.error(f"Failed to generate PDF: {str(e)}")
253
- pdf_file = None
254
 
255
  progress(1.0, "Processing complete!")
256
  return summary, preview, chart, anomalies, amc_reminders, insights, pdf_file
@@ -267,7 +275,6 @@ try:
267
  .dashboard-section {margin-bottom: 5px;}
268
  .dashboard-section h3 {font-size: 18px; margin-bottom: 2px;}
269
  .dashboard-section p {margin: 1px 0; line-height: 1.2;}
270
- .dashboard-section li {margin: 1px 0; line-height: 1.2;}
271
  .dashboard-section ul {margin: 2px 0; padding-left: 20px;}
272
  """) as iface:
273
  gr.Markdown("<h1>LabOps Log Analyzer Dashboard (Hugging Face AI)</h1>")
@@ -308,8 +315,7 @@ try:
308
  amc_output = gr.Markdown()
309
 
310
  # Step 6: Dashboard Insights
311
- with gr.Group(elem_classes="dashboard-section"):
312
- gr.Markdown("### Step 6: Dashboard Insights (AI)")
313
  insights_output = gr.Markdown()
314
 
315
  # PDF Download
 
6
  from sklearn.ensemble import IsolationForest
7
  from transformers import pipeline
8
  import torch
9
+ from reportlab.lib.pagesizes import letter
10
+ from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
11
+ from reportlab.lib.styles import getSampleStyleSheet
12
+ import os
13
+ import io
14
 
15
  # Configure logging for debugging
16
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
 
37
  return summary
38
  except Exception as e:
39
  logging.error(f"Summary generation failed: {str(e)}")
40
+ return f"Failed to generate summary: {str(e)}"
41
 
42
  # Anomaly Detection using Isolation Forest
43
  def detect_anomalies(df, progress=gr.Progress()):
 
55
  anomalies = df[df["anomaly"] == -1][["device_id", "usage_hours", "downtime", "timestamp"]]
56
  if anomalies.empty:
57
  return "No anomalies detected."
58
+ anomaly_lines = ["Detected Anomalies:"]
59
  for idx, row in anomalies.head(5).iterrows():
60
  anomaly_lines.append(f"- Device ID: {row['device_id']}, Usage Hours: {row['usage_hours']}, Downtime: {row['downtime']}, Timestamp: {row['timestamp']}")
61
  anomaly_list = "\n".join(anomaly_lines)
 
78
  reminders = df[(df["days_to_amc"] >= 0) & (df["days_to_amc"] <= 30)][["device_id", "amc_date"]]
79
  if reminders.empty:
80
  return "No AMC reminders due within the next 30 days."
81
+ reminder_lines = ["Upcoming AMC Reminders:"]
82
  for idx, row in reminders.head(5).iterrows():
83
  reminder_lines.append(f"- Device ID: {row['device_id']}, AMC Date: {row['amc_date']}")
84
  reminder_list = "\n".join(reminder_lines)
 
132
  logging.error(f"Failed to create usage chart: {str(e)}")
133
  return None
134
 
135
+ # Generate PDF content using reportlab
136
  def generate_pdf_content(summary, preview, anomalies, amc_reminders, insights):
137
+ try:
138
+ pdf_path = "analysis_report.pdf"
139
+ doc = SimpleDocTemplate(pdf_path, pagesize=letter)
140
+ styles = getSampleStyleSheet()
141
+ story = []
142
+
143
+ # Title
144
+ story.append(Paragraph("LabOps Log Analysis Report", styles['Title']))
145
+ story.append(Paragraph(f"Generated on {datetime.now().strftime('%Y-%m-%d')}", styles['Normal']))
146
+ story.append(Spacer(1, 12))
147
+
148
+ # Summary Report
149
+ story.append(Paragraph("Summary Report", styles['Heading2']))
150
+ for line in summary.split('\n'):
151
+ story.append(Paragraph(line.replace('**', ''), styles['Normal']))
152
+ story.append(Spacer(1, 12))
153
+
154
+ # Log Preview
155
+ story.append(Paragraph("Log Preview", styles['Heading2']))
156
+ for line in preview.split('\n'):
157
+ story.append(Paragraph(line.replace('**', ''), styles['Normal']))
158
+ story.append(Spacer(1, 12))
159
+
160
+ # Anomaly Detection
161
+ story.append(Paragraph("Anomaly Detection", styles['Heading2']))
162
+ for line in anomalies.split('\n'):
163
+ story.append(Paragraph(line.replace('**', ''), styles['Normal']))
164
+ story.append(Spacer(1, 12))
165
+
166
+ # AMC Reminders
167
+ story.append(Paragraph("AMC Reminders", styles['Heading2']))
168
+ for line in amc_reminders.split('\n'):
169
+ story.append(Paragraph(line.replace('**', ''), styles['Normal']))
170
+ story.append(Spacer(1, 12))
171
+
172
+ # Dashboard Insights
173
+ story.append(Paragraph("Dashboard Insights", styles['Heading2']))
174
+ for line in insights.split('\n'):
175
+ story.append(Paragraph(line.replace('**', ''), styles['Normal']))
176
+
177
+ # Build PDF
178
+ doc.build(story)
179
+ logging.info(f"PDF generated successfully at {pdf_path}")
180
+ return pdf_path
181
+ except Exception as e:
182
+ logging.error(f"Failed to generate PDF: {str(e)}")
183
+ return None
184
 
185
  # Main Gradio function
186
  async def process_logs(file_obj, progress=gr.Progress()):
 
199
 
200
  progress(0.05, "Loading CSV file...")
201
  try:
202
+ required_columns = ["device_id", "log_type", "status", "timestamp", "usage_hours", "downtime", "amc_date"]
203
  dtypes = {
204
  "device_id": "string",
205
  "log_type": "string",
 
208
  "downtime": "float32",
209
  "amc_date": "string"
210
  }
211
+ df = pd.read_csv(file_name, dtype=dtypes)
212
+ # Validate required columns
213
+ missing_columns = [col for col in required_columns if col not in df.columns]
214
+ if missing_columns:
215
+ logging.error(f"Missing required columns: {missing_columns}")
216
+ return f"Missing required columns: {missing_columns}", None, None, None, None, None, None
217
  logging.info(f"File loaded successfully with {len(df)} rows")
218
  except Exception as e:
219
  logging.error(f"Failed to load CSV: {str(e)}")
 
232
 
233
  # Step 1: Summary Report
234
  progress(0.2, "Generating summary...")
235
+ summary = f"Step 1: Summary Report\n{summarize_logs(df, progress)}"
236
 
237
  # Step 2: Log Preview
238
  progress(0.3, "Previewing logs...")
239
  if not df.empty:
240
+ preview_lines = ["Step 2: Log Preview (First 5 Rows)"]
241
  for idx, row in df.head().iterrows():
242
+ preview_lines.append(f"Row {idx + 1}: Device ID: {row['device_id']}, Log Type: {row['log_type']}, Status: {row['status']}, Timestamp: {row['timestamp']}, Usage Hours: {row['usage_hours']}, Downtime: {row['downtime']}, AMC Date: {row['amc_date']}")
243
  preview = "\n".join(preview_lines)
244
  else:
245
+ preview = "Step 2: Log Preview\nNo data available."
246
 
247
  # Step 3: Usage Chart
248
  chart = create_usage_chart(df, progress)
249
 
250
  # Step 4: Anomaly Detection
251
+ anomalies = f"Step 3: Anomaly Detection\n{detect_anomalies(df, progress)}"
252
 
253
  # Step 5: AMC Reminders
254
+ amc_reminders = f"Step 4: AMC Reminders\n{check_amc_reminders(df, datetime.now(), progress)}"
255
 
256
  # Step 6: Dashboard Insights
257
+ insights = f"Step 5: Dashboard Insights (AI)\n{generate_dashboard_insights(df, progress)}"
258
 
259
  # Generate PDF
260
  progress(0.95, "Generating PDF report...")
261
+ pdf_file = generate_pdf_content(summary, preview, anomalies, amc_reminders, insights)
 
 
 
 
 
 
262
 
263
  progress(1.0, "Processing complete!")
264
  return summary, preview, chart, anomalies, amc_reminders, insights, pdf_file
 
275
  .dashboard-section {margin-bottom: 5px;}
276
  .dashboard-section h3 {font-size: 18px; margin-bottom: 2px;}
277
  .dashboard-section p {margin: 1px 0; line-height: 1.2;}
 
278
  .dashboard-section ul {margin: 2px 0; padding-left: 20px;}
279
  """) as iface:
280
  gr.Markdown("<h1>LabOps Log Analyzer Dashboard (Hugging Face AI)</h1>")
 
315
  amc_output = gr.Markdown()
316
 
317
  # Step 6: Dashboard Insights
318
+ MD("### Step 6: Dashboard Insights (AI)")
 
319
  insights_output = gr.Markdown()
320
 
321
  # PDF Download