Spaces:
Sleeping
Sleeping
# 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 | |
# ---------------------- | |
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() | |