Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
"""
|
| 2 |
-
LabOps Log Analyzer Dashboard with
|
| 3 |
"""
|
| 4 |
import gradio as gr
|
| 5 |
import pandas as pd
|
|
@@ -10,18 +10,20 @@ from sklearn.ensemble import IsolationForest
|
|
| 10 |
from transformers import pipeline
|
| 11 |
import torch
|
| 12 |
|
| 13 |
-
# Try to
|
| 14 |
try:
|
| 15 |
-
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 16 |
from reportlab.lib.pagesizes import letter
|
| 17 |
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
| 18 |
from reportlab.lib.styles import getSampleStyleSheet
|
| 19 |
reportlab_available = True
|
| 20 |
logging.info("reportlab module successfully imported")
|
| 21 |
-
except ImportError
|
| 22 |
logging.warning("reportlab module not found. PDF generation will be disabled. Install with: pip install reportlab")
|
| 23 |
reportlab_available = False
|
| 24 |
|
|
|
|
|
|
|
|
|
|
| 25 |
# Preload Hugging Face summarization model at startup
|
| 26 |
logging.info("Preloading Hugging Face model...")
|
| 27 |
try:
|
|
@@ -225,7 +227,7 @@ async def process_logs(file_obj, progress=gr.Progress()):
|
|
| 225 |
logging.error(f"Missing required columns: {missing_columns}")
|
| 226 |
return f"Missing required columns: {missing_columns}", None, None, None, None, None, None
|
| 227 |
logging.info(f"File loaded successfully with {len(df)} rows")
|
| 228 |
-
|
| 229 |
logging.error(f"Failed to load CSV: {str(e)}")
|
| 230 |
return f"Failed to load CSV: {str(e)}", None, None, None, None, None, None
|
| 231 |
|
|
@@ -233,12 +235,12 @@ async def process_logs(file_obj, progress=gr.Progress()):
|
|
| 233 |
try:
|
| 234 |
df["timestamp"] = pd.to_datetime(df["timestamp"], errors='coerce')
|
| 235 |
except Exception as e:
|
| 236 |
-
logging.error(f"
|
| 237 |
-
return f"
|
| 238 |
|
| 239 |
if df.empty:
|
| 240 |
logging.warning("No data provided in the file")
|
| 241 |
-
return "No data available in provided.", "No data to preview.", None, "No anomalies detected.", "No AMC reminders.", "No insights generated.", None
|
| 242 |
|
| 243 |
# Step 1: Summary
|
| 244 |
progress(0.2, "Generating summary...")
|
|
@@ -248,8 +250,8 @@ async def process_logs(file_obj, progress=gr.Progress()):
|
|
| 248 |
progress(0.3, "Previewing logs...")
|
| 249 |
if not df.empty:
|
| 250 |
preview_lines = ["Step 2: Log Preview (First 5 Rows)"]
|
| 251 |
-
for idx, row in
|
| 252 |
-
preview_lines.append(f"Row {idx}: 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']}")
|
| 253 |
preview = "\n".join(preview_lines)
|
| 254 |
else:
|
| 255 |
preview = "Step 2: Log Preview\nNo data available."
|
|
@@ -357,4 +359,96 @@ if __name__ == "__main__":
|
|
| 357 |
except Exception as e:
|
| 358 |
logging.error(f"Failed to launch Gradio interface: {str(e)}")
|
| 359 |
print(f"Error launching app: {str(e)}")
|
| 360 |
-
raise e
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
|
|
|
| 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')
|
| 26 |
+
|
| 27 |
# Preload Hugging Face summarization model at startup
|
| 28 |
logging.info("Preloading Hugging Face model...")
|
| 29 |
try:
|
|
|
|
| 227 |
logging.error(f"Missing required columns: {missing_columns}")
|
| 228 |
return f"Missing required columns: {missing_columns}", None, None, None, None, None, None
|
| 229 |
logging.info(f"File loaded successfully with {len(df)} rows")
|
| 230 |
+
except Exception as e:
|
| 231 |
logging.error(f"Failed to load CSV: {str(e)}")
|
| 232 |
return f"Failed to load CSV: {str(e)}", None, None, None, None, None, None
|
| 233 |
|
|
|
|
| 235 |
try:
|
| 236 |
df["timestamp"] = pd.to_datetime(df["timestamp"], errors='coerce')
|
| 237 |
except Exception as e:
|
| 238 |
+
logging.error(f"Date conversion failed: {str(e)}")
|
| 239 |
+
return f"Failed to convert timestamp to datetime: {str(e)}", None, None, None, None, None, None
|
| 240 |
|
| 241 |
if df.empty:
|
| 242 |
logging.warning("No data provided in the file")
|
| 243 |
+
return "No data available in provided.", "No data to preview.", None, "No anomalies detected.", "No AMC reminders.", "No insights generated.", None
|
| 244 |
|
| 245 |
# Step 1: Summary
|
| 246 |
progress(0.2, "Generating summary...")
|
|
|
|
| 250 |
progress(0.3, "Previewing logs...")
|
| 251 |
if not df.empty:
|
| 252 |
preview_lines = ["Step 2: Log Preview (First 5 Rows)"]
|
| 253 |
+
for idx, row in df.head().iterrows():
|
| 254 |
+
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']}")
|
| 255 |
preview = "\n".join(preview_lines)
|
| 256 |
else:
|
| 257 |
preview = "Step 2: Log Preview\nNo data available."
|
|
|
|
| 359 |
except Exception as e:
|
| 360 |
logging.error(f"Failed to launch Gradio interface: {str(e)}")
|
| 361 |
print(f"Error launching app: {str(e)}")
|
| 362 |
+
raise e
|
| 363 |
+
</xArtifact>
|
| 364 |
+
|
| 365 |
+
### Key Changes Made
|
| 366 |
+
1. **Fixed Syntax Error**:
|
| 367 |
+
- Removed the invalid leading double quote (`"`) before `except Exception as e:` in the CSV loading section (around line 228).
|
| 368 |
+
- Corrected the syntax to ensure proper error handling:
|
| 369 |
+
```python
|
| 370 |
+
except Exception as e:
|
| 371 |
+
logging.error(f"Failed to load CSV: {str(e)}")
|
| 372 |
+
return f"Failed to load CSV: {str(e)}", None, None, None, None, None, None
|
| 373 |
+
```
|
| 374 |
+
- Fixed another syntax error in the timestamp conversion error message, replacing `error(e)` with `str(e)`.
|
| 375 |
+
|
| 376 |
+
2. **Preserved Functionality**:
|
| 377 |
+
- Retained all analysis functions (`summarize_logs`, `detect_anomalies`, `check_amc_reminders`, `generate_dashboard_insights`, `create_usage_chart`) unchanged.
|
| 378 |
+
- Kept the optional `reportlab` PDF generation with graceful fallback if `reportlab` is not installed.
|
| 379 |
+
- Maintained the Gradio interface layout and CSS styling.
|
| 380 |
+
- Ensured CSV file upload functionality without a row limit.
|
| 381 |
+
|
| 382 |
+
3. **Error Handling**:
|
| 383 |
+
- Fixed syntax errors in error messages (e.g., corrected `error(e)` to `str(e)`).
|
| 384 |
+
- Kept robust logging to diagnose issues, including CSV loading, timestamp conversion, and PDF generation.
|
| 385 |
+
|
| 386 |
+
4. **CSV Validation**:
|
| 387 |
+
- Retained validation for required columns (`device_id`, `log_type`, `status`, `timestamp`, `usage_hours`, `downtime`, `amc_date`).
|
| 388 |
+
- Corrected the log preview loop to use `df.head().iterrows()` consistently.
|
| 389 |
+
|
| 390 |
+
### Prerequisites
|
| 391 |
+
- Install required packages:
|
| 392 |
+
```bash
|
| 393 |
+
pip install gradio pandas plotly scikit-learn transformers torch
|
| 394 |
+
```
|
| 395 |
+
- For PDF generation (optional but recommended):
|
| 396 |
+
```bash
|
| 397 |
+
pip install reportlab
|
| 398 |
+
```
|
| 399 |
+
- No external tools (e.g., `wkhtmltopdf` or LaTeX) are required unless `reportlab` is installed.
|
| 400 |
+
|
| 401 |
+
### CSV File Requirements
|
| 402 |
+
- The CSV file must contain the columns: `device_id`, `log_type`, `status`, `timestamp`, `usage_hours`, `downtime`, and `amc_date`.
|
| 403 |
+
- Example CSV structure:
|
| 404 |
+
```csv
|
| 405 |
+
device_id,log_type,status,timestamp,usage_hours,downtime,amc_date
|
| 406 |
+
DEV001,Maintenance,Active,2025-06-01T10:00:00,5.5,0.2,2025-07-01
|
| 407 |
+
DEV002,Operation,Inactive,2025-06-02T12:00:00,3.0,1.5,2025-06-15
|
| 408 |
+
```
|
| 409 |
+
|
| 410 |
+
### How to Use
|
| 411 |
+
1. Install the required Python packages listed above. Install `reportlab` if you want PDF generation.
|
| 412 |
+
2. Save the code as `app.py` and run it:
|
| 413 |
+
```bash
|
| 414 |
+
python app.py
|
| 415 |
+
```
|
| 416 |
+
3. Open the Gradio interface in your browser (default: `http://0.0.0.0:7860`).
|
| 417 |
+
4. Upload a CSV file with the required columns and click "Analyze".
|
| 418 |
+
5. View the analysis results in the dashboard (summary, preview, chart, anomalies, AMC reminders, insights).
|
| 419 |
+
6. If `reportlab` is installed, download the PDF report from the "Download Report" section. If not, the PDF section will be empty, but other outputs will display.
|
| 420 |
+
|
| 421 |
+
### Troubleshooting
|
| 422 |
+
- **SyntaxError: unterminated string literal**:
|
| 423 |
+
- This error should now be resolved. If you encounter another syntax error, check for stray quotes or indentation issues in `app.py`. Ensure your editor uses 4 spaces for indentation and no mixed tabs/spaces.
|
| 424 |
+
|
| 425 |
+
- **ModuleNotFoundError: No module named 'reportlab'**:
|
| 426 |
+
- If you see a warning like `reportlab module not found. PDF generation will be disabled`, install `reportlab` using `pip install reportlab` or ignore it if PDF output is not needed.
|
| 427 |
+
|
| 428 |
+
- **CSV Parsing Error**:
|
| 429 |
+
- Ensure the CSV file has all required columns and valid data (e.g., `usage_hours` and `downtime` as numbers, `timestamp` and `amc_date` in a recognizable date format like `YYYY-MM-DD` or `YYYY-MM-DDTHH:MM:SS`).
|
| 430 |
+
- Check the terminal for errors like `Missing required columns` or `Failed to load CSV`.
|
| 431 |
+
|
| 432 |
+
- **Gradio Interface Issues**:
|
| 433 |
+
- Ensure port 7860 is available and you’re accessing the correct URL.
|
| 434 |
+
- Check the terminal for errors like `Failed to initialize Gradio interface`.
|
| 435 |
+
|
| 436 |
+
- **Other Errors**:
|
| 437 |
+
- If the interface loads but no output appears, check the terminal for logged errors (e.g., `Failed to process file`).
|
| 438 |
+
- Share the specific error message or terminal output for further assistance.
|
| 439 |
+
|
| 440 |
+
- **Dependency Issues**:
|
| 441 |
+
- Verify all packages are installed using `pip list`. If you encounter version conflicts, use a virtual environment:
|
| 442 |
+
```bash
|
| 443 |
+
python -m venv env
|
| 444 |
+
source env/bin/activate # On Windows: env\Scripts\activate
|
| 445 |
+
pip install gradio pandas plotly scikit-learn transformers torch reportlab
|
| 446 |
+
```
|
| 447 |
+
|
| 448 |
+
### Notes
|
| 449 |
+
- **PDF Generation**: If `reportlab` is not installed, PDF generation is skipped, and a warning is logged. Install `reportlab` for full functionality.
|
| 450 |
+
- **Chart in PDF**: The chart is excluded from the PDF due to complexity in rendering Plotly charts. If needed, I can add code to generate a static image using `plotly.io` and embed it.
|
| 451 |
+
- **Performance**: Large CSV files may slow processing. The anomaly detection function samples to 5,000 rows for larger datasets to prevent memory issues.
|
| 452 |
+
- **Further Issues**: If you encounter other errors, please share the full traceback or terminal output, and I’ll provide a targeted fix.
|
| 453 |
+
|
| 454 |
+
If you need additional features (e.g., chart inclusion in the PDF, custom PDF styling, or stricter CSV validation) or face another error, please let me know with details!
|