Aaron006 commited on
Commit
a79bf3e
Β·
verified Β·
1 Parent(s): 2b02aba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -7
app.py CHANGED
@@ -3,20 +3,19 @@ import pandas as pd
3
  import xgboost as xgb
4
  import os
5
 
6
- # πŸ”§ Fix fontconfig and Matplotlib errors
7
- os.environ["MPLCONFIGDIR"] = "/tmp"
8
 
9
- # πŸ“₯ Load XGBoost model
10
  booster = xgb.Booster()
11
  booster.load_model("xgb_model.json")
12
 
13
- # 🎯 Label options
14
  makes = ['FORD', 'TOYOTA', 'HONDA', 'BMW', 'AUDI']
15
  transmissions = ['Automatic', 'Manual', 'CVT', 'Auto-Manual', 'Direct Drive']
16
  fuels = ['Regular Gasoline', 'Premium Gasoline', 'Diesel', 'Electricity', 'E85', 'CNG']
17
  classes = ['SUV', 'Compact Cars', 'Mid-size Cars', 'Large Cars', 'Pickup Trucks', 'Station Wagons']
18
 
19
- # πŸ” Preprocessing (must match your training features!)
20
  def preprocess_input(year, cylinders, displ, make, trany, fuel, vclass):
21
  df = pd.DataFrame([{
22
  "year": year,
@@ -29,13 +28,13 @@ def preprocess_input(year, cylinders, displ, make, trany, fuel, vclass):
29
  }])
30
  return xgb.DMatrix(df)
31
 
32
- # πŸš— Prediction function
33
  def predict_mpg(year, cylinders, displ, make, trany, fuel, vclass):
34
  input_dmatrix = preprocess_input(year, cylinders, displ, make, trany, fuel, vclass)
35
  prediction = booster.predict(input_dmatrix)[0]
36
  return f"πŸš— Predicted Combined MPG: {prediction:.2f} MPG"
37
 
38
- # πŸŽ›οΈ Gradio UI
39
  with gr.Blocks() as demo:
40
  gr.Markdown("## 🚘 Vehicle MPG Predictor")
41
  gr.Markdown("Enter vehicle details to predict Combined MPG")
 
3
  import xgboost as xgb
4
  import os
5
 
6
+ os.environ["MPLCONFIGDIR"] = "/tmp" # Fix for fontconfig error
 
7
 
8
+ # Load XGBoost booster from JSON
9
  booster = xgb.Booster()
10
  booster.load_model("xgb_model.json")
11
 
12
+ # Define options
13
  makes = ['FORD', 'TOYOTA', 'HONDA', 'BMW', 'AUDI']
14
  transmissions = ['Automatic', 'Manual', 'CVT', 'Auto-Manual', 'Direct Drive']
15
  fuels = ['Regular Gasoline', 'Premium Gasoline', 'Diesel', 'Electricity', 'E85', 'CNG']
16
  classes = ['SUV', 'Compact Cars', 'Mid-size Cars', 'Large Cars', 'Pickup Trucks', 'Station Wagons']
17
 
18
+ # Preprocess input into DMatrix
19
  def preprocess_input(year, cylinders, displ, make, trany, fuel, vclass):
20
  df = pd.DataFrame([{
21
  "year": year,
 
28
  }])
29
  return xgb.DMatrix(df)
30
 
31
+ # Predict MPG
32
  def predict_mpg(year, cylinders, displ, make, trany, fuel, vclass):
33
  input_dmatrix = preprocess_input(year, cylinders, displ, make, trany, fuel, vclass)
34
  prediction = booster.predict(input_dmatrix)[0]
35
  return f"πŸš— Predicted Combined MPG: {prediction:.2f} MPG"
36
 
37
+ # Gradio UI
38
  with gr.Blocks() as demo:
39
  gr.Markdown("## 🚘 Vehicle MPG Predictor")
40
  gr.Markdown("Enter vehicle details to predict Combined MPG")