MaheshP98 commited on
Commit
f6b4c75
Β·
verified Β·
1 Parent(s): 31e728d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -28
app.py CHANGED
@@ -1,36 +1,95 @@
1
  import streamlit as st
2
  import pandas as pd
3
- from utils.load_data import load_logs
4
- from utils.visualize import plot_usage
5
- from utils.report import generate_pdf
6
- from models.anomaly import detect_anomalies
7
- from utils.amc import upcoming_amc_devices
 
 
 
 
8
 
 
9
  st.set_page_config(page_title="LabOps Dashboard", layout="wide")
10
- st.title("πŸ“Š Multi-Device LabOps Dashboard")
11
 
12
- uploaded_files = st.file_uploader("Upload Device Logs (CSV)", accept_multiple_files=True)
 
 
 
 
 
13
 
14
- if uploaded_files:
15
- df = load_logs(uploaded_files)
16
-
17
- st.subheader("πŸ“‹ Uploaded Logs")
18
- st.dataframe(df.head())
19
-
20
- st.subheader("πŸ“ˆ Daily Usage Chart")
21
- st.pyplot(plot_usage(df))
22
 
23
- st.subheader("🚨 Detected Anomalies")
24
- anomalies = detect_anomalies(df)
25
- st.dataframe(anomalies)
26
-
27
- st.subheader("πŸ›  Upcoming AMC Devices")
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.")
 
1
  import streamlit as st
2
  import pandas as pd
3
+ import plotly.express as px
4
+ from datetime import datetime, timedelta
5
+ from simple_salesforce import Salesforce
6
+ from transformers import pipeline
7
+ from reportlab.lib.pagesizes import letter
8
+ from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph
9
+ from reportlab.lib import colors
10
+ from reportlab.lib.styles import getSampleStyleSheet
11
+ from utils import fetch_salesforce_data, detect_anomalies, generate_pdf_report
12
 
13
+ # Streamlit app configuration
14
  st.set_page_config(page_title="LabOps Dashboard", layout="wide")
 
15
 
16
+ # Salesforce authentication (replace with your credentials)
17
+ sf = Salesforce(
18
+ username=st.secrets["sf_username"],
19
+ password=st.secrets["sf_password"],
20
+ security_token=st.secrets["sf_security_token"]
21
+ )
22
 
23
+ # Initialize Hugging Face anomaly detection pipeline
24
+ anomaly_detector = pipeline("text-classification", model="bert-base-uncased", tokenizer="bert-base-uncased")
 
 
 
 
 
 
25
 
26
+ def main():
27
+ st.title("Multi-Device LabOps Dashboard")
28
+
29
+ # Filters
30
+ col1, col2, col3 = st.columns(3)
31
+ with col1:
32
+ lab_site = st.selectbox("Select Lab Site", ["All", "Lab1", "Lab2", "Lab3"])
33
+ with col2:
34
+ equipment_type = st.selectbox("Equipment Type", ["All", "Cell Analyzer", "Weight Log", "UV Verification"])
35
+ with col3:
36
+ date_range = st.date_input("Date Range", [datetime.now() - timedelta(days=7), datetime.now()])
37
+
38
+ # Fetch data from Salesforce
39
+ query = f"""
40
+ SELECT Equipment__c, Log_Timestamp__c, Status__c, Usage_Count__c
41
+ FROM SmartLog__c
42
+ WHERE Log_Timestamp__c >= {date_range[0].strftime('%Y-%m-%d')}
43
+ AND Log_Timestamp__c <= {date_range[1].strftime('%Y-%m-%d')}
44
+ """
45
+ if lab_site != "All":
46
+ query += f" AND Lab__c = '{lab_site}'"
47
+ if equipment_type != "All":
48
+ query += f" AND Equipment_Type__c = '{equipment_type}'"
49
+
50
+ data = fetch_salesforce_data(sf, query)
51
+ df = pd.DataFrame(data)
52
+
53
+ if df.empty:
54
+ st.warning("No data available for the selected filters.")
55
+ return
56
+
57
+ # Detect anomalies using Hugging Face
58
+ df["Anomaly"] = df["Status__c"].apply(lambda x: detect_anomalies(x, anomaly_detector))
59
+
60
+ # Device Cards
61
+ st.subheader("Device Status")
62
+ for equipment in df["Equipment__c"].unique():
63
+ device_data = df[df["Equipment__c"] == equipment]
64
+ latest_log = device_data.iloc[-1]
65
+ anomaly = "⚠️ Anomaly" if latest_log["Anomaly"] == "POSITIVE" else "βœ… Normal"
66
+ st.markdown(f"""
67
+ **{equipment}** | Health: {latest_log["Status__c"]} | Usage: {latest_log["Usage_Count__c"]} | Last Log: {latest_log["Log_Timestamp__c"]} | {anomaly}
68
+ """)
69
+
70
+ # Usage Chart
71
+ st.subheader("Usage Trends")
72
+ fig = px.line(df, x="Log_Timestamp__c", y="Usage_Count__c", color="Equipment__c", title="Daily Usage Trends")
73
+ st.plotly_chart(fig, use_container_width=True)
74
+
75
+ # Downtime Chart
76
+ downtime_df = df[df["Status__c"] == "Down"]
77
+ if not downtime_df.empty:
78
+ fig_downtime = px.histogram(downtime_df, x="Log_Timestamp__c", color="Equipment__c", title="Downtime Patterns")
79
+ st.plotly_chart(fig_downtime, use_container_width=True)
80
+
81
+ # AMC Reminders
82
+ st.subheader("AMC Reminders")
83
+ amc_query = "SELECT Equipment__c, AMC_Expiry_Date__c FROM Equipment__c WHERE AMC_Expiry_Date__c <= NEXT_N_DAYS:14"
84
+ amc_data = fetch_salesforce_data(sf, amc_query)
85
+ for record in amc_data:
86
+ st.write(f"Equipment {record['Equipment__c']} - AMC Expiry: {record['AMC_Expiry_Date__c']}")
87
+
88
+ # Export PDF
89
+ if st.button("Export PDF Report"):
90
+ pdf_file = generate_pdf_report(df, lab_site, equipment_type, date_range)
91
+ with open(pdf_file, "rb") as f:
92
+ st.download_button("Download PDF", f, file_name="LabOps_Report.pdf")
93
 
94
+ if __name__ == "__main__":
95
+ main()