umanr18075 commited on
Commit
f83ad84
·
verified ·
1 Parent(s): 27353bb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ from huggingface_hub import hf_hub_download
5
+ import joblib
6
+
7
+ # Load the model from Hugging Face
8
+ @st.cache_resource
9
+ def load_model_from_hf(repo_id, filename):
10
+ file_path = hf_hub_download(repo_id=repo_id, filename=filename)
11
+ model = joblib.load(file_path)
12
+ return model
13
+
14
+ # App settings
15
+ st.set_page_config(page_title="Power Prediction App", layout="centered")
16
+
17
+ # Sidebar inputs
18
+ st.sidebar.title("Model Integration Settings")
19
+ repo_id = st.sidebar.text_input("Hugging Face Repo ID", "random_forest_power_model")
20
+ filename = st.sidebar.text_input("Model Filename", "model.joblib")
21
+
22
+ # Main app
23
+ st.title("Power Prediction using Random Forest Model")
24
+ st.write("Enter the input values for current and voltage to predict power.")
25
+
26
+ # Load the model
27
+ try:
28
+ model = load_model_from_hf(repo_id, filename)
29
+ st.success("Model loaded successfully from Hugging Face!")
30
+ except Exception as e:
31
+ st.error(f"Error loading model: {e}")
32
+ st.stop()
33
+
34
+ # User input
35
+ current = st.number_input("Enter Current (I) in Amperes:", min_value=0.0, value=10.0, step=0.1)
36
+ voltage = st.number_input("Enter Voltage (V) in Volts:", min_value=0.0, value=220.0, step=1.0)
37
+
38
+ # Predict power
39
+ if st.button("Predict Power"):
40
+ try:
41
+ input_data = pd.DataFrame({"Current": [current], "Voltage": [voltage]})
42
+ prediction = model.predict(input_data)
43
+ st.success(f"Predicted Power (P): {prediction[0]:.2f} W")
44
+ except Exception as e:
45
+ st.error(f"Error in prediction: {e}")
46
+
47
+ # Option to upload new models
48
+ st.sidebar.header("Upload a New Model")
49
+ uploaded_file = st.sidebar.file_uploader("Upload a .joblib model file", type=["joblib"])
50
+ if uploaded_file:
51
+ with open("uploaded_model.joblib", "wb") as f:
52
+ f.write(uploaded_file.getbuffer())
53
+ st.sidebar.success("Model uploaded! Use 'uploaded_model.joblib' as the filename.")
54
+
55
+ # Footer
56
+ st.write("---")
57
+ st.write("This app uses a Random Forest model hosted on Hugging Face to predict power.")