File size: 2,901 Bytes
cdccea3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
90
91
92
93
94
95
96
# app.py

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

# -----------------------------
# CONFIG β€” Fill with your creds
# -----------------------------
SF_USERNAME = "[email protected]"
SF_PASSWORD = "Vedavathi@04"
SF_SECURITY_TOKEN = "jqe4His8AcuFJucZz5NBHfGU"
SF_DOMAIN = "login"  # or "test" if you're using a sandbox

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

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

# -----------------------------
# Fetch Smart Pole Data
# -----------------------------
def fetch_pole_data(sf):
    query = """
    SELECT Name, Solar_Generation__c, Wind_Generation__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
# -----------------------------
def predict_with_huggingface(df):
    headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
    predictions = []

    for _, row in df.iterrows():
        payload = {
            "inputs": {
                "solar": row.get("Solar_Generation__c"),
                "wind": row.get("Wind_Generation__c"),
                "tilt": row.get("Tilt__c"),
                "vibration": row.get("Vibration__c"),
                "camera": row.get("Camera_Status__c")
            }
        }
        response = requests.post(HF_API_URL, headers=headers, json=payload)
        if response.status_code == 200:
            result = response.json()
            label = result[0]['label'] if isinstance(result, list) else result.get("label", "Unknown")
        else:
            label = "Error"
        predictions.append(label)
    
    df["Predicted Alert Level"] = predictions
    return df

# -----------------------------
# Streamlit App UI
# -----------------------------
def main():
    st.set_page_config(layout="wide")
    st.title("πŸ“‘ Salesforce β†’ Hugging Face Smart Pole Anomaly Detector")

    try:
        sf = connect_salesforce()
        df = fetch_pole_data(sf)

        if df.empty:
            st.warning("No records found.")
            return

        st.subheader("πŸ“‹ Raw Pole Data from Salesforce")
        st.dataframe(df)

        st.subheader("πŸ€– Running Hugging Face Predictions...")
        df_with_preds = predict_with_huggingface(df)
        st.success("Predictions complete.")

        st.subheader("πŸ“Š Results with AI-Predicted Alerts")
        st.dataframe(df_with_preds)

    except Exception as e:
        st.error(f"Error: {e}")

if __name__ == "__main__":
    main()