Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,97 +1,35 @@
|
|
1 |
# app.py
|
2 |
|
3 |
-
import os
|
4 |
import streamlit as st
|
5 |
-
|
6 |
-
import
|
7 |
-
from
|
8 |
|
9 |
-
# -----------------------------
|
10 |
-
# CONFIG β Use env vars or hardcode for testing
|
11 |
-
# -----------------------------
|
12 |
-
SF_USERNAME = os.getenv("SF_USERNAME", "[email protected]")
|
13 |
-
SF_PASSWORD = os.getenv("SF_PASSWORD", "Vedavathi@04")
|
14 |
-
SF_SECURITY_TOKEN = os.getenv("SF_SECURITY_TOKEN", "jqe4His8AcuFJucZz5NBHfGU")
|
15 |
-
SF_DOMAIN = os.getenv("SF_DOMAIN", "login") # use "test" for sandbox
|
16 |
|
17 |
-
HF_API_URL = os.getenv("HF_API_URL", "https://api-inference.huggingface.co/models/your-model")
|
18 |
-
HF_API_TOKEN = os.getenv("HF_API_TOKEN", "hf_your_token")
|
19 |
|
20 |
-
|
21 |
-
# Connect to Salesforce
|
22 |
-
# -----------------------------
|
23 |
-
def connect_salesforce():
|
24 |
-
return Salesforce(username=SF_USERNAME, password=SF_PASSWORD, security_token=SF_SECURITY_TOKEN, domain=SF_DOMAIN)
|
25 |
|
26 |
-
#
|
27 |
-
|
28 |
-
# -----------------------------
|
29 |
-
def fetch_pole_data(sf):
|
30 |
-
query = """
|
31 |
-
SELECT Name, Solar_Generation__c, Wind_Generation__c, Tilt__c, Vibration__c, Camera_Status__c
|
32 |
-
FROM Vedavathi_Smart_Pole__c
|
33 |
-
LIMIT 50
|
34 |
-
"""
|
35 |
-
records = sf.query_all(query)['records']
|
36 |
-
df = pd.DataFrame(records).drop(columns=['attributes'])
|
37 |
-
return df
|
38 |
|
39 |
-
#
|
40 |
-
|
41 |
-
|
42 |
-
def predict_with_huggingface(df):
|
43 |
-
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
44 |
-
predictions = []
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
"wind": row.get("Wind_Generation__c", 0),
|
51 |
-
"tilt": row.get("Tilt__c", 0),
|
52 |
-
"vibration": row.get("Vibration__c", 0),
|
53 |
-
"camera": row.get("Camera_Status__c", "Online")
|
54 |
-
}
|
55 |
-
}
|
56 |
-
try:
|
57 |
-
response = requests.post(HF_API_URL, headers=headers, json=payload, timeout=10)
|
58 |
-
response.raise_for_status()
|
59 |
-
result = response.json()
|
60 |
-
label = result[0]['label'] if isinstance(result, list) else result.get("label", "Unknown")
|
61 |
-
except Exception as e:
|
62 |
-
label = f"Error: {e}"
|
63 |
-
predictions.append(label)
|
64 |
|
65 |
-
|
66 |
-
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
df = fetch_pole_data(sf)
|
78 |
-
|
79 |
-
if df.empty:
|
80 |
-
st.warning("No records found in Salesforce.")
|
81 |
-
return
|
82 |
-
|
83 |
-
st.subheader("π Raw Pole Data")
|
84 |
-
st.dataframe(df)
|
85 |
-
|
86 |
-
st.subheader("π€ AI Predictions via Hugging Face")
|
87 |
-
df_pred = predict_with_huggingface(df)
|
88 |
-
st.success("β
Predictions completed")
|
89 |
-
|
90 |
-
st.subheader("π Final Table with Alerts")
|
91 |
-
st.dataframe(df_pred)
|
92 |
-
|
93 |
-
except Exception as e:
|
94 |
-
st.error(f"β Error: {e}")
|
95 |
-
|
96 |
-
if __name__ == "__main__":
|
97 |
-
main()
|
|
|
1 |
# app.py
|
2 |
|
|
|
3 |
import streamlit as st
|
4 |
+
from salesforce_integration import fetch_poles
|
5 |
+
import plotly.express as px
|
6 |
+
from modules.visuals import display_dashboard, display_charts
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
|
|
|
|
9 |
|
10 |
+
st.title("π‘ VIEP Smart Poles Dashboard (Salesforce Data)")
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
# Fetch Data
|
13 |
+
df = fetch_poles()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
# Display Table
|
16 |
+
st.subheader("π Pole Table")
|
17 |
+
st.dataframe(df)
|
|
|
|
|
|
|
18 |
|
19 |
+
# Charts
|
20 |
+
st.subheader("β Energy Generation (Solar vs Wind)")
|
21 |
+
st.plotly_chart(px.bar(df, x="Name", y=["Solar_Generation__c", "Wind_Generation__c"], barmode="group"))
|
22 |
+
st.subheader("π₯ Camera Status Overview")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
camera_counts = df["Camera_Status__c"].value_counts().reset_index()
|
25 |
+
camera_counts.columns = ["Status", "Count"]
|
26 |
|
27 |
+
st.plotly_chart(px.bar(
|
28 |
+
camera_counts,
|
29 |
+
x="Count",
|
30 |
+
y="Status",
|
31 |
+
orientation="h",
|
32 |
+
color="Status",
|
33 |
+
title="Camera Status Distribution",
|
34 |
+
text="Count"
|
35 |
+
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|