Spaces:
Build error
Build error
Sanket Kathrotiya commited on
Commit ·
4160ee9
1
Parent(s): cb3f094
v3
Browse files
app.py
CHANGED
|
@@ -1,88 +1,105 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
-
import
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
Preprocessing: The date and time passed to the Python function will be a string formatted as YYYY-MM-DD HH:MM:SS or a datetime.datetime object
|
| 11 |
-
depending on the value of the type parameter.
|
| 12 |
-
|
| 13 |
-
Postprocessing: The value returned from the function can be a string or a datetime.datetime object.
|
| 14 |
-
|
| 15 |
-
Parameters:
|
| 16 |
-
value: The default date and time value, formatted as YYYY-MM-DD HH:MM:SS. Can be either a string or datetime.datetime object.
|
| 17 |
-
type: The type of the value to pass to the Python function. Either "string" or "datetime".
|
| 18 |
-
label: The label for the component.
|
| 19 |
-
info: Extra text to render below the component.
|
| 20 |
-
show_label: Whether to show the label for the component.
|
| 21 |
-
container: Whether to show the component in a container.
|
| 22 |
-
scale: The relative size of the component compared to other components in the same row.
|
| 23 |
-
min_width: The minimum width of the component.
|
| 24 |
-
interactive: Whether to allow the user to interact with the component.
|
| 25 |
-
visible: Whether to show the component.
|
| 26 |
-
elem_id: The id of the component. Useful for custom js or css.
|
| 27 |
-
elem_classes: The classes of the component. Useful for custom js or css.
|
| 28 |
-
render: Whether to render the component in the parent Blocks scope.
|
| 29 |
-
load_fn: A function to run when the component is first loaded onto the page to set the initial value.
|
| 30 |
-
every: Whether load_fn should be run on a fixed time interval.
|
| 31 |
-
"""
|
| 32 |
-
|
| 33 |
-
EVENTS = ["change", "input", "submit"]
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
label: str | None = None, info: str | None = None,
|
| 38 |
-
show_label: bool | None = None, container: bool = True, scale: int | None = None,
|
| 39 |
-
min_width: int | None = None, interactive: bool | None = None, visible: bool = True,
|
| 40 |
-
elem_id: str | None = None, elem_classes: list[str] | str | None = None,
|
| 41 |
-
render: bool = True,
|
| 42 |
-
load_fn: Callable[..., Any] | None = None,
|
| 43 |
-
every: float | None = None):
|
| 44 |
-
self._format_str = "%Y-%m-%d %H:%M:%S"
|
| 45 |
-
self.type = type
|
| 46 |
-
super().__init__(value, label=label, info=info, show_label=show_label, container=container, scale=scale, min_width=min_width, interactive=interactive, visible=visible, elem_id=elem_id, elem_classes=elem_classes, render=render, load_fn=load_fn, every=every)
|
| 47 |
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
return payload
|
| 54 |
-
else:
|
| 55 |
-
return datetime.datetime.strptime(payload, self._format_str)
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
|
| 88 |
-
demo.launch()
|
|
|
|
| 1 |
+
import xgboost as xgb
|
| 2 |
+
import pandas as pd
|
| 3 |
import gradio as gr
|
| 4 |
+
import os
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
from pandas.tseries.holiday import USFederalHolidayCalendar as calendar
|
| 7 |
|
| 8 |
+
# Load your trained XGBoost model from a .bin file
|
| 9 |
+
model = xgb.Booster()
|
| 10 |
+
model.load_model(os.path.join(os.path.dirname(__file__), "xgb_model.bin"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Load the unseen data from a CSV file
|
| 13 |
+
df = pd.read_csv(os.path.join(os.path.dirname(__file__), "unseen_data.csv"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Define the expected columns for prediction
|
| 16 |
+
expected_columns = ['temperature', 'year', 'month', 'day', 'hr', 'day_of_week', 'is_weekend', 'holiday']
|
| 17 |
|
| 18 |
+
def get_random_data():
|
| 19 |
+
# Select 5 random rows from the unseen data
|
| 20 |
+
random_data = df.sample(5).copy()
|
| 21 |
+
return random_data
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
def predict_demand(input_df):
|
| 24 |
+
# Prepare data for prediction
|
| 25 |
+
prediction_data = input_df[expected_columns]
|
| 26 |
+
dmatrix = xgb.DMatrix(prediction_data)
|
| 27 |
+
predictions = model.predict(dmatrix)
|
| 28 |
+
# Add predictions to the dataframe
|
| 29 |
+
input_df['predicted_demand'] = predictions.round(0).astype(int)
|
| 30 |
+
return input_df
|
| 31 |
|
| 32 |
+
def format_random_output(prediction_df):
|
| 33 |
+
# Calculate percentage error
|
| 34 |
+
prediction_df['error_percentage'] = ((prediction_df['predicted_demand'] - prediction_df['demand']) / prediction_df['demand'] * 100).round(2)
|
| 35 |
+
|
| 36 |
+
# Format date and time
|
| 37 |
+
prediction_df['datetime'] = pd.to_datetime(prediction_df['date'] + ' ' + prediction_df['hr'].astype(str) + ':00:00')
|
| 38 |
+
|
| 39 |
+
# Select and rename columns
|
| 40 |
+
output_df = prediction_df[['datetime', 'temperature', 'predicted_demand', 'demand', 'error_percentage']]
|
| 41 |
+
output_df.columns = ['Date and Time', 'Temperature (°C)', 'Predicted Demand (MW)', 'Actual Demand (MW)', 'Error (%)']
|
| 42 |
+
|
| 43 |
+
return output_df
|
| 44 |
|
| 45 |
+
def gradio_interface():
|
| 46 |
+
# Get random data and predict
|
| 47 |
+
random_data = get_random_data()
|
| 48 |
+
prediction_df = predict_demand(random_data)
|
| 49 |
+
formatted_output = format_random_output(prediction_df)
|
| 50 |
+
return formatted_output
|
| 51 |
|
| 52 |
+
def custom_predict(date_time, temperature):
|
| 53 |
+
# Parse date and time
|
| 54 |
+
dt = pd.to_datetime(date_time)
|
| 55 |
+
|
| 56 |
+
# Calculate additional parameters
|
| 57 |
+
is_weekend = dt.dayofweek >= 5
|
| 58 |
+
holidays = calendar().holidays(start=dt.floor('D'), end=dt.ceil('D'))
|
| 59 |
+
is_holiday = dt.floor('D') in holidays
|
| 60 |
|
| 61 |
+
# Create custom data
|
| 62 |
+
custom_data = pd.DataFrame([[
|
| 63 |
+
temperature,
|
| 64 |
+
dt.year,
|
| 65 |
+
dt.month,
|
| 66 |
+
dt.day,
|
| 67 |
+
dt.hour,
|
| 68 |
+
dt.dayofweek,
|
| 69 |
+
int(is_weekend),
|
| 70 |
+
int(is_holiday)
|
| 71 |
+
]], columns=expected_columns)
|
| 72 |
+
|
| 73 |
+
# Predict
|
| 74 |
+
prediction_df = predict_demand(custom_data)
|
| 75 |
+
|
| 76 |
+
# Format output
|
| 77 |
+
output_df = pd.DataFrame({
|
| 78 |
+
'Date and Time': [dt],
|
| 79 |
+
'Temperature (°C)': [temperature],
|
| 80 |
+
'Predicted Demand (MW)': prediction_df['predicted_demand']
|
| 81 |
+
})
|
| 82 |
+
|
| 83 |
+
return output_df
|
| 84 |
|
| 85 |
+
# Create the Gradio app
|
| 86 |
+
with gr.Blocks(title="Electricity Demand Prediction") as demo:
|
| 87 |
+
gr.Markdown("# Electricity Demand Prediction")
|
| 88 |
+
gr.Markdown("Predict electricity demand based on various factors.")
|
| 89 |
+
|
| 90 |
+
with gr.Tab("Random Predictions"):
|
| 91 |
+
random_output = gr.DataFrame(label="Random Predictions")
|
| 92 |
+
random_button = gr.Button("Predict for 5 Random Data Points")
|
| 93 |
+
random_button.click(fn=gradio_interface, outputs=random_output)
|
| 94 |
+
|
| 95 |
+
with gr.Tab("Custom Prediction"):
|
| 96 |
+
with gr.Row():
|
| 97 |
+
date_time = gr.DateTime(label="Date and Time")
|
| 98 |
+
temperature = gr.Slider(0, 40, label="Temperature (°C)")
|
| 99 |
+
custom_output = gr.DataFrame(label="Custom Prediction")
|
| 100 |
+
custom_button = gr.Button("Predict for Custom Input")
|
| 101 |
+
custom_button.click(fn=custom_predict,
|
| 102 |
+
inputs=[date_time, temperature],
|
| 103 |
+
outputs=custom_output)
|
| 104 |
|
| 105 |
+
demo.launch()
|
|
|