car_dashboard / src /src_backup /streamlit_app.py
Praful
Upload only src folder with streamlit_app.py
af73e08
Raw
History Blame Contribute Delete
4.05 kB
import streamlit as st
import pandas as pd
import requests
import plotly.express as px
# -------------------------------
# Load Final Dataset
# -------------------------------
@st.cache_data
def load_data():
url = 'https://raw.githubusercontent.com/xprafulx/car-predict/refs/heads/main/final_car_data.csv'
df = pd.read_csv(url)
return df
df = load_data()
# -------------------------------
# Sidebar: User Inputs
# -------------------------------
st.sidebar.header("Select Car Features")
# Numeric Features (already engineered in dataset)
car_age_choice = st.sidebar.select_slider(
"Car Age",
options=sorted(df['car_age'].unique())
)
mileage_choice = st.sidebar.slider(
"Mileage per Year",
float(df['mileage_per_year'].min()),
float(df['mileage_per_year'].max()),
float(df['mileage_per_year'].min())
)
levy_choice = st.sidebar.slider(
"Levy",
float(df['levy'].min()),
float(df['levy'].max()),
float(df['levy'].min())
)
engine_volume_choice = st.sidebar.select_slider(
"Engine Volume",
options=sorted(df['engine_volume'].unique())
)
cylinders_choice = st.sidebar.select_slider(
"Cylinders",
options=sorted(df['cylinders'].unique())
)
airbags_choice = st.sidebar.select_slider(
"Airbags",
options=sorted(df['airbags'].unique())
)
# Categorical Features
category_choice = st.sidebar.selectbox("Category", sorted(df['category'].unique()))
fuel_type_choice = st.sidebar.selectbox("Fuel Type", sorted(df['fuel_type'].unique()))
gear_box_choice = st.sidebar.selectbox("Gear Box Type", sorted(df['gear_box_type'].unique()))
drive_wheels_choice = st.sidebar.selectbox("Drive Wheels", sorted(df['drive_wheels'].unique()))
doors_choice = st.sidebar.selectbox("Doors", sorted(df['doors'].unique()))
wheel_choice = st.sidebar.selectbox("Wheel", sorted(df['wheel'].unique()))
color_choice = st.sidebar.selectbox("Color", sorted(df['color'].unique()))
# Boolean Feature
leather_interior_choice = st.sidebar.checkbox("Leather Interior", value=False)
# -------------------------------
# Prepare API Payload (fixed serialization)
# -------------------------------
payload = {
"data": [
[
int(car_age_choice),
float(engine_volume_choice),
int(cylinders_choice),
int(airbags_choice),
float(mileage_choice),
float(levy_choice),
str(category_choice),
str(fuel_type_choice),
str(gear_box_choice),
str(drive_wheels_choice),
str(doors_choice),
str(wheel_choice),
str(color_choice),
bool(leather_interior_choice)
]
]
}
# -------------------------------
# Call Hugging Face API
# -------------------------------
api_url = "https://appleballcay-car-price-api.hf.space/run/predict_batch"
response = requests.post(api_url, json=payload)
if response.status_code == 200:
predicted_price = response.json()['data'][0]
st.subheader("Predicted Car Price")
st.write(f"${predicted_price:,.2f}")
# -------------------------------
# Scatter Plot (with predicted car)
# -------------------------------
fig = px.scatter(
df,
x="engine_volume",
y="price",
color="fuel_type",
size='levy',
hover_name="model",
hover_data=[
"manufacturer", "prod._year", "mileage_km", "leather_interior",
'category','gear_box_type','drive_wheels','doors','wheel','color',
'cylinders','airbags'
],
title="Engine Volume vs. Price Colored by Fuel Type",
labels={"engine_volume": "Engine Volume (L)", "price": "Price ($)"}
)
# Highlight the predicted car
fig.add_scatter(
x=[engine_volume_choice],
y=[predicted_price],
mode='markers',
marker=dict(color='red', size=15),
name='Predicted Car'
)
fig.update_layout(width=1200, height=700)
st.plotly_chart(fig)
else:
st.error("API request failed. Please check the Space.")