Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- app.py +44 -55
- requirements.txt +3 -3
app.py
CHANGED
|
@@ -1,60 +1,49 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
-
import numpy as np
|
| 3 |
-
import gradio as gr
|
| 4 |
-
from sklearn.ensemble import RandomForestRegressor
|
| 5 |
-
from sklearn.compose import ColumnTransformer
|
| 6 |
-
from sklearn.pipeline import Pipeline
|
| 7 |
-
from sklearn.preprocessing import OneHotEncoder, StandardScaler
|
| 8 |
import joblib
|
|
|
|
| 9 |
|
| 10 |
-
# Load
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
gr.Markdown("Masukkan detail produk dan faktor supply chain untuk memprediksi jumlah produk yang akan terjual.")
|
| 54 |
-
|
| 55 |
-
input_row = gr.Row(inputs)
|
| 56 |
-
output = gr.Textbox(label="Hasil Prediksi")
|
| 57 |
-
submit = gr.Button("Prediksi")
|
| 58 |
-
submit.click(fn=predict_single, inputs=inputs, outputs=output)
|
| 59 |
|
| 60 |
demo.launch()
|
|
|
|
| 1 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import joblib
|
| 3 |
+
import gradio as gr
|
| 4 |
|
| 5 |
+
# Load model dan data contoh
|
| 6 |
+
model = joblib.load("demand_forecasting_model.pkl")
|
| 7 |
+
data = pd.read_csv("supply_chain_data.csv")
|
| 8 |
+
|
| 9 |
+
# Kolom input yang digunakan model
|
| 10 |
+
input_cols = ['product_category', 'price', 'stock', 'location', 'lead_time']
|
| 11 |
+
|
| 12 |
+
# Fungsi prediksi satuan (input manual)
|
| 13 |
+
def predict_demand(product_category, price, stock, location, lead_time):
|
| 14 |
+
df = pd.DataFrame([[product_category, price, stock, location, lead_time]], columns=input_cols)
|
| 15 |
+
prediction = model.predict(df)[0]
|
| 16 |
+
return round(prediction, 2)
|
| 17 |
+
|
| 18 |
+
# Fungsi prediksi batch dari CSV
|
| 19 |
+
def predict_from_csv(file):
|
| 20 |
+
df = pd.read_csv(file.name)
|
| 21 |
+
if not set(input_cols).issubset(df.columns):
|
| 22 |
+
return "❌ CSV harus mengandung kolom: " + ", ".join(input_cols)
|
| 23 |
+
preds = model.predict(df[input_cols])
|
| 24 |
+
df['predicted_demand'] = preds
|
| 25 |
+
return df
|
| 26 |
+
|
| 27 |
+
# UI Gradio
|
| 28 |
+
with gr.Blocks(title="SmartDemand") as demo:
|
| 29 |
+
gr.Markdown("# 📦 SmartDemand - Supply Chain Demand Forecasting")
|
| 30 |
+
gr.Markdown("Masukkan informasi produk untuk memprediksi permintaan, atau upload file CSV.")
|
| 31 |
+
|
| 32 |
+
with gr.Tab("Prediksi Manual"):
|
| 33 |
+
with gr.Row():
|
| 34 |
+
product_category = gr.Dropdown(choices=data['product_category'].unique().tolist(), label="Product Category")
|
| 35 |
+
price = gr.Number(label="Price")
|
| 36 |
+
stock = gr.Number(label="Stock")
|
| 37 |
+
with gr.Row():
|
| 38 |
+
location = gr.Dropdown(choices=data['location'].unique().tolist(), label="Location")
|
| 39 |
+
lead_time = gr.Number(label="Lead Time (days)")
|
| 40 |
+
output_manual = gr.Number(label="Predicted Demand")
|
| 41 |
+
btn_manual = gr.Button("Predict")
|
| 42 |
+
btn_manual.click(fn=predict_demand, inputs=[product_category, price, stock, location, lead_time], outputs=output_manual)
|
| 43 |
+
|
| 44 |
+
with gr.Tab("Prediksi dari File CSV"):
|
| 45 |
+
csv_input = gr.File(label="Upload CSV", file_types=[".csv"])
|
| 46 |
+
output_csv = gr.Dataframe(label="Hasil Prediksi")
|
| 47 |
+
csv_input.change(fn=predict_from_csv, inputs=csv_input, outputs=output_csv)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
gradio
|
| 2 |
-
scikit-learn
|
| 3 |
pandas
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
pandas
|
| 2 |
+
scikit-learn
|
| 3 |
+
gradio==4.16.0
|
| 4 |
+
joblib
|