Spaces:
Runtime error
Runtime error
Download app.py from Aaron006/fuel_efficiency: direct link, hf CLI and curl.
- Browser
- Download file 2.13 kB
-
https://huggingface.co/spaces/Aaron006/fuel_efficiency/resolve/main/app.py
- Command line
-
hf download hf://spaces/Aaron006/fuel_efficiency/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Aaron006/fuel_efficiency/resolve/main/app.py
2.13 kB
| import gradio as gr | |
| import pandas as pd | |
| import xgboost as xgb | |
| import os | |
| os.environ["MPLCONFIGDIR"] = "/tmp" # Fix for fontconfig error | |
| # Load XGBoost booster from JSON | |
| booster = xgb.Booster() | |
| booster.load_model("xgb_model.json") | |
| # Define options | |
| makes = ['FORD', 'TOYOTA', 'HONDA', 'BMW', 'AUDI'] | |
| transmissions = ['Automatic', 'Manual', 'CVT', 'Auto-Manual', 'Direct Drive'] | |
| fuels = ['Regular Gasoline', 'Premium Gasoline', 'Diesel', 'Electricity', 'E85', 'CNG'] | |
| classes = ['SUV', 'Compact Cars', 'Mid-size Cars', 'Large Cars', 'Pickup Trucks', 'Station Wagons'] | |
| # Preprocess input into DMatrix | |
| def preprocess_input(year, cylinders, displ, make, trany, fuel, vclass): | |
| df = pd.DataFrame([{ | |
| "year": year, | |
| "cylinders": cylinders, | |
| "displ": displ, | |
| "make": make, | |
| "trany": trany, | |
| "fuelType": fuel, | |
| "VClass": vclass | |
| }]) | |
| return xgb.DMatrix(df) | |
| # Predict MPG | |
| def predict_mpg(year, cylinders, displ, make, trany, fuel, vclass): | |
| input_dmatrix = preprocess_input(year, cylinders, displ, make, trany, fuel, vclass) | |
| prediction = booster.predict(input_dmatrix)[0] | |
| return f"π Predicted Combined MPG: {prediction:.2f} MPG" | |
| # Gradio UI | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## π Vehicle MPG Predictor") | |
| gr.Markdown("Enter vehicle details to predict Combined MPG") | |
| with gr.Row(): | |
| with gr.Column(): | |
| year = gr.Slider(2000, 2025, value=2020, step=1, label="Year") | |
| cylinders = gr.Slider(2, 16, value=4, step=1, label="Cylinders") | |
| displ = gr.Slider(1.0, 8.0, value=2.0, step=0.1, label="Engine Displacement (L)") | |
| with gr.Column(): | |
| make = gr.Dropdown(makes, label="Make") | |
| trany = gr.Dropdown(transmissions, label="Transmission") | |
| fuel = gr.Dropdown(fuels, label="Fuel Type") | |
| vclass = gr.Dropdown(classes, label="Vehicle Class") | |
| result = gr.Textbox(label="Prediction Result") | |
| predict_btn = gr.Button("π Predict MPG") | |
| predict_btn.click(fn=predict_mpg, inputs=[year, cylinders, displ, make, trany, fuel, vclass], outputs=result) | |
| demo.launch() |