Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,3 @@
|
|
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
|
@@ -44,6 +13,7 @@ uploaded_files = st.file_uploader("Upload Device Logs (CSV)", accept_multiple_fi
|
|
44 |
|
45 |
if uploaded_files:
|
46 |
df = load_logs(uploaded_files)
|
|
|
47 |
st.subheader("π Uploaded Logs")
|
48 |
st.dataframe(df.head())
|
49 |
|
@@ -58,27 +28,9 @@ if uploaded_files:
|
|
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
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
from utils.load_data import load_logs
|
|
|
13 |
|
14 |
if uploaded_files:
|
15 |
df = load_logs(uploaded_files)
|
16 |
+
|
17 |
st.subheader("π Uploaded Logs")
|
18 |
st.dataframe(df.head())
|
19 |
|
|
|
28 |
if "amc_expiry" in df.columns:
|
29 |
amc_df = upcoming_amc_devices(df)
|
30 |
st.dataframe(amc_df)
|
31 |
+
else:
|
32 |
+
st.warning("AMC expiry column (`amc_expiry`) not found in uploaded data.")
|
33 |
|
34 |
if st.button("π Generate PDF Report"):
|
35 |
generate_pdf(df)
|
36 |
+
st.success("PDF report generated and saved.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|