Spaces:
Running
Running
# app.py | |
import os | |
import streamlit as st | |
import pandas as pd | |
import requests | |
from simple_salesforce import Salesforce | |
# ----------------------------- | |
# CONFIG β Use env vars or hardcode for testing | |
# ----------------------------- | |
SF_USERNAME = os.getenv("SF_USERNAME", "[email protected]") | |
SF_PASSWORD = os.getenv("SF_PASSWORD", "Vedavathi@04") | |
SF_SECURITY_TOKEN = os.getenv("SF_SECURITY_TOKEN", "jqe4His8AcuFJucZz5NBHfGU") | |
SF_DOMAIN = os.getenv("SF_DOMAIN", "login") # use "test" for sandbox | |
HF_API_URL = os.getenv("HF_API_URL", "https://api-inference.huggingface.co/models/your-model") | |
HF_API_TOKEN = os.getenv("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 Vedavathi_Smart_Pole__c | |
LIMIT 50 | |
""" | |
records = sf.query_all(query)['records'] | |
df = pd.DataFrame(records).drop(columns=['attributes']) | |
return df | |
# ----------------------------- | |
# Predict with 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", 0), | |
"wind": row.get("Wind_Generation__c", 0), | |
"tilt": row.get("Tilt__c", 0), | |
"vibration": row.get("Vibration__c", 0), | |
"camera": row.get("Camera_Status__c", "Online") | |
} | |
} | |
try: | |
response = requests.post(HF_API_URL, headers=headers, json=payload, timeout=10) | |
response.raise_for_status() | |
result = response.json() | |
label = result[0]['label'] if isinstance(result, list) else result.get("label", "Unknown") | |
except Exception as e: | |
label = f"Error: {e}" | |
predictions.append(label) | |
df["Predicted Alert Level"] = predictions | |
return df | |
# ----------------------------- | |
# Streamlit App | |
# ----------------------------- | |
def main(): | |
st.set_page_config(layout="wide") | |
st.title("π‘ Vedavathi Smart Pole Anomaly Detection") | |
try: | |
sf = connect_salesforce() | |
df = fetch_pole_data(sf) | |
if df.empty: | |
st.warning("No records found in Salesforce.") | |
return | |
st.subheader("π Raw Pole Data") | |
st.dataframe(df) | |
st.subheader("π€ AI Predictions via Hugging Face") | |
df_pred = predict_with_huggingface(df) | |
st.success("β Predictions completed") | |
st.subheader("π Final Table with Alerts") | |
st.dataframe(df_pred) | |
except Exception as e: | |
st.error(f"β Error: {e}") | |
if __name__ == "__main__": | |
main() | |