Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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: Salesforce + Hugging Face
|
10 |
+
# ----------------------
|
11 |
+
SF_USERNAME = "[email protected]"
|
12 |
+
SF_PASSWORD = "Vedavathi@04"
|
13 |
+
SF_SECURITY_TOKEN = "jqe4His8AcuFJucZz5NBHfGU"
|
14 |
+
SF_DOMAIN = "login" # or "test" for sandbox
|
15 |
+
|
16 |
+
HF_API_URL = "https://api-inference.huggingface.co/models/your-username/your-model"
|
17 |
+
HF_API_TOKEN = "hf_your_token"
|
18 |
+
|
19 |
+
# ----------------------
|
20 |
+
# Connect to Salesforce
|
21 |
+
# ----------------------
|
22 |
+
@st.cache_resource
|
23 |
+
def connect_salesforce():
|
24 |
+
sf = Salesforce(username=SF_USERNAME, password=SF_PASSWORD, security_token=SF_SECURITY_TOKEN, domain=SF_DOMAIN)
|
25 |
+
return sf
|
26 |
+
|
27 |
+
# ----------------------
|
28 |
+
# Get Pole Data from Salesforce
|
29 |
+
# ----------------------
|
30 |
+
def fetch_pole_data(sf):
|
31 |
+
query = """
|
32 |
+
SELECT Name, Solar_Gen__c, Wind_Gen__c, Tilt__c, Vibration__c, Camera_Status__c
|
33 |
+
FROM Smart_Pole__c
|
34 |
+
LIMIT 50
|
35 |
+
"""
|
36 |
+
records = sf.query_all(query)['records']
|
37 |
+
df = pd.DataFrame(records).drop(columns=['attributes'])
|
38 |
+
return df
|
39 |
+
|
40 |
+
# ----------------------
|
41 |
+
# Send Data to Hugging Face Model
|
42 |
+
# ----------------------
|
43 |
+
def get_hf_predictions(df):
|
44 |
+
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
45 |
+
preds = []
|
46 |
+
|
47 |
+
for _, row in df.iterrows():
|
48 |
+
input_data = {
|
49 |
+
"solar": row["Solar_Gen__c"],
|
50 |
+
"wind": row["Wind_Gen__c"],
|
51 |
+
"tilt": row["Tilt__c"],
|
52 |
+
"vibration": row["Vibration__c"],
|
53 |
+
"camera": row["Camera_Status__c"]
|
54 |
+
}
|
55 |
+
response = requests.post(HF_API_URL, headers=headers, json={"inputs": input_data})
|
56 |
+
if response.status_code == 200:
|
57 |
+
result = response.json()
|
58 |
+
preds.append(result[0]['label'] if isinstance(result, list) else result.get("label", "Unknown"))
|
59 |
+
else:
|
60 |
+
preds.append("Error")
|
61 |
+
|
62 |
+
df["Alert_Prediction"] = preds
|
63 |
+
return df
|
64 |
+
|
65 |
+
# ----------------------
|
66 |
+
# Streamlit App
|
67 |
+
# ----------------------
|
68 |
+
def main():
|
69 |
+
st.title("π Salesforce β Hugging Face Smart Pole Integration")
|
70 |
+
|
71 |
+
sf = connect_salesforce()
|
72 |
+
df = fetch_pole_data(sf)
|
73 |
+
|
74 |
+
if not df.empty:
|
75 |
+
st.subheader("π₯ Raw Pole Data from Salesforce")
|
76 |
+
st.dataframe(df)
|
77 |
+
|
78 |
+
st.subheader("π€ Running Hugging Face AI Predictions...")
|
79 |
+
df = get_hf_predictions(df)
|
80 |
+
|
81 |
+
st.success("β
Predictions Complete")
|
82 |
+
st.subheader("π Results with Predictions")
|
83 |
+
st.dataframe(df)
|
84 |
+
else:
|
85 |
+
st.warning("No data fetched from Salesforce.")
|
86 |
+
|
87 |
+
if __name__ == "__main__":
|
88 |
+
main()
|