Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app +50 -0
- demand_forecasting_model.pkl +3 -0
app
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import joblib
|
| 5 |
+
|
| 6 |
+
# Load model
|
| 7 |
+
model = joblib.load("demand_forecasting_model.pkl")
|
| 8 |
+
|
| 9 |
+
# Ordinal encoder (harus sama urutannya dengan saat training)
|
| 10 |
+
from sklearn.preprocessing import OrdinalEncoder
|
| 11 |
+
encoder = OrdinalEncoder()
|
| 12 |
+
encoder.fit([
|
| 13 |
+
['A', 'New York'], ['B', 'Los Angeles'], ['C', 'Chicago'], ['D', 'Houston'], ['E', 'Phoenix']
|
| 14 |
+
]) # dummy untuk encode ulang di input manual
|
| 15 |
+
|
| 16 |
+
# Fungsi prediksi untuk input manual
|
| 17 |
+
def predict_demand(product_type, price, stock, location, lead_time):
|
| 18 |
+
data = pd.DataFrame([[product_type, price, stock, location, lead_time]],
|
| 19 |
+
columns=["Product type", "Price", "Stock levels", "Location", "Lead time"])
|
| 20 |
+
data[["Product type", "Location"]] = encoder.transform(data[["Product type", "Location"]])
|
| 21 |
+
prediction = model.predict(data)
|
| 22 |
+
return round(prediction[0], 2)
|
| 23 |
+
|
| 24 |
+
# Fungsi prediksi dari file CSV
|
| 25 |
+
def predict_from_csv(file):
|
| 26 |
+
df = pd.read_csv(file.name)
|
| 27 |
+
df[["Product type", "Location"]] = encoder.transform(df[["Product type", "Location"]])
|
| 28 |
+
preds = model.predict(df[["Product type", "Price", "Stock levels", "Location", "Lead time"]])
|
| 29 |
+
df["Predicted Demand"] = preds
|
| 30 |
+
return df
|
| 31 |
+
|
| 32 |
+
# Gradio antarmuka
|
| 33 |
+
with gr.Blocks() as demo:
|
| 34 |
+
gr.Markdown("# 📦 Demand Forecasting App")
|
| 35 |
+
with gr.Tab("Input Manual"):
|
| 36 |
+
product_type = gr.Dropdown(["A", "B", "C", "D", "E"], label="Product Type")
|
| 37 |
+
price = gr.Number(label="Price")
|
| 38 |
+
stock = gr.Number(label="Stock Levels")
|
| 39 |
+
location = gr.Dropdown(["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"], label="Location")
|
| 40 |
+
lead_time = gr.Number(label="Lead Time (days)")
|
| 41 |
+
predict_btn = gr.Button("Predict Demand")
|
| 42 |
+
output = gr.Textbox(label="Predicted Demand")
|
| 43 |
+
predict_btn.click(fn=predict_demand, inputs=[product_type, price, stock, location, lead_time], outputs=output)
|
| 44 |
+
|
| 45 |
+
with gr.Tab("Upload CSV"):
|
| 46 |
+
csv_input = gr.File(label="Upload CSV", file_types=[".csv"])
|
| 47 |
+
csv_output = gr.Dataframe(label="CSV with Predictions")
|
| 48 |
+
csv_input.change(fn=predict_from_csv, inputs=csv_input, outputs=csv_output)
|
| 49 |
+
|
| 50 |
+
demo.launch()
|
demand_forecasting_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fa7db6ff9a89d059e1db0b39cbee5f9ae7865d20d18cd4d6adf4ee3a18556b3d
|
| 3 |
+
size 631409
|