Download app.py from arawan003/car: direct link, hf CLI and curl.
- Browser
- Download file 3.46 kB
-
https://huggingface.co/spaces/arawan003/car/resolve/main/app.py
- Command line
-
hf download hf://spaces/arawan003/car/app.py
-
curl -L -o app.py https://huggingface.co/spaces/arawan003/car/resolve/main/app.py
3.46 kB
| import gradio as gr | |
| import pandas as pd | |
| import joblib | |
| import numpy as np | |
| # Load the trained model and preprocessors | |
| rfr_model = joblib.load('car_price_model.pkl') | |
| encoder = joblib.load('encoder.pkl') | |
| scaler = joblib.load('scaler.pkl') | |
| # These choices should ideally be loaded from a config or determined dynamically | |
| # For this example, they are hardcoded based on the training data. | |
| brand_choices = ['Audi', 'Hyundai', 'Mercedes', 'Kia', 'Nissan', 'Suzuki', 'BMW', 'Honda', 'Toyota', 'Ford'] | |
| fuel_type_choices = ['Petrol', 'Diesel', 'Hybrid'] | |
| transmission_choices = ['Manual', 'Automatic'] | |
| # Define the prediction function | |
| def predict_price( | |
| brand, | |
| model_year, | |
| engine_size, | |
| mileage, | |
| fuel_type, | |
| transmission, | |
| owner_count | |
| ): | |
| # Create a DataFrame from the input arguments | |
| input_df = pd.DataFrame({ | |
| 'brand': [brand], | |
| 'model_year': [model_year], | |
| 'engine_size': [engine_size], | |
| 'mileage': [mileage], | |
| 'fuel_type': [fuel_type], | |
| 'transmission': [transmission], | |
| 'owner_count': [owner_count] | |
| }) | |
| # Identify categorical and numerical columns based on original preprocessing setup | |
| categorical_cols = ['brand', 'fuel_type', 'transmission'] | |
| numerical_cols_to_scale = ['model_year', 'engine_size', 'mileage'] | |
| remainder_cols = ['owner_count'] | |
| # Separate features for preprocessing | |
| input_categorical = input_df[categorical_cols] | |
| input_numerical_to_scale = input_df[numerical_cols_to_scale] | |
| input_remainder = input_df[remainder_cols] | |
| # Apply OneHotEncoder to categorical features | |
| encoded_features = encoder.transform(input_categorical) | |
| # Apply StandardScaler to numerical features to be scaled | |
| scaled_features = scaler.transform(input_numerical_to_scale) | |
| # Concatenate all processed features in the correct order | |
| # The order of columns from preprocessor_final was: encoded_categorical, scaled_numerical, remainder (owner_count) | |
| processed_features = np.concatenate( | |
| [encoded_features, scaled_features, input_remainder.values], | |
| axis=1 | |
| ) | |
| # Make prediction | |
| predicted_price = rfr_model.predict(processed_features)[0] | |
| return f'Predicted Price: Rs {predicted_price:,.2f}' | |
| # Create Gradio input components | |
| brand_input = gr.Dropdown(brand_choices, label="Brand", value='Audi') | |
| model_year_input = gr.Slider(minimum=2000, maximum=2024, value=2015, step=1, label="Model Year") | |
| engine_size_input = gr.Slider(minimum=0.5, maximum=5.0, value=2.0, step=0.1, label="Engine Size (L)") | |
| mileage_input = gr.Slider(minimum=0, maximum=300000, value=50000, step=1000, label="Mileage (km)") | |
| fuel_type_input = gr.Dropdown(fuel_type_choices, label="Fuel Type", value='Petrol') | |
| transmission_input = gr.Dropdown(transmission_choices, label="Transmission", value='Automatic') | |
| owner_count_input = gr.Slider(minimum=1, maximum=4, value=1, step=1, label="Owner Count") | |
| # Create Gradio output component | |
| output = gr.Textbox(label="Predicted Car Price") | |
| # Launch the Gradio interface | |
| iface = gr.Interface( | |
| fn=predict_price, | |
| inputs=[ | |
| brand_input, | |
| model_year_input, | |
| engine_size_input, | |
| mileage_input, | |
| fuel_type_input, | |
| transmission_input, | |
| owner_count_input | |
| ], | |
| outputs=output, | |
| title="Car Price Prediction App", | |
| description="Enter car features to get a predicted selling price." | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch(share=False) | |