Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import joblib | |
| import numpy as np | |
| import pandas as pd | |
| st.markdown( | |
| """ | |
| <style> | |
| body { | |
| background-color: #008080; | |
| } | |
| .stApp { | |
| background-color: #00FFFF; | |
| } | |
| h1 { | |
| text-align: center; | |
| color: #808080; /* Dark Red */ | |
| } | |
| label, p, div { | |
| color: black !important; | |
| } | |
| .result { | |
| font-size: 24px; | |
| font-weight: bold; | |
| color: #808000; /* Red */ | |
| text-align: center; | |
| } | |
| /* Style for button */ | |
| div.stButton > button { | |
| background-color: #b71c1c !important; /* Dark Red */ | |
| color: white !important; /* White Text */ | |
| border-radius: 8px; | |
| padding: 10px 20px; | |
| font-size: 16px; | |
| } | |
| </style> | |
| """, | |
| unsafe_allow_html=True, | |
| ) | |
| # Load trained model | |
| model = joblib.load("carr.pkl") # Make sure this file exists | |
| # Streamlit UI | |
| st.markdown("<h1 style='text-align: center; color: #1a237e;'> π Car Price Prediction π</h1>", unsafe_allow_html=True) | |
| # User Inputs | |
| ID = st.text_input("ID") | |
| Levy = st.number_input("Levy", min_value=0, step=1) | |
| Manufacturer = st.text_input("Manufacturer") | |
| Model = st.text_input("Model") | |
| Prod_year = st.number_input("Production Year", min_value=1900, max_value=2025, step=1) | |
| Category = st.selectbox("Category", ["Sedan", "SUV", "Hatchback", "Coupe", "Convertible", "Pickup", "Minivan", "Other"]) | |
| Leather_interior = st.radio("Leather Interior", ["Yes", "No"]) | |
| Fuel_type = st.selectbox("Fuel Type", ["Petrol", "Diesel", "Electric", "Hybrid", "Other"]) | |
| Engine_volume = st.number_input("Engine Volume (L)", min_value=0.0, step=0.1) | |
| Mileage = st.number_input("Mileage (km)", min_value=0, step=1000) | |
| Cylinders = st.number_input("Cylinders", min_value=1, max_value=16, step=1) | |
| Gear_box_type = st.selectbox("Gearbox Type", ["Manual", "Automatic", "CVT", "Other"]) | |
| Drive_wheels = st.selectbox("Drive Wheels", ["Front", "Rear", "All Wheel Drive"]) | |
| Doors = st.number_input("Doors", min_value=2, max_value=5, step=1) | |
| Wheel = st.selectbox("Wheel Position", ["Left", "Right"]) | |
| Color = st.color_picker("Color") # Might need conversion | |
| Airbags = st.number_input("Airbags", min_value=0, max_value=12, step=1) | |
| # Convert categorical values to numerical format | |
| Leather_interior = 1 if Leather_interior == "Yes" else 0 | |
| Wheel = 1 if Wheel == "Left" else 0 # Convert to binary | |
| # Create DataFrame | |
| input_df = pd.DataFrame([[ | |
| ID, Levy, Manufacturer, Model, Prod_year, Category, Leather_interior, | |
| Fuel_type, Engine_volume, Mileage, Cylinders, Gear_box_type, Drive_wheels, | |
| Doors, Wheel, Color, Airbags | |
| ]], columns=[ | |
| "ID","Levy", "Manufacturer", "Model", "Prod_year", "Category", "Leather_interior", | |
| "Fuel_type", "Engine_volume", "Mileage", "Cylinders", "Gear_box_type", | |
| "Drive_wheels", "Doors", "Wheel", "Color", "Airbags" | |
| ]) | |
| # Prediction Button | |
| if st.button("Predict Price π°"): | |
| try: | |
| # Check input shape | |
| expected_features = model.n_features_in_ | |
| if input_df.shape[1] != expected_features: | |
| st.error(f"Model expects {expected_features} features, but received {input_df.shape[1]}. Check input data.") | |
| else: | |
| prediction = model.predict(input_df) | |
| formatted_price = f"βΉ {prediction[0]:,.2f}" | |
| st.success(f"Predicted Price: {formatted_price}") | |
| except Exception as e: | |
| st.error(f"Prediction Error: {str(e)}") |