Spaces:
Runtime error
Runtime error
| 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() | |