Spaces:
Sleeping

project / app.py
Kavi11's picture
Update app.py
9aa5184 verified
Raw History Blame Contribute Delete
7.8 kB
import streamlit as st
import pickle
import numpy as np
# Set page configuration
st.set_page_config(
page_title="Laptop Price Predictor",
page_icon="๐Ÿ’ป",
layout="wide",
initial_sidebar_state="expanded"
)
# Custom CSS for better styling
st.markdown("""
<style>
.main-header {
text-align: center;
color: #2E86AB;
font-size: 3rem;
font-weight: bold;
margin-bottom: 2rem;
text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
}
.sub-header {
text-align: center;
color: #666;
font-size: 1.2rem;
margin-bottom: 3rem;
}
.section-header {
color: #2E86AB;
font-size: 1.5rem;
font-weight: bold;
margin-top: 2rem;
margin-bottom: 1rem;
border-bottom: 2px solid #2E86AB;
padding-bottom: 0.5rem;
}
.price-result {
background: linear-gradient(45deg, #2E86AB, #A23B72);
color: white;
padding: 2rem;
border-radius: 15px;
text-align: center;
font-size: 2rem;
font-weight: bold;
margin: 2rem 0;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
animation: pulse 2s infinite;
}
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.02); }
100% { transform: scale(1); }
}
.stButton > button {
background: linear-gradient(45deg, #2E86AB, #A23B72);
color: white;
border: none;
padding: 0.75rem 2rem;
font-size: 1.2rem;
font-weight: bold;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
transition: all 0.3s ease;
width: 100%;
}
.stButton > button:hover {
transform: translateY(-2px);
box-shadow: 0 6px 8px rgba(0, 0, 0, 0.15);
}
.feature-card {
background: #f8f9fa;
padding: 1.5rem;
border-radius: 10px;
border-left: 4px solid #2E86AB;
margin-bottom: 1rem;
}
.stSelectbox > div > div {
border-radius: 8px;
}
.stSlider > div > div {
color: #2E86AB;
}
</style>
""", unsafe_allow_html=True)
# Load models
try:
pipe = pickle.load(open('pipe.pkl','rb'))
df = pickle.load(open('df.pkl','rb'))
except FileNotFoundError:
st.error("โš ๏ธ Model files not found! Please ensure 'pipe.pkl' and 'df.pkl' are in the same directory.")
st.stop()
# Header
st.markdown('<h1 class="main-header">๐Ÿ’ป Laptop Price Predictor</h1>', unsafe_allow_html=True)
st.markdown('<p class="sub-header">Get instant price predictions for your ideal laptop configuration</p>', unsafe_allow_html=True)
# Create columns for better layout
col1, col2 = st.columns([1, 1])
with col1:
st.markdown('<h3 class="section-header">๐Ÿท๏ธ Basic Specifications</h3>', unsafe_allow_html=True)
# Brand
company = st.selectbox(
'๐Ÿข Brand',
df['Company'].unique(),
help="Select the laptop manufacturer"
)
# Type of laptop
type = st.selectbox(
'๐Ÿ’ผ Type',
df['TypeName'].unique(),
help="Choose the laptop category"
)
# RAM
ram = st.selectbox(
'๐Ÿง  RAM (GB)',
[2,4,6,8,12,16,24,32,64],
index=3, # Default to 8GB
help="Select the amount of RAM"
)
# Weight
weight = st.number_input(
'โš–๏ธ Weight (kg)',
min_value=0.5,
max_value=5.0,
value=2.0,
step=0.1,
help="Enter the laptop weight in kilograms"
)
# OS
os = st.selectbox(
'๐Ÿ’ฟ Operating System',
df['os'].unique(),
help="Select the operating system"
)
with col2:
st.markdown('<h3 class="section-header">๐Ÿ–ฅ๏ธ Display & Hardware</h3>', unsafe_allow_html=True)
# Screen size
screen_size = st.slider(
'๐Ÿ“ Screen Size (inches)',
10.0, 18.0, 13.0,
step=0.1,
help="Adjust the screen size"
)
# Resolution
resolution = st.selectbox(
'๐Ÿ–ผ๏ธ Screen Resolution',
['1920x1080','1366x768','1600x900','3840x2160','3200x1800','2880x1800','2560x1600','2560x1440','2304x1440'],
index=0, # Default to 1920x1080
help="Select the display resolution"
)
# Touchscreen
touchscreen = st.selectbox(
'๐Ÿ‘† Touchscreen',
['No','Yes'],
help="Does the laptop have a touchscreen?"
)
# IPS
ips = st.selectbox(
'๐ŸŽจ IPS Display',
['No','Yes'],
help="Does the laptop have an IPS display?"
)
# Storage and Processing section
st.markdown('<h3 class="section-header">๐Ÿ’พ Storage & Processing</h3>', unsafe_allow_html=True)
col3, col4, col5 = st.columns(3)
with col3:
# HDD
hdd = st.selectbox(
'๐Ÿ’ฝ HDD Storage (GB)',
[0,128,256,512,1024,2048],
index=0, # Default to 0
help="Select HDD storage capacity"
)
with col4:
# SSD
ssd = st.selectbox(
'โšก SSD Storage (GB)',
[0,8,128,256,512,1024],
index=3, # Default to 256GB
help="Select SSD storage capacity"
)
with col5:
# CPU
cpu = st.selectbox(
'๐Ÿ”ง CPU Brand',
df['Cpu brand'].unique(),
help="Select the processor brand"
)
# GPU section
st.markdown('<h3 class="section-header">๐ŸŽฎ Graphics</h3>', unsafe_allow_html=True)
gpu = st.selectbox(
'๐Ÿ–ฅ๏ธ GPU Brand',
df['Gpu brand'].unique(),
help="Select the graphics card brand"
)
# Prediction section
st.markdown('<h3 class="section-header">๐Ÿ”ฎ Price Prediction</h3>', unsafe_allow_html=True)
# Add some spacing
st.markdown("<br>", unsafe_allow_html=True)
# Center the predict button
col_btn1, col_btn2, col_btn3 = st.columns([1, 2, 1])
with col_btn2:
predict_button = st.button('๐ŸŽฏ Predict Price', use_container_width=True)
if predict_button:
with st.spinner('๐Ÿ”„ Calculating price...'):
# Query processing (original logic preserved)
ppi = None
if touchscreen == 'Yes':
touchscreen = 1
else:
touchscreen = 0
if ips == 'Yes':
ips = 1
else:
ips = 0
X_res = int(resolution.split('x')[0])
Y_res = int(resolution.split('x')[1])
ppi = ((X_res**2) + (Y_res**2))**0.5/screen_size
query = np.array([company,type,ram,weight,touchscreen,ips,ppi,cpu,hdd,ssd,gpu,os],dtype=object)
query = query.reshape(1,12)
predicted_price = int(np.exp(pipe.predict(query)[0]))
# Display result with enhanced styling
st.markdown(f'''
<div class="price-result">
๐Ÿ’ฐ Predicted Price: ${predicted_price:,}
</div>
''', unsafe_allow_html=True)
# Additional info
st.success(f"โœ… Price prediction completed successfully!")
# Display configuration summary
with st.expander("๐Ÿ“‹ Configuration Summary"):
st.write(f"**Brand:** {company}")
st.write(f"**Type:** {type}")
st.write(f"**RAM:** {ram} GB")
st.write(f"**Weight:** {weight} kg")
st.write(f"**Screen:** {screen_size}\" {resolution} {'with Touchscreen' if touchscreen == 1 else ''}")
st.write(f"**Display:** {'IPS' if ips == 1 else 'Standard'}")
st.write(f"**Storage:** {f'{hdd}GB HDD + ' if hdd > 0 else ''}{ssd}GB SSD")
st.write(f"**CPU:** {cpu}")
st.write(f"**GPU:** {gpu}")
st.write(f"**OS:** {os}")
# Footer
st.markdown("---")
st.markdown(
'<p style="text-align: center; color: #666; margin-top: 2rem;">Built with โค๏ธ using Streamlit | Laptop Price Prediction System</p>',
unsafe_allow_html=True
)