File size: 2,005 Bytes
f83ad84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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.")