Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
-
import
|
2 |
-
import streamlit as st
|
3 |
import pandas as pd
|
4 |
from utils.load_data import load_logs
|
5 |
from utils.visualize import plot_usage
|
@@ -7,66 +6,105 @@ from utils.report import generate_pdf
|
|
7 |
from models.anomaly import detect_anomalies
|
8 |
from utils.amc import upcoming_amc_devices
|
9 |
import logging
|
|
|
10 |
|
11 |
# Configure logging
|
12 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
13 |
logger = logging.getLogger(__name__)
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
st.set_page_config(page_title="LabOps Dashboard", layout="wide")
|
18 |
-
logger.info("Streamlit page configuration set successfully.")
|
19 |
-
except Exception as e:
|
20 |
-
logger.error(f"Failed to configure Streamlit: {e}")
|
21 |
-
raise
|
22 |
-
|
23 |
-
st.title("π Multi-Device LabOps Dashboard")
|
24 |
-
|
25 |
-
# File uploader
|
26 |
-
try:
|
27 |
-
uploaded_files = st.file_uploader("Upload Device Logs (CSV)", accept_multiple_files=True, type=["csv"])
|
28 |
logger.info(f"Received {len(uploaded_files)} uploaded files.")
|
29 |
-
except Exception as e:
|
30 |
-
logger.error(f"File uploader error: {e}")
|
31 |
-
st.error(f"Failed to initialize file uploader: {e}")
|
32 |
-
|
33 |
-
if uploaded_files:
|
34 |
try:
|
|
|
|
|
|
|
|
|
35 |
df = load_logs(uploaded_files)
|
36 |
logger.info(f"Loaded {len(df)} log records from uploaded files.")
|
37 |
|
38 |
-
|
39 |
-
|
40 |
|
41 |
-
|
42 |
fig = plot_usage(df)
|
43 |
-
st.pyplot(fig)
|
44 |
|
45 |
-
|
46 |
anomalies = detect_anomalies(df)
|
47 |
-
|
48 |
|
49 |
-
|
|
|
50 |
if "amc_expiry" in df.columns:
|
51 |
amc_df = upcoming_amc_devices(df)
|
52 |
-
|
53 |
else:
|
54 |
-
|
55 |
logger.warning("Missing `amc_expiry` column in data.")
|
56 |
|
57 |
-
|
58 |
-
pdf_path = generate_pdf(df)
|
59 |
-
with open(pdf_path, "rb") as f:
|
60 |
-
st.download_button("Download PDF", f, file_name="labops_report.pdf", mime="application/pdf")
|
61 |
-
logger.info("PDF report generated and offered for download.")
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
except Exception as e:
|
64 |
-
logger.error(f"
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
if __name__ == "__main__":
|
68 |
try:
|
69 |
logger.info("Application starting...")
|
|
|
70 |
except Exception as e:
|
71 |
logger.error(f"Application failed to start: {e}")
|
72 |
raise
|
|
|
1 |
+
import gradio as gr
|
|
|
2 |
import pandas as pd
|
3 |
from utils.load_data import load_logs
|
4 |
from utils.visualize import plot_usage
|
|
|
6 |
from models.anomaly import detect_anomalies
|
7 |
from utils.amc import upcoming_amc_devices
|
8 |
import logging
|
9 |
+
import os
|
10 |
|
11 |
# Configure logging
|
12 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
13 |
logger = logging.getLogger(__name__)
|
14 |
|
15 |
+
def process_files(*uploaded_files):
|
16 |
+
"""Process uploaded CSV files and generate dashboard outputs."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
logger.info(f"Received {len(uploaded_files)} uploaded files.")
|
|
|
|
|
|
|
|
|
|
|
18 |
try:
|
19 |
+
if not uploaded_files:
|
20 |
+
return "Please upload at least one CSV file.", None, None, None, None
|
21 |
+
|
22 |
+
# Load data
|
23 |
df = load_logs(uploaded_files)
|
24 |
logger.info(f"Loaded {len(df)} log records from uploaded files.")
|
25 |
|
26 |
+
# Log table
|
27 |
+
log_table = df.head().to_dict(orient="records")
|
28 |
|
29 |
+
# Usage chart
|
30 |
fig = plot_usage(df)
|
|
|
31 |
|
32 |
+
# Anomalies
|
33 |
anomalies = detect_anomalies(df)
|
34 |
+
anomaly_table = anomalies.to_dict(orient="records") if not anomalies.empty else "No anomalies detected."
|
35 |
|
36 |
+
# AMC expiries
|
37 |
+
amc_table = None
|
38 |
if "amc_expiry" in df.columns:
|
39 |
amc_df = upcoming_amc_devices(df)
|
40 |
+
amc_table = amc_df.to_dict(orient="records") if not amc_df.empty else "No upcoming AMC expiries."
|
41 |
else:
|
42 |
+
amc_table = "Column `amc_expiry` not found in uploaded data."
|
43 |
logger.warning("Missing `amc_expiry` column in data.")
|
44 |
|
45 |
+
return log_table, fig, anomaly_table, amc_table, df
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
def generate_pdf_report(df):
|
48 |
+
"""Generate and return path to PDF report."""
|
49 |
+
if df is None:
|
50 |
+
return "Please upload CSV files first."
|
51 |
+
logger.info("Generating PDF report...")
|
52 |
+
try:
|
53 |
+
pdf_path = generate_pdf(df)
|
54 |
+
return pdf_path
|
55 |
except Exception as e:
|
56 |
+
logger.error(f"Failed to generate PDF: {e}")
|
57 |
+
return f"Error generating PDF: {e}"
|
58 |
+
|
59 |
+
with gr.Blocks(title="Multi-Device LabOps Dashboard") as demo:
|
60 |
+
gr.Markdown("# π Multi-Device LabOps Dashboard")
|
61 |
+
|
62 |
+
with gr.Row():
|
63 |
+
file_input = gr.File(file_count="multiple", file_types=[".csv"], label="Upload Device Logs (CSV)")
|
64 |
+
|
65 |
+
with gr.Row():
|
66 |
+
submit_btn = gr.Button("Process Files")
|
67 |
+
|
68 |
+
with gr.Row():
|
69 |
+
with gr.Column():
|
70 |
+
gr.Markdown("## π Uploaded Logs")
|
71 |
+
log_output = gr.Dataframe()
|
72 |
+
with gr.Column():
|
73 |
+
gr.Markdown("## π Daily Usage Chart")
|
74 |
+
chart_output = gr.Plot()
|
75 |
+
|
76 |
+
with gr.Row():
|
77 |
+
with gr.Column():
|
78 |
+
gr.Markdown("## π¨ Detected Anomalies")
|
79 |
+
anomaly_output = gr.Dataframe()
|
80 |
+
with gr.Column():
|
81 |
+
gr.Markdown("## π Upcoming AMC Devices")
|
82 |
+
amc_output = gr.Dataframe()
|
83 |
+
|
84 |
+
with gr.Row():
|
85 |
+
pdf_btn = gr.Button("π Generate PDF Report")
|
86 |
+
pdf_output = gr.File(label="Download PDF Report")
|
87 |
+
|
88 |
+
# State to store dataframe
|
89 |
+
df_state = gr.State()
|
90 |
+
|
91 |
+
# Connect inputs to outputs
|
92 |
+
submit_btn.click(
|
93 |
+
fn=process_files,
|
94 |
+
inputs=[file_input],
|
95 |
+
outputs=[log_output, chart_output, anomaly_output, amc_output, df_state]
|
96 |
+
)
|
97 |
+
|
98 |
+
pdf_btn.click(
|
99 |
+
fn=generate_pdf_report,
|
100 |
+
inputs=[df_state],
|
101 |
+
outputs=[pdf_output]
|
102 |
+
)
|
103 |
|
104 |
if __name__ == "__main__":
|
105 |
try:
|
106 |
logger.info("Application starting...")
|
107 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
108 |
except Exception as e:
|
109 |
logger.error(f"Application failed to start: {e}")
|
110 |
raise
|