Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pandas as pd
|
3 |
+
import tensorflow as tf
|
4 |
+
from tensorflow.keras.models import Sequential
|
5 |
+
from tensorflow.keras.layers import Dense, LSTM
|
6 |
+
from sklearn.ensemble import RandomForestRegressor
|
7 |
+
from sklearn.model_selection import train_test_split
|
8 |
+
from sklearn.preprocessing import MinMaxScaler
|
9 |
+
|
10 |
+
# Function to preprocess data
|
11 |
+
def preprocess_data(data):
|
12 |
+
# Convert date column to datetime
|
13 |
+
data['date'] = pd.to_datetime(data['date'])
|
14 |
+
|
15 |
+
# Create a sequence feature
|
16 |
+
data['sequence'] = np.arange(len(data))
|
17 |
+
|
18 |
+
# Scale the demand values
|
19 |
+
scaler = MinMaxScaler()
|
20 |
+
data['demand_scaled'] = scaler.fit_transform(data['demand'].values.reshape(-1, 1))
|
21 |
+
|
22 |
+
return data, scaler
|
23 |
+
|
24 |
+
# Function for demand forecasting
|
25 |
+
def forecast_demand(data, model_type='LSTM'):
|
26 |
+
data, scaler = preprocess_data(data)
|
27 |
+
|
28 |
+
X = data[['sequence']]
|
29 |
+
y = data['demand_scaled']
|
30 |
+
|
31 |
+
# Split the data into train and test sets
|
32 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
33 |
+
|
34 |
+
if model_type == 'LSTM':
|
35 |
+
# Reshape the input data for LSTM
|
36 |
+
X_train = X_train.values.reshape(-1, 1, 1)
|
37 |
+
X_test = X_test.values.reshape(-1, 1, 1)
|
38 |
+
|
39 |
+
model = Sequential()
|
40 |
+
model.add(LSTM(64, input_shape=(1, 1)))
|
41 |
+
model.add(Dense(1))
|
42 |
+
model.compile(optimizer='adam', loss='mean_squared_error')
|
43 |
+
model.fit(X_train, y_train, epochs=50, batch_size=32, verbose=0)
|
44 |
+
forecast = model.predict(X_test)
|
45 |
+
|
46 |
+
# Inverse scale the forecasted values
|
47 |
+
forecast = scaler.inverse_transform(forecast)
|
48 |
+
elif model_type == 'RandomForest':
|
49 |
+
model = RandomForestRegressor(n_estimators=100, random_state=42)
|
50 |
+
model.fit(X_train, y_train)
|
51 |
+
forecast = model.predict(X_test)
|
52 |
+
|
53 |
+
# Inverse scale the forecasted values
|
54 |
+
forecast = scaler.inverse_transform(forecast.reshape(-1, 1)).squeeze()
|
55 |
+
|
56 |
+
# Inverse scale the actual test values
|
57 |
+
y_test = scaler.inverse_transform(y_test.values.reshape(-1, 1)).squeeze()
|
58 |
+
|
59 |
+
return forecast, y_test
|
60 |
+
|
61 |
+
# Gradio interface
|
62 |
+
import gradio as gr
|
63 |
+
|
64 |
+
def run_app():
|
65 |
+
with gr.Blocks() as demo:
|
66 |
+
gr.Markdown("# Intelligent Inventory and Supply Chain Management")
|
67 |
+
|
68 |
+
with gr.Tab("Demand Forecasting"):
|
69 |
+
with gr.Row():
|
70 |
+
model_type = gr.Radio(["LSTM", "RandomForest"], label="Model Type")
|
71 |
+
with gr.Row():
|
72 |
+
data_upload = gr.File(label="Upload Data")
|
73 |
+
forecast_button = gr.Button("Forecast Demand")
|
74 |
+
with gr.Row():
|
75 |
+
forecast_plot = gr.Plot()
|
76 |
+
forecast_output = gr.Dataframe(label="Forecasted Demand")
|
77 |
+
|
78 |
+
forecast_button.click(forecast_demand_wrapper, inputs=[data_upload, model_type], outputs=[forecast_plot, forecast_output])
|
79 |
+
|
80 |
+
demo.launch()
|
81 |
+
|
82 |
+
if __name__ == "__main__":
|
83 |
+
run_app()
|