Spaces:
Build error
Build error
| import gradio as gr | |
| import pandas as pd | |
| import joblib | |
| # Load dataset referensi | |
| df = pd.read_csv('data_summary.csv') # Pastikan file ada di direktori repo HF | |
| # Load model Random Forest | |
| model = joblib.load("model_rf.joblib") | |
| def prediksi_harga(brand, model_input, year, mileage, fuel_type, | |
| transmission, body_type, engine_capacity, seller_type): | |
| input_df = pd.DataFrame([{ | |
| 'brand': brand, | |
| 'model': model_input, | |
| 'year': int(year), | |
| 'mileage': float(mileage), | |
| 'fuel_type': fuel_type, | |
| 'transmission': transmission, | |
| 'body_type': body_type, | |
| 'engine_capacity': float(engine_capacity), | |
| 'seller_type': seller_type | |
| }]) | |
| try: | |
| pred = model.predict(input_df)[0] | |
| pred_rupiah = pred * 1_000_000 | |
| return f"Perkiraan Harga: Rp {pred_rupiah:,.0f}".replace(",", ".") | |
| except Exception as e: | |
| return f"Gagal prediksi: {str(e)}" | |
| def get_unique(col): | |
| return sorted(df[col].dropna().unique().tolist()) | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## π Prediksi Harga Mobil Bekas (Random Forest)") | |
| gr.Markdown("Isi informasi mobil untuk memprediksi harga jual.") | |
| with gr.Row(): | |
| with gr.Column(): | |
| brand = gr.Dropdown(choices=get_unique('brand'), label="Brand") | |
| model_input = gr.Dropdown(choices=get_unique('model'), label="Model") | |
| year = gr.Number(label="Tahun", value=2020, precision=0) | |
| mileage = gr.Number(label="Jarak Tempuh (km)", value=50000) | |
| fuel_type = gr.Dropdown(choices=get_unique('fuel_type'), label="Jenis Bahan Bakar") | |
| transmission = gr.Dropdown(choices=get_unique('transmission'), label="Transmisi") | |
| body_type = gr.Dropdown(choices=get_unique('body_type'), label="Tipe Body") | |
| engine_capacity = gr.Number(label="Kapasitas Mesin (cc)", value=1500) | |
| seller_type = gr.Dropdown(choices=get_unique('seller_type'), label="Tipe Penjual") | |
| predict_button = gr.Button("π Prediksi Harga") | |
| with gr.Column(): | |
| output = gr.Textbox(label="Hasil Prediksi Harga", lines=2) | |
| predict_button.click(fn=prediksi_harga, inputs=[ | |
| brand, model_input, year, mileage, fuel_type, | |
| transmission, body_type, engine_capacity, seller_type | |
| ], outputs=output) | |
| demo.launch() |