lavanya121 commited on
Commit
7f23852
·
verified ·
1 Parent(s): f2634f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -21
app.py CHANGED
@@ -1,3 +1,6 @@
 
 
 
1
  import gradio as gr
2
  import pandas as pd
3
  from datetime import datetime
@@ -206,7 +209,6 @@ async def process_logs(file_obj, progress=gr.Progress()):
206
  logging.error("Unsupported file format")
207
  return "Unsupported file format. Please upload a CSV file.", "", None, "", "", "", None
208
 
209
- progress(0.05, "Loading CSV file...")
210
  try:
211
  required_columns = ["device_id", "log_type", "status", "timestamp", "usage_hours", "downtime", "amc_date"]
212
  dtypes = {
@@ -218,17 +220,16 @@ async def process_logs(file_obj, progress=gr.Progress()):
218
  "amc_date": "string"
219
  }
220
  df = pd.read_csv(file_obj, dtype=dtypes)
221
- # Validate required columns
222
  missing_columns = [col for col in required_columns if col not in df.columns]
223
  if missing_columns:
224
- logging.error(f"Missing required columns: {missing_columns}")
225
  return f"Missing required columns: {missing_columns}", None, None, None, None, None, None
226
  logging.info(f"File loaded successfully with {len(df)} rows")
227
  except Exception as e:
228
  logging.error(f"Failed to load CSV: {str(e)}")
229
  return f"Failed to load CSV: {str(e)}", None, None, None, None, None, None
230
 
231
- progress(0.1, "Converting timestamps...")
232
  try:
233
  df["timestamp"] = pd.to_datetime(df["timestamp"], errors='coerce')
234
  except Exception as e:
@@ -237,47 +238,56 @@ async def process_logs(file_obj, progress=gr.Progress()):
237
 
238
  if df.empty:
239
  logging.warning("No data provided in the file")
240
- return "No data available in provided.", "No data to preview.", None, "No anomalies detected.", "No AMC reminders.", "No insights generated.", None
241
 
242
  # Step 1: Summary
243
- progress(0.2, "Generating summary...")
244
- summary = f"Step 1: Summary Report\n{summarize_logs(df, progress)}"
245
 
246
  # Step 2: Log Preview
247
- progress(0.3, "Previewing logs...")
248
  if not df.empty:
249
  preview_lines = ["Step 2: Log Preview (First 5 Rows)"]
250
- for idx, row in df.head().iterrows():
251
- 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']}")
252
- preview = "\ scorer_lines)
 
 
 
 
 
253
  else:
254
- preview.append("Step 2: Log Preview\nNo data available.")
255
 
256
  # Step 3: Usage Chart
257
- chart = create_usage_chart(df, progress)
 
258
 
259
  # Step 4: Anomaly Detection
260
- anomalies = f"Step 3: Anomaly Detection\n{detect_anomalies(df, progress)}"
 
261
 
262
  # Step 5: AMC Reminders
263
- amc_reminders = f"Step 4: AMC Reminders\n{check_amc_reminders(df, datetime.now(), progress)}"
 
264
 
265
  # Step 6: Insights
266
- insights = f"Step 5: Dashboard Insights (AI)\n{generate_dashboard_insights(df, progress)}"
 
267
 
268
  # Generate PDF if available
269
- progress(0.95, "Generating PDF report...")
270
  pdf_file = generate_pdf_content(summary, preview, anomalies, amc_reminders, insights) if reportlab_available else None
271
  if pdf_file is None and reportlab_available:
272
- logging.warning("PDF generation failed, but analysis completed")
273
  elif pdf_file is None:
274
- logging.info("PDF generation skipped due to missing reportlab")
275
 
276
- progress(1.0, "Processing complete!")
277
  return summary, preview, chart, anomalies, amc_reminders, insights, pdf_file
278
  except Exception as e:
279
  logging.error(f"Failed to process file: {str(e)}")
280
- return f"Error processing file: {error(e)}", None, None, None, None, None, None
281
 
282
  # Gradio Interface
283
  try:
 
1
+ """
2
+ LabOps Log Analyzer Dashboard with CSV file upload and optional PDF generation
3
+ """
4
  import gradio as gr
5
  import pandas as pd
6
  from datetime import datetime
 
209
  logging.error("Unsupported file format")
210
  return "Unsupported file format. Please upload a CSV file.", "", None, "", "", "", None
211
 
 
212
  try:
213
  required_columns = ["device_id", "log_type", "status", "timestamp", "usage_hours", "downtime", "amc_date"]
214
  dtypes = {
 
220
  "amc_date": "string"
221
  }
222
  df = pd.read_csv(file_obj, dtype=dtypes)
223
+ # Check for required columns
224
  missing_columns = [col for col in required_columns if col not in df.columns]
225
  if missing_columns:
226
+ logging.error(f"Missing columns: {missing_columns}")
227
  return f"Missing required columns: {missing_columns}", None, None, None, None, None, None
228
  logging.info(f"File loaded successfully with {len(df)} rows")
229
  except Exception as e:
230
  logging.error(f"Failed to load CSV: {str(e)}")
231
  return f"Failed to load CSV: {str(e)}", None, None, None, None, None, None
232
 
 
233
  try:
234
  df["timestamp"] = pd.to_datetime(df["timestamp"], errors='coerce')
235
  except Exception as e:
 
238
 
239
  if df.empty:
240
  logging.warning("No data provided in the file")
241
+ return "No data available in provided.", None, None, None, None, None, None
242
 
243
  # Step 1: Summary
244
+ progress(0.2, "Summary...")
245
+ summary = f"Step 1: Summary Report\n{summarize_logs(df)}"
246
 
247
  # Step 2: Log Preview
248
+ progress(0.3, "Previewing...")
249
  if not df.empty:
250
  preview_lines = ["Step 2: Log Preview (First 5 Rows)"]
251
+ for idx, row in df.head(5).iterrows():
252
+ preview_lines.append(
253
+ f"Row {idx + 1}: Device ID: {row['device_id']}, "
254
+ f"Log Type: {row['log_type']}, Status: {row['status']}, "
255
+ f"Timestamp: {row['timestamp']}, Usage Hours: {row['usage_hours']}, "
256
+ f"Downtime: {row['downtime']}, AMC Date: {row['amc_date']}"
257
+ )
258
+ preview = "\n".join(preview_lines)
259
  else:
260
+ preview = "Step 2: Log Preview\nNo data available."
261
 
262
  # Step 3: Usage Chart
263
+ progress(0.5, "Chart...")
264
+ chart = create_usage_chart(df)
265
 
266
  # Step 4: Anomaly Detection
267
+ progress(0.7, "Anomalies...")
268
+ anomalies = f"Anomaly Detection\n{detect_anomalies(df)}"
269
 
270
  # Step 5: AMC Reminders
271
+ progress(0.8, "AMC...")
272
+ amc_reminders = f"AMC Reminders\n{check_amc_reminders(df, datetime.now())}"
273
 
274
  # Step 6: Insights
275
+ progress(0.9, "Insights...")
276
+ insights = f"Dashboard Insights (AI)\n{generate_dashboard_insights(df)}"
277
 
278
  # Generate PDF if available
279
+ progress(0.95, "PDF...")
280
  pdf_file = generate_pdf_content(summary, preview, anomalies, amc_reminders, insights) if reportlab_available else None
281
  if pdf_file is None and reportlab_available:
282
+ logging.warning("PDF generation failed")
283
  elif pdf_file is None:
284
+ logging.info("PDF skipped: no reportlab")
285
 
286
+ progress(1.0, "Done!")
287
  return summary, preview, chart, anomalies, amc_reminders, insights, pdf_file
288
  except Exception as e:
289
  logging.error(f"Failed to process file: {str(e)}")
290
+ return f"Error processing file: {str(e)}", None, None, None, None, None, None
291
 
292
  # Gradio Interface
293
  try: