Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
|
| 6 |
+
# Load the trained model
|
| 7 |
+
model = joblib.load('power_consumption_model.pkl')
|
| 8 |
+
|
| 9 |
+
# Function to preprocess input and make predictions
|
| 10 |
+
def predict_power_consumption(global_reactive_power, voltage, global_intensity, sub_metering_1, sub_metering_2, sub_metering_3, hour, day, month):
|
| 11 |
+
# Create a DataFrame from the input
|
| 12 |
+
input_data = pd.DataFrame({
|
| 13 |
+
'Global_reactive_power': [global_reactive_power],
|
| 14 |
+
'Voltage': [voltage],
|
| 15 |
+
'Global_intensity': [global_intensity],
|
| 16 |
+
'Sub_metering_1': [sub_metering_1],
|
| 17 |
+
'Sub_metering_2': [sub_metering_2],
|
| 18 |
+
'Sub_metering_3': [sub_metering_3],
|
| 19 |
+
'Hour': [hour],
|
| 20 |
+
'Day': [day],
|
| 21 |
+
'Month': [month]
|
| 22 |
+
})
|
| 23 |
+
|
| 24 |
+
# Make a prediction
|
| 25 |
+
prediction = model.predict(input_data)
|
| 26 |
+
return prediction[0]
|
| 27 |
+
|
| 28 |
+
# Gradio interface
|
| 29 |
+
inputs = [
|
| 30 |
+
gr.Number(label="Global Reactive Power"),
|
| 31 |
+
gr.Number(label="Voltage"),
|
| 32 |
+
gr.Number(label="Global Intensity"),
|
| 33 |
+
gr.Number(label="Sub Metering 1"),
|
| 34 |
+
gr.Number(label="Sub Metering 2"),
|
| 35 |
+
gr.Number(label="Sub Metering 3"),
|
| 36 |
+
gr.Slider(0, 23, step=1, label="Hour"),
|
| 37 |
+
gr.Slider(1, 31, step=1, label="Day"),
|
| 38 |
+
gr.Slider(1, 12, step=1, label="Month")
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
outputs = gr.Number(label="Predicted Global Active Power")
|
| 42 |
+
|
| 43 |
+
# Create the Gradio app
|
| 44 |
+
app = gr.Interface(
|
| 45 |
+
fn=predict_power_consumption,
|
| 46 |
+
inputs=inputs,
|
| 47 |
+
outputs=outputs,
|
| 48 |
+
title="Power Consumption Prediction",
|
| 49 |
+
description="Predict global active power consumption based on input features."
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# Launch the app
|
| 53 |
+
app.launch()
|