MaheshP98 commited on
Commit
6eaa3dc
Β·
verified Β·
1 Parent(s): 9b3c5e0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Let's add an anomaly detection model stub and AMC reminder logic to the project structure
2
+
3
+ import os
4
+ import zipfile
5
+
6
+ # Define updated file contents with anomaly detection + AMC logic
7
+ updated_files = {
8
+ "LabOps_HuggingFace_Space/models/anomaly.py": """
9
+ import pandas as pd
10
+ import numpy as np
11
+
12
+ def detect_anomalies(df, threshold=2.0):
13
+ df['timestamp'] = pd.to_datetime(df['timestamp'])
14
+ usage = df.groupby(df['timestamp'].dt.date).size()
15
+ mean = usage.mean()
16
+ std = usage.std()
17
+
18
+ anomalies = usage[abs(usage - mean) > threshold * std]
19
+ return anomalies.reset_index().rename(columns={0: 'log_count'})
20
+ """,
21
+ "LabOps_HuggingFace_Space/utils/amc.py": """
22
+ import pandas as pd
23
+ from datetime import datetime, timedelta
24
+
25
+ def upcoming_amc_devices(df, days_ahead=14):
26
+ df['amc_expiry'] = pd.to_datetime(df['amc_expiry'])
27
+ target_date = datetime.today() + timedelta(days=days_ahead)
28
+ upcoming = df[df['amc_expiry'] <= target_date]
29
+ return upcoming[['device_id', 'amc_expiry']]
30
+ """,
31
+ "LabOps_HuggingFace_Space/app.py": """
32
+ import streamlit as st
33
+ import pandas as pd
34
+ from utils.load_data import load_logs
35
+ from utils.visualize import plot_usage
36
+ from utils.report import generate_pdf
37
+ from models.anomaly import detect_anomalies
38
+ from utils.amc import upcoming_amc_devices
39
+
40
+ st.set_page_config(page_title="LabOps Dashboard", layout="wide")
41
+ st.title("πŸ“Š Multi-Device LabOps Dashboard")
42
+
43
+ uploaded_files = st.file_uploader("Upload Device Logs (CSV)", accept_multiple_files=True)
44
+
45
+ if uploaded_files:
46
+ df = load_logs(uploaded_files)
47
+ st.subheader("πŸ“‹ Uploaded Logs")
48
+ st.dataframe(df.head())
49
+
50
+ st.subheader("πŸ“ˆ Daily Usage Chart")
51
+ st.pyplot(plot_usage(df))
52
+
53
+ st.subheader("🚨 Detected Anomalies")
54
+ anomalies = detect_anomalies(df)
55
+ st.dataframe(anomalies)
56
+
57
+ st.subheader("πŸ›  Upcoming AMC Devices")
58
+ if "amc_expiry" in df.columns:
59
+ amc_df = upcoming_amc_devices(df)
60
+ st.dataframe(amc_df)
61
+
62
+ if st.button("πŸ“„ Generate PDF Report"):
63
+ generate_pdf(df)
64
+ st.success("PDF report generated successfully.")
65
+ """
66
+ }
67
+
68
+ # Update the files
69
+ for path, content in updated_files.items():
70
+ full_path = f"/mnt/data/{path}"
71
+ os.makedirs(os.path.dirname(full_path), exist_ok=True)
72
+ with open(full_path, "w") as f:
73
+ f.write(content.strip())
74
+
75
+ # Rezip the full updated project
76
+ zip_path_updated = "/mnt/data/LabOps_HuggingFace_Space_Updated.zip"
77
+ with zipfile.ZipFile(zip_path_updated, 'w') as zipf:
78
+ for folder, _, filenames in os.walk("/mnt/data/LabOps_HuggingFace_Space"):
79
+ for filename in filenames:
80
+ full_path = os.path.join(folder, filename)
81
+ arcname = os.path.relpath(full_path, "/mnt/data/LabOps_HuggingFace_Space")
82
+ zipf.write(full_path, arcname)
83
+
84
+ zip_path_updated