File size: 2,063 Bytes
5c6f8ff
 
40fa23d
5c6f8ff
40fa23d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c6f8ff
 
1
2
3
4
5
6
7
8
9
10
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
import pandas as pd
import joblib
import gradio as gr

# Load model dan data contoh
model = joblib.load("demand_forecasting_model.pkl")
data = pd.read_csv("supply_chain_data.csv")

# Kolom input yang digunakan model
input_cols = ['product_category', 'price', 'stock', 'location', 'lead_time']

# Fungsi prediksi satuan (input manual)
def predict_demand(product_category, price, stock, location, lead_time):
    df = pd.DataFrame([[product_category, price, stock, location, lead_time]], columns=input_cols)
    prediction = model.predict(df)[0]
    return round(prediction, 2)

# Fungsi prediksi batch dari CSV
def predict_from_csv(file):
    df = pd.read_csv(file.name)
    if not set(input_cols).issubset(df.columns):
        return "❌ CSV harus mengandung kolom: " + ", ".join(input_cols)
    preds = model.predict(df[input_cols])
    df['predicted_demand'] = preds
    return df

# UI Gradio
with gr.Blocks(title="SmartDemand") as demo:
    gr.Markdown("# 📦 SmartDemand - Supply Chain Demand Forecasting")
    gr.Markdown("Masukkan informasi produk untuk memprediksi permintaan, atau upload file CSV.")

    with gr.Tab("Prediksi Manual"):
        with gr.Row():
            product_category = gr.Dropdown(choices=data['product_category'].unique().tolist(), label="Product Category")
            price = gr.Number(label="Price")
            stock = gr.Number(label="Stock")
        with gr.Row():
            location = gr.Dropdown(choices=data['location'].unique().tolist(), label="Location")
            lead_time = gr.Number(label="Lead Time (days)")
        output_manual = gr.Number(label="Predicted Demand")
        btn_manual = gr.Button("Predict")
        btn_manual.click(fn=predict_demand, inputs=[product_category, price, stock, location, lead_time], outputs=output_manual)

    with gr.Tab("Prediksi dari File CSV"):
        csv_input = gr.File(label="Upload CSV", file_types=[".csv"])
        output_csv = gr.Dataframe(label="Hasil Prediksi")
        csv_input.change(fn=predict_from_csv, inputs=csv_input, outputs=output_csv)

demo.launch()