import streamlit as st import pandas as pd import numpy as np from huggingface_hub import hf_hub_download import joblib # Load the model from Hugging Face @st.cache_resource def load_model_from_hf(repo_id, filename): file_path = hf_hub_download(repo_id=repo_id, filename=filename) model = joblib.load(file_path) return model # App settings st.set_page_config(page_title="Power Prediction App", layout="centered") # Sidebar inputs st.sidebar.title("Model Integration Settings") repo_id = st.sidebar.text_input("Hugging Face Repo ID", "random_forest_power_model") filename = st.sidebar.text_input("Model Filename", "model.joblib") # Main app st.title("Power Prediction using Random Forest Model") st.write("Enter the input values for current and voltage to predict power.") # Load the model try: model = load_model_from_hf(repo_id, filename) st.success("Model loaded successfully from Hugging Face!") except Exception as e: st.error(f"Error loading model: {e}") st.stop() # User input current = st.number_input("Enter Current (I) in Amperes:", min_value=0.0, value=10.0, step=0.1) voltage = st.number_input("Enter Voltage (V) in Volts:", min_value=0.0, value=220.0, step=1.0) # Predict power if st.button("Predict Power"): try: input_data = pd.DataFrame({"Current": [current], "Voltage": [voltage]}) prediction = model.predict(input_data) st.success(f"Predicted Power (P): {prediction[0]:.2f} W") except Exception as e: st.error(f"Error in prediction: {e}") # Option to upload new models st.sidebar.header("Upload a New Model") uploaded_file = st.sidebar.file_uploader("Upload a .joblib model file", type=["joblib"]) if uploaded_file: with open("uploaded_model.joblib", "wb") as f: f.write(uploaded_file.getbuffer()) st.sidebar.success("Model uploaded! Use 'uploaded_model.joblib' as the filename.") # Footer st.write("---") st.write("This app uses a Random Forest model hosted on Hugging Face to predict power.")