Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
from datetime import datetime
|
|
@@ -6,9 +9,17 @@ import plotly.express as px
|
|
| 6 |
from sklearn.ensemble import IsolationForest
|
| 7 |
from transformers import pipeline
|
| 8 |
import torch
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Configure logging for debugging
|
| 14 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
@@ -130,8 +141,11 @@ def create_usage_chart(df, progress=gr.Progress()):
|
|
| 130 |
logging.error(f"Failed to create usage chart: {str(e)}")
|
| 131 |
return None
|
| 132 |
|
| 133 |
-
# Generate PDF content using reportlab
|
| 134 |
def generate_pdf_content(summary, preview, anomalies, amc_reminders, insights):
|
|
|
|
|
|
|
|
|
|
| 135 |
try:
|
| 136 |
pdf_path = "analysis_report.pdf"
|
| 137 |
doc = SimpleDocTemplate(pdf_path, pagesize=letter)
|
|
@@ -195,7 +209,6 @@ async def process_logs(file_obj, progress=gr.Progress()):
|
|
| 195 |
logging.error("Unsupported file format")
|
| 196 |
return "Unsupported file format. Please upload a CSV file.", "", None, "", "", "", None
|
| 197 |
|
| 198 |
-
progress(0.05, "Loading CSV file...")
|
| 199 |
try:
|
| 200 |
required_columns = ["device_id", "log_type", "status", "timestamp", "usage_hours", "downtime", "amc_date"]
|
| 201 |
dtypes = {
|
|
@@ -207,17 +220,16 @@ async def process_logs(file_obj, progress=gr.Progress()):
|
|
| 207 |
"amc_date": "string"
|
| 208 |
}
|
| 209 |
df = pd.read_csv(file_obj, dtype=dtypes)
|
| 210 |
-
#
|
| 211 |
missing_columns = [col for col in required_columns if col not in df.columns]
|
| 212 |
if missing_columns:
|
| 213 |
-
logging.error(f"Missing
|
| 214 |
return f"Missing required columns: {missing_columns}", None, None, None, None, None, None
|
| 215 |
logging.info(f"File loaded successfully with {len(df)} rows")
|
| 216 |
except Exception as e:
|
| 217 |
logging.error(f"Failed to load CSV: {str(e)}")
|
| 218 |
return f"Failed to load CSV: {str(e)}", None, None, None, None, None, None
|
| 219 |
|
| 220 |
-
progress(0.1, "Converting timestamps...")
|
| 221 |
try:
|
| 222 |
df["timestamp"] = pd.to_datetime(df["timestamp"], errors='coerce')
|
| 223 |
except Exception as e:
|
|
@@ -226,46 +238,52 @@ async def process_logs(file_obj, progress=gr.Progress()):
|
|
| 226 |
|
| 227 |
if df.empty:
|
| 228 |
logging.warning("No data provided in the file")
|
| 229 |
-
return "No data available in provided.",
|
| 230 |
|
| 231 |
# Step 1: Summary
|
| 232 |
-
progress(0.2, "
|
| 233 |
-
summary = f"Step 1: Summary Report\n{summarize_logs(df
|
| 234 |
|
| 235 |
# Step 2: Log Preview
|
| 236 |
-
progress(0.3, "Previewing
|
| 237 |
if not df.empty:
|
| 238 |
preview_lines = ["Step 2: Log Preview (First 5 Rows)"]
|
| 239 |
-
for idx, row in df.head().iterrows():
|
| 240 |
-
preview_lines.append(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
preview = "\n".join(preview_lines)
|
| 242 |
else:
|
| 243 |
preview = "Step 2: Log Preview\nNo data available."
|
| 244 |
|
| 245 |
# Step 3: Usage Chart
|
| 246 |
-
progress(0.5, "
|
| 247 |
-
chart = create_usage_chart(df
|
| 248 |
|
| 249 |
# Step 4: Anomaly Detection
|
| 250 |
-
progress(0.7, "
|
| 251 |
-
anomalies = f"
|
| 252 |
|
| 253 |
# Step 5: AMC Reminders
|
| 254 |
-
progress(0.8, "
|
| 255 |
-
amc_reminders = f"
|
| 256 |
-
|
| 257 |
-
# Step 6:
|
| 258 |
-
progress(0.9, "
|
| 259 |
-
insights = f"
|
| 260 |
-
|
| 261 |
-
# Generate PDF
|
| 262 |
-
progress(0.95, "
|
| 263 |
-
pdf_file = generate_pdf_content(summary, preview, anomalies, amc_reminders, insights)
|
| 264 |
-
if pdf_file is None:
|
| 265 |
-
logging.
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
|
|
|
| 269 |
return summary, preview, chart, anomalies, amc_reminders, insights, pdf_file
|
| 270 |
except Exception as e:
|
| 271 |
logging.error(f"Failed to process file: {str(e)}")
|
|
@@ -273,11 +291,11 @@ async def process_logs(file_obj, progress=gr.Progress()):
|
|
| 273 |
|
| 274 |
# Gradio Interface
|
| 275 |
try:
|
| 276 |
-
logging.info("Initializing Gradio
|
| 277 |
with gr.Blocks(css="""
|
| 278 |
-
.dashboard-container {border: 1px solid #e0e0e0; padding: 10px; border-radius: 5px;
|
| 279 |
.dashboard-title {font-size: 24px; font-weight: bold; margin-bottom: 5px;}
|
| 280 |
-
.dashboard-section {margin-bottom:
|
| 281 |
.dashboard-section h3 {font-size: 18px; margin-bottom: 2px;}
|
| 282 |
.dashboard-section p {margin: 1px 0; line-height: 1.2;}
|
| 283 |
.dashboard-section ul {margin: 2px 0; padding-left: 20px;}
|
|
@@ -289,7 +307,7 @@ try:
|
|
| 289 |
with gr.Column(scale=1):
|
| 290 |
file_input = gr.File(label="Upload Logs (CSV)", file_types=[".csv"])
|
| 291 |
submit_button = gr.Button("Analyze", variant="primary")
|
| 292 |
-
|
| 293 |
with gr.Column(scale=2):
|
| 294 |
with gr.Group(elem_classes="dashboard-container"):
|
| 295 |
gr.Markdown("<div class='dashboard-title'>Analysis Results (Step-by-Step)</div>")
|
|
@@ -303,27 +321,27 @@ try:
|
|
| 303 |
with gr.Group(elem_classes="dashboard-section"):
|
| 304 |
gr.Markdown("### Step 2: Log Preview")
|
| 305 |
preview_output = gr.Markdown()
|
| 306 |
-
|
| 307 |
# Step 3: Usage Chart
|
| 308 |
with gr.Group(elem_classes="dashboard-section"):
|
| 309 |
gr.Markdown("### Step 3: Usage Chart")
|
| 310 |
chart_output = gr.Plot()
|
| 311 |
-
|
| 312 |
# Step 4: Anomaly Detection
|
| 313 |
with gr.Group(elem_classes="dashboard-section"):
|
| 314 |
gr.Markdown("### Step 4: Anomaly Detection")
|
| 315 |
anomaly_output = gr.Markdown()
|
| 316 |
-
|
| 317 |
# Step 5: AMC Reminders
|
| 318 |
with gr.Group(elem_classes="dashboard-section"):
|
| 319 |
gr.Markdown("### Step 5: AMC Reminders")
|
| 320 |
amc_output = gr.Markdown()
|
| 321 |
-
|
| 322 |
# Step 6: Dashboard Insights
|
| 323 |
with gr.Group(elem_classes="dashboard-section"):
|
| 324 |
-
gr.Markdown("### Step 6:
|
| 325 |
insights_output = gr.Markdown()
|
| 326 |
-
|
| 327 |
# PDF Download
|
| 328 |
with gr.Group(elem_classes="dashboard-section"):
|
| 329 |
gr.Markdown("### Download Report")
|
|
|
|
| 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
|
|
|
|
| 9 |
from sklearn.ensemble import IsolationForest
|
| 10 |
from transformers import pipeline
|
| 11 |
import torch
|
| 12 |
+
|
| 13 |
+
# Try to import reportlab for PDF generation
|
| 14 |
+
try:
|
| 15 |
+
from reportlab.lib.pagesizes import letter
|
| 16 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
| 17 |
+
from reportlab.lib.styles import getSampleStyleSheet
|
| 18 |
+
reportlab_available = True
|
| 19 |
+
logging.info("reportlab module successfully imported")
|
| 20 |
+
except ImportError:
|
| 21 |
+
logging.warning("reportlab module not found. PDF generation will be disabled. Install with: pip install reportlab")
|
| 22 |
+
reportlab_available = False
|
| 23 |
|
| 24 |
# Configure logging for debugging
|
| 25 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
| 141 |
logging.error(f"Failed to create usage chart: {str(e)}")
|
| 142 |
return None
|
| 143 |
|
| 144 |
+
# Generate PDF content using reportlab if available
|
| 145 |
def generate_pdf_content(summary, preview, anomalies, amc_reminders, insights):
|
| 146 |
+
if not reportlab_available:
|
| 147 |
+
logging.warning("Skipping PDF generation: reportlab not installed")
|
| 148 |
+
return None
|
| 149 |
try:
|
| 150 |
pdf_path = "analysis_report.pdf"
|
| 151 |
doc = SimpleDocTemplate(pdf_path, pagesize=letter)
|
|
|
|
| 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)}")
|
|
|
|
| 291 |
|
| 292 |
# Gradio Interface
|
| 293 |
try:
|
| 294 |
+
logging.info("Initializing Gradio interface...")
|
| 295 |
with gr.Blocks(css="""
|
| 296 |
+
.dashboard-container {border: 1px solid #e0e0e0; padding: 10px; border-radius: 5px;}
|
| 297 |
.dashboard-title {font-size: 24px; font-weight: bold; margin-bottom: 5px;}
|
| 298 |
+
.dashboard-section {margin-bottom: 20px;}
|
| 299 |
.dashboard-section h3 {font-size: 18px; margin-bottom: 2px;}
|
| 300 |
.dashboard-section p {margin: 1px 0; line-height: 1.2;}
|
| 301 |
.dashboard-section ul {margin: 2px 0; padding-left: 20px;}
|
|
|
|
| 307 |
with gr.Column(scale=1):
|
| 308 |
file_input = gr.File(label="Upload Logs (CSV)", file_types=[".csv"])
|
| 309 |
submit_button = gr.Button("Analyze", variant="primary")
|
| 310 |
+
|
| 311 |
with gr.Column(scale=2):
|
| 312 |
with gr.Group(elem_classes="dashboard-container"):
|
| 313 |
gr.Markdown("<div class='dashboard-title'>Analysis Results (Step-by-Step)</div>")
|
|
|
|
| 321 |
with gr.Group(elem_classes="dashboard-section"):
|
| 322 |
gr.Markdown("### Step 2: Log Preview")
|
| 323 |
preview_output = gr.Markdown()
|
| 324 |
+
|
| 325 |
# Step 3: Usage Chart
|
| 326 |
with gr.Group(elem_classes="dashboard-section"):
|
| 327 |
gr.Markdown("### Step 3: Usage Chart")
|
| 328 |
chart_output = gr.Plot()
|
| 329 |
+
|
| 330 |
# Step 4: Anomaly Detection
|
| 331 |
with gr.Group(elem_classes="dashboard-section"):
|
| 332 |
gr.Markdown("### Step 4: Anomaly Detection")
|
| 333 |
anomaly_output = gr.Markdown()
|
| 334 |
+
|
| 335 |
# Step 5: AMC Reminders
|
| 336 |
with gr.Group(elem_classes="dashboard-section"):
|
| 337 |
gr.Markdown("### Step 5: AMC Reminders")
|
| 338 |
amc_output = gr.Markdown()
|
| 339 |
+
|
| 340 |
# Step 6: Dashboard Insights
|
| 341 |
with gr.Group(elem_classes="dashboard-section"):
|
| 342 |
+
gr.Markdown("### Step 6: Insights (AI)")
|
| 343 |
insights_output = gr.Markdown()
|
| 344 |
+
|
| 345 |
# PDF Download
|
| 346 |
with gr.Group(elem_classes="dashboard-section"):
|
| 347 |
gr.Markdown("### Download Report")
|