Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ from datetime import datetime, timedelta
|
|
5 |
from simple_salesforce import Salesforce
|
6 |
from transformers import pipeline
|
7 |
from utils import fetch_salesforce_data, detect_anomalies, generate_pdf_report
|
|
|
8 |
|
9 |
# Streamlit app configuration
|
10 |
st.set_page_config(page_title="LabOps Dashboard", layout="wide")
|
@@ -14,9 +15,9 @@ st.set_page_config(page_title="LabOps Dashboard", layout="wide")
|
|
14 |
def init_salesforce():
|
15 |
try:
|
16 |
return Salesforce(
|
17 |
-
username=st.secrets
|
18 |
-
password=st.secrets
|
19 |
-
security_token=st.secrets
|
20 |
)
|
21 |
except Exception as e:
|
22 |
st.error(f"Failed to connect to Salesforce: {e}")
|
@@ -25,12 +26,16 @@ def init_salesforce():
|
|
25 |
# Cache Hugging Face model
|
26 |
@st.cache_resource
|
27 |
def init_anomaly_detector():
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
34 |
|
35 |
# Initialize connections
|
36 |
sf = init_salesforce()
|
@@ -53,8 +58,8 @@ def get_filtered_data(lab_site, equipment_type, date_start, date_end):
|
|
53 |
return fetch_salesforce_data(sf, query)
|
54 |
|
55 |
def main():
|
56 |
-
if sf is None:
|
57 |
-
st.error("Cannot proceed without Salesforce connection.")
|
58 |
return
|
59 |
|
60 |
st.title("Multi-Device LabOps Dashboard")
|
@@ -77,7 +82,8 @@ def main():
|
|
77 |
date_start, date_end = date_range
|
78 |
|
79 |
# Fetch and process data
|
80 |
-
|
|
|
81 |
if not data:
|
82 |
st.warning("No data available for the selected filters.")
|
83 |
return
|
@@ -147,12 +153,13 @@ def main():
|
|
147 |
|
148 |
# Export PDF
|
149 |
if st.button("Export PDF Report"):
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
|
|
156 |
|
157 |
if __name__ == "__main__":
|
158 |
main()
|
|
|
5 |
from simple_salesforce import Salesforce
|
6 |
from transformers import pipeline
|
7 |
from utils import fetch_salesforce_data, detect_anomalies, generate_pdf_report
|
8 |
+
import os
|
9 |
|
10 |
# Streamlit app configuration
|
11 |
st.set_page_config(page_title="LabOps Dashboard", layout="wide")
|
|
|
15 |
def init_salesforce():
|
16 |
try:
|
17 |
return Salesforce(
|
18 |
+
username=os.getenv("SF_USERNAME", st.secrets.get("sf_username")),
|
19 |
+
password=os.getenv("SF_PASSWORD", st.secrets.get("sf_password")),
|
20 |
+
security_token=os.getenv("SF_SECURITY_TOKEN", st.secrets.get("sf_security_token"))
|
21 |
)
|
22 |
except Exception as e:
|
23 |
st.error(f"Failed to connect to Salesforce: {e}")
|
|
|
26 |
# Cache Hugging Face model
|
27 |
@st.cache_resource
|
28 |
def init_anomaly_detector():
|
29 |
+
try:
|
30 |
+
return pipeline(
|
31 |
+
"text-classification",
|
32 |
+
model="distilbert-base-uncased",
|
33 |
+
tokenizer="distilbert-base-uncased",
|
34 |
+
clean_up_tokenization_spaces=True
|
35 |
+
)
|
36 |
+
except Exception as e:
|
37 |
+
st.error(f"Failed to initialize anomaly detector: {e}")
|
38 |
+
return None
|
39 |
|
40 |
# Initialize connections
|
41 |
sf = init_salesforce()
|
|
|
58 |
return fetch_salesforce_data(sf, query)
|
59 |
|
60 |
def main():
|
61 |
+
if sf is None or anomaly_detector is None:
|
62 |
+
st.error("Cannot proceed without Salesforce connection or anomaly detector.")
|
63 |
return
|
64 |
|
65 |
st.title("Multi-Device LabOps Dashboard")
|
|
|
82 |
date_start, date_end = date_range
|
83 |
|
84 |
# Fetch and process data
|
85 |
+
with st.spinner("Fetching data..."):
|
86 |
+
data = get_filtered_data(lab_site, equipment_type, date_start, date_end)
|
87 |
if not data:
|
88 |
st.warning("No data available for the selected filters.")
|
89 |
return
|
|
|
153 |
|
154 |
# Export PDF
|
155 |
if st.button("Export PDF Report"):
|
156 |
+
with st.spinner("Generating PDF..."):
|
157 |
+
try:
|
158 |
+
pdf_file = generate_pdf_report(df, lab_site, equipment_type, [date_start, date_end])
|
159 |
+
with open(pdf_file, "rb") as f:
|
160 |
+
st.download_button("Download PDF", f, file_name="LabOps_Report.pdf", mime="application/pdf")
|
161 |
+
except Exception as e:
|
162 |
+
st.error(f"Failed to generate PDF: {e}")
|
163 |
|
164 |
if __name__ == "__main__":
|
165 |
main()
|