Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,21 @@
|
|
1 |
# app.py
|
2 |
|
|
|
3 |
import streamlit as st
|
4 |
import pandas as pd
|
5 |
import requests
|
6 |
from simple_salesforce import Salesforce
|
7 |
|
8 |
# -----------------------------
|
9 |
-
# CONFIG β
|
10 |
# -----------------------------
|
11 |
-
SF_USERNAME = "[email protected]"
|
12 |
-
SF_PASSWORD = "Vedavathi@04"
|
13 |
-
SF_SECURITY_TOKEN = "jqe4His8AcuFJucZz5NBHfGU"
|
14 |
-
SF_DOMAIN = "login" #
|
15 |
|
16 |
-
HF_API_URL = "https://api-inference.huggingface.co/models/your-model"
|
17 |
-
HF_API_TOKEN = "hf_your_token"
|
18 |
|
19 |
# -----------------------------
|
20 |
# Connect to Salesforce
|
@@ -28,7 +29,7 @@ def connect_salesforce():
|
|
28 |
def fetch_pole_data(sf):
|
29 |
query = """
|
30 |
SELECT Name, Solar_Generation__c, Wind_Generation__c, Tilt__c, Vibration__c, Camera_Status__c
|
31 |
-
FROM
|
32 |
LIMIT 50
|
33 |
"""
|
34 |
records = sf.query_all(query)['records']
|
@@ -36,7 +37,7 @@ def fetch_pole_data(sf):
|
|
36 |
return df
|
37 |
|
38 |
# -----------------------------
|
39 |
-
#
|
40 |
# -----------------------------
|
41 |
def predict_with_huggingface(df):
|
42 |
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
@@ -45,51 +46,52 @@ def predict_with_huggingface(df):
|
|
45 |
for _, row in df.iterrows():
|
46 |
payload = {
|
47 |
"inputs": {
|
48 |
-
"solar": row.get("Solar_Generation__c"),
|
49 |
-
"wind": row.get("Wind_Generation__c"),
|
50 |
-
"tilt": row.get("Tilt__c"),
|
51 |
-
"vibration": row.get("Vibration__c"),
|
52 |
-
"camera": row.get("Camera_Status__c")
|
53 |
}
|
54 |
}
|
55 |
-
|
56 |
-
|
|
|
57 |
result = response.json()
|
58 |
label = result[0]['label'] if isinstance(result, list) else result.get("label", "Unknown")
|
59 |
-
|
60 |
-
label = "Error"
|
61 |
predictions.append(label)
|
62 |
-
|
63 |
df["Predicted Alert Level"] = predictions
|
64 |
return df
|
65 |
|
66 |
# -----------------------------
|
67 |
-
# Streamlit App
|
68 |
# -----------------------------
|
69 |
def main():
|
70 |
st.set_page_config(layout="wide")
|
71 |
-
st.title("π‘
|
72 |
|
73 |
try:
|
74 |
sf = connect_salesforce()
|
75 |
df = fetch_pole_data(sf)
|
76 |
|
77 |
if df.empty:
|
78 |
-
st.warning("No records found.")
|
79 |
return
|
80 |
|
81 |
-
st.subheader("π Raw Pole Data
|
82 |
st.dataframe(df)
|
83 |
|
84 |
-
st.subheader("π€
|
85 |
-
|
86 |
-
st.success("Predictions
|
87 |
|
88 |
-
st.subheader("π
|
89 |
-
st.dataframe(
|
90 |
|
91 |
except Exception as e:
|
92 |
-
st.error(f"Error: {e}")
|
93 |
|
94 |
if __name__ == "__main__":
|
95 |
main()
|
|
|
1 |
# app.py
|
2 |
|
3 |
+
import os
|
4 |
import streamlit as st
|
5 |
import pandas as pd
|
6 |
import requests
|
7 |
from simple_salesforce import Salesforce
|
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
|
|
|
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']
|
|
|
37 |
return df
|
38 |
|
39 |
# -----------------------------
|
40 |
+
# Predict with Hugging Face
|
41 |
# -----------------------------
|
42 |
def predict_with_huggingface(df):
|
43 |
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
|
|
46 |
for _, row in df.iterrows():
|
47 |
payload = {
|
48 |
"inputs": {
|
49 |
+
"solar": row.get("Solar_Generation__c", 0),
|
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 |
df["Predicted Alert Level"] = predictions
|
66 |
return df
|
67 |
|
68 |
# -----------------------------
|
69 |
+
# Streamlit App
|
70 |
# -----------------------------
|
71 |
def main():
|
72 |
st.set_page_config(layout="wide")
|
73 |
+
st.title("π‘ Vedavathi Smart Pole Anomaly Detection")
|
74 |
|
75 |
try:
|
76 |
sf = connect_salesforce()
|
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()
|