Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 β Fill with your creds
|
10 |
+
# -----------------------------
|
11 |
+
SF_USERNAME = "[email protected]"
|
12 |
+
SF_PASSWORD = "Vedavathi@04"
|
13 |
+
SF_SECURITY_TOKEN = "jqe4His8AcuFJucZz5NBHfGU"
|
14 |
+
SF_DOMAIN = "login" # or "test" if you're using a sandbox
|
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
|
21 |
+
# -----------------------------
|
22 |
+
def connect_salesforce():
|
23 |
+
return Salesforce(username=SF_USERNAME, password=SF_PASSWORD, security_token=SF_SECURITY_TOKEN, domain=SF_DOMAIN)
|
24 |
+
|
25 |
+
# -----------------------------
|
26 |
+
# Fetch Smart Pole Data
|
27 |
+
# -----------------------------
|
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 Smart_Pole__c
|
32 |
+
LIMIT 50
|
33 |
+
"""
|
34 |
+
records = sf.query_all(query)['records']
|
35 |
+
df = pd.DataFrame(records).drop(columns=['attributes'])
|
36 |
+
return df
|
37 |
+
|
38 |
+
# -----------------------------
|
39 |
+
# Send data to Hugging Face
|
40 |
+
# -----------------------------
|
41 |
+
def predict_with_huggingface(df):
|
42 |
+
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
43 |
+
predictions = []
|
44 |
+
|
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 |
+
response = requests.post(HF_API_URL, headers=headers, json=payload)
|
56 |
+
if response.status_code == 200:
|
57 |
+
result = response.json()
|
58 |
+
label = result[0]['label'] if isinstance(result, list) else result.get("label", "Unknown")
|
59 |
+
else:
|
60 |
+
label = "Error"
|
61 |
+
predictions.append(label)
|
62 |
+
|
63 |
+
df["Predicted Alert Level"] = predictions
|
64 |
+
return df
|
65 |
+
|
66 |
+
# -----------------------------
|
67 |
+
# Streamlit App UI
|
68 |
+
# -----------------------------
|
69 |
+
def main():
|
70 |
+
st.set_page_config(layout="wide")
|
71 |
+
st.title("π‘ Salesforce β Hugging Face Smart Pole Anomaly Detector")
|
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 from Salesforce")
|
82 |
+
st.dataframe(df)
|
83 |
+
|
84 |
+
st.subheader("π€ Running Hugging Face Predictions...")
|
85 |
+
df_with_preds = predict_with_huggingface(df)
|
86 |
+
st.success("Predictions complete.")
|
87 |
+
|
88 |
+
st.subheader("π Results with AI-Predicted Alerts")
|
89 |
+
st.dataframe(df_with_preds)
|
90 |
+
|
91 |
+
except Exception as e:
|
92 |
+
st.error(f"Error: {e}")
|
93 |
+
|
94 |
+
if __name__ == "__main__":
|
95 |
+
main()
|