File size: 2,597 Bytes
02048bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# app.py

import streamlit as st
import pandas as pd
import requests
from simple_salesforce import Salesforce

# ----------------------
# CONFIG: Salesforce + Hugging Face
# ----------------------
SF_USERNAME = "[email protected]"
SF_PASSWORD = "Vedavathi@04"
SF_SECURITY_TOKEN = "jqe4His8AcuFJucZz5NBHfGU"
SF_DOMAIN = "login"  # or "test" for sandbox

HF_API_URL = "https://api-inference.huggingface.co/models/your-username/your-model"
HF_API_TOKEN = "hf_your_token"

# ----------------------
# Connect to Salesforce
# ----------------------
@st.cache_resource
def connect_salesforce():
    sf = Salesforce(username=SF_USERNAME, password=SF_PASSWORD, security_token=SF_SECURITY_TOKEN, domain=SF_DOMAIN)
    return sf

# ----------------------
# Get Pole Data from Salesforce
# ----------------------
def fetch_pole_data(sf):
    query = """
    SELECT Name, Solar_Gen__c, Wind_Gen__c, Tilt__c, Vibration__c, Camera_Status__c 
    FROM Smart_Pole__c
    LIMIT 50
    """
    records = sf.query_all(query)['records']
    df = pd.DataFrame(records).drop(columns=['attributes'])
    return df

# ----------------------
# Send Data to Hugging Face Model
# ----------------------
def get_hf_predictions(df):
    headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
    preds = []

    for _, row in df.iterrows():
        input_data = {
            "solar": row["Solar_Gen__c"],
            "wind": row["Wind_Gen__c"],
            "tilt": row["Tilt__c"],
            "vibration": row["Vibration__c"],
            "camera": row["Camera_Status__c"]
        }
        response = requests.post(HF_API_URL, headers=headers, json={"inputs": input_data})
        if response.status_code == 200:
            result = response.json()
            preds.append(result[0]['label'] if isinstance(result, list) else result.get("label", "Unknown"))
        else:
            preds.append("Error")

    df["Alert_Prediction"] = preds
    return df

# ----------------------
# Streamlit App
# ----------------------
def main():
    st.title("πŸ”Œ Salesforce ↔ Hugging Face Smart Pole Integration")

    sf = connect_salesforce()
    df = fetch_pole_data(sf)

    if not df.empty:
        st.subheader("πŸ“₯ Raw Pole Data from Salesforce")
        st.dataframe(df)

        st.subheader("πŸ€– Running Hugging Face AI Predictions...")
        df = get_hf_predictions(df)
        
        st.success("βœ… Predictions Complete")
        st.subheader("πŸ“Š Results with Predictions")
        st.dataframe(df)
    else:
        st.warning("No data fetched from Salesforce.")

if __name__ == "__main__":
    main()