Skip to content

Commit

Permalink
Merge pull request #31 from dncq/dev/model
Browse files Browse the repository at this point in the history
Dev/model
  • Loading branch information
dncq committed Dec 29, 2023
2 parents 0eb1f3f + 5c1aae6 commit a63e668
Show file tree
Hide file tree
Showing 28 changed files with 4,775 additions and 9,419 deletions.
Binary file added src/app/__pycache__/app.cpython-310.pyc
Binary file not shown.
Binary file added src/app/__pycache__/map_cpu_gpu.cpython-310.pyc
Binary file not shown.
Binary file added src/app/__pycache__/mapping.cpython-310.pyc
Binary file not shown.
Binary file added src/app/__pycache__/predict_price.cpython-310.pyc
Binary file not shown.
148 changes: 112 additions & 36 deletions src/app/app.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,133 @@
import streamlit as st
import pickle as pk
import pandas as pd
from sklearn.preprocessing import StandardScaler
import joblib
import sys
import os

def predict_price(brand="Apple", cpu="Intel Iris Xe", gpu="GeForce GTX 1650", monitor="15.6\"", screen_size="11920x1080", ram="8GB", storage="256GB", os="macOS", weight="1.78kg", model="RF"):
predicted = 195

return brand, cpu, gpu, monitor, screen_size, ram, storage, os, weight, model, predicted
from predict_price import *

def main():
st.title("Laptop Price Prediction")
st.caption("Introduction to Data Science")

brand = st.selectbox(label="Brand", options=["Apple", "Dell", "Lenovo", "Asus", "Acer", "HP", "Microsoft", "Other"])
os = st.selectbox(label="Operating System", options=["macOS", "Windows 11", "Windows 11 Home", "Windows 11 Pro", "Windows 10", "Chrome OS", "Other"])
# Brand & OS
BRAND_OPTIONS = ["Apple", "Dell", "Lenovo", "HP", "Asus", "Acer", "MSI", "Microsoft", "Other"]
OS_OPTIONS = ["macOS", "Windows 11", "Windows 11 Home", "Windows 11 Pro", "Windows 10", "Chrome OS", "Other"]
brd_col, ops_col = st.columns(2)
with brd_col:
brand_input = st.selectbox(label="Brand", options=BRAND_OPTIONS)
if brand_input=="Other":
brand_input = st.text_input("Brand")
if brand_input == "":
brand_input = "Dell"

with ops_col:
os_input = st.selectbox(label="Operating System", options=OS_OPTIONS)
if os_input=="Other":
os_input = st.text_input("Operating System")
if os_input == "":
os_input = "Windows 11"

cpu = st.text_input(label="CPU", placeholder="e.g. Intel Iris Xe..", value="Intel Iris Xe")
gpu = st.text_input(label="GPU", placeholder="e.g. GeForce GTX 1650..", value="GeForce GTX 1650")
# CPU & GPU Input
cpu_input = st.text_input(label="CPU", placeholder="e.g. Intel Core i7 12700H..")
gpu_input = st.text_input(label="GPU", placeholder="e.g. Intel Iris Xe..")

# RAM
RAM_OPTIONS = ["4GB", "8GB", "16GB", "32GB", "64GB", "128GB", "256GB", "Other"]
STORAGE_OPTIONS = ["64GB", "128GB", "256GB", "512GB", "1TB", "2TB", "4TB", "Other"]
ram_col, stg_col = st.columns(2)
with ram_col:
ram_input = st.selectbox("RAM", options=RAM_OPTIONS)
if ram_input=="Other":
rcol1, rcol2 = st.columns([1,3])
with rcol1:
rtype = st.selectbox("Unit", options=["GB", "TB"])
with rcol2:
rvalue = st.text_input("Enter RAM value", placeholder="e.g. 24")
if rvalue == "":
rvalue = "2"
ram_input = str(rvalue) + rtype

ram_input = st.text_input("Enter RAM value:", key="ram_input", placeholder="e.g. 8GB")
with stg_col:
storage_input = st.selectbox("Storage", options=STORAGE_OPTIONS)
if storage_input=="Other":
scol1, scol2 = st.columns([1,3])
with scol1:
stype = st.selectbox("Unit", options=["GB", "TB"])
with scol2:
svalue = st.text_input("Enter Storage value", placeholder="e.g. 192")
if svalue == "":
svalue == "2"

storage_input = st.text_input("Enter storage value:", key="storage_input", placeholder="e.g. 256GB")
storage_input = str(svalue) + stype

weight_input = st.text_input("Enter weight value:", key="weight_input", placeholder="e.g. 1.78kg")
# Screen Size
SCREEN_SIZE_OPTIONS = ["1366x768", "1600x900", "1920x1080", "1920x1200", "2560x1440", "2560x1600", "3024x1964", "3072x1920", "3840x2160", "3840x2400", "Other"]
resolution_input = st.selectbox("Resolution", options=SCREEN_SIZE_OPTIONS)
if resolution_input == "Other":
wcol, hcol = st.columns(2)
with wcol:
width = st.text_input("Width (pixels)", placeholder="e.g. 1920")
if width == "":
width = 1920
with hcol:
height = st.text_input("Height (pixels)", placeholder="e.g. 1080")
if height == "":
height = 1080
resolution_input = str(width) + "x" + str(height)

# Monitor
MONITOR_OPTIONS = [x / 10 for x in range(106, 200)]
monitor_input = st.select_slider("Monitor", options=MONITOR_OPTIONS)

monitor_input = st.text_input("Enter monitor value:", key="monitor_input", placeholder="e.g. 15.6\"")
WEIGHT_RANGE = [x / 100 for x in range(32, 860)]
weight_input = st.select_slider("Weight", WEIGHT_RANGE)

screen_size = st.text_input("Enter screen_size value:", key="screen_size_input", placeholder="e.g. 1920x1080")

# Display selected values
st.write("___")

selected_model = "RF"
st.write("Features Summary:")
if cpu_input == "":
cpu_input = "Intel Core i7 12700H"
if gpu_input == "":
gpu_input = "Intel Iris Xe"
st.table({
"Brand": brand_input,
"Operating System": os_input,
"CPU": cpu_input,
"GPU": gpu_input,
"RAM": ram_input,
"Storage": storage_input,
"Monitor": str(monitor_input) + "\"",
"Resolution": resolution_input,
"Weight": str(weight_input) + "kg"
})

st.write("___")

if st.button('Submit'):
brand, cpu, gpu, monitor, screen_size, ram, storage, os, weight, model, predicted = predict_price(brand, cpu, gpu, monitor_input, screen_size, ram_input, storage_input, os, weight_input, selected_model)
st.success(f'{predicted} USD')

st.write("Features Summary:")
st.table({
"Brand": brand,
"CPU": cpu,
"GPU": gpu,
"RAM": ram,
"Storage": storage,
"Weight": weight,
"Monitor": monitor,
"Screen Size": screen_size,
"Operating System": os
})

st.write("___")

df = transfer_to_df(brand=brand_input,
cpu=cpu_input,
gpu=gpu_input,
monitor=str(monitor_input),
resolution=resolution_input,
ram=ram_input,
storage=storage_input,
os=os_input,
weight=str(weight_input))
st.dataframe(df)

MODELNAME = "knn_model.pkl"
PARENT_DIR = os.path.abspath(os.path.dirname(__file__))
print(PARENT_DIR)
MODEL_PATH = os.path.abspath(os.path.join(PARENT_DIR, MODELNAME))

price = knn_predict_price(df, MODEL_PATH)
st.success(f'{price} USD')

else:
st.success("")
st.success("0 USD")

if __name__ == '__main__':
main()
main()
Binary file added src/app/knn_model.pkl
Binary file not shown.
3 changes: 2 additions & 1 deletion src/app/mapping.py → src/app/map_cpu_gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ def get_gpu_name(gpu):
if __name__ == '__main__':
TEST_CPU_NAME = 'Intel core i5 1155g7'

_, cpu_mark = get_cpu_name(TEST_CPU_NAME)
b, cpu_mark = get_cpu_name(TEST_CPU_NAME)
print(cpu_mark)
print(b)
36 changes: 0 additions & 36 deletions src/app/predict.py

This file was deleted.

130 changes: 130 additions & 0 deletions src/app/predict_price.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import pandas as pd
from sklearn.preprocessing import StandardScaler
import joblib

from map_cpu_gpu import *


def transfer_to_df(brand,
cpu,
gpu,
monitor,
resolution,
ram,
storage,
os,
weight) -> pd.DataFrame:

df = pd.DataFrame(columns=['Brand_hp', 'GPU Brand_rtx', 'GPU Brand_nvidia', 'Weight', 'Monitor',
'GPU Brand_intel', 'GPU Brand_geforce', 'OS_MacOS', 'Brand_apple',
'CPU Brand_apple', 'RAM', 'Storage Amount', 'GPU Mark', 'Width',
'Height', 'CPU Mark'])

# Brand_hp & Brand_apple
if brand.lower() == "apple":
df.at[0, "Brand_hp"] = 0
df.at[0, "Brand_apple"] = 1
if brand.lower() == "hp":
df.at[0, "Brand_hp"] = 1
df.at[0, "Brand_apple"] = 0
if brand.lower() != "apple" and brand.lower() != "hp":
df.at[0, "Brand_hp"] = 0
df.at[0, "Brand_apple"] = 0

# OS_MacOS
if os.lower() == "macos":
df.at[0, "OS_MacOS"] = 1
if os.lower() != "macos":
df.at[0, "OS_MacOS"] = 0


# GPU_rtx & GPU Brand_nvidia & GPU Brand_intel & GPU Brand_geforce
df.at[0, "GPU Brand_rtx"] = 0
df.at[0, "GPU Brand_nvidia"] = 0
df.at[0, "GPU Brand_intel"] = 0
df.at[0, "GPU Brand_geforce"] = 0
if "intel" in gpu.lower():
df.at[0, "GPU Brand_intel"] = 1
if "nvidia" in gpu.lower():
df.at[0, "GPU Brand_nvidia"] = 1
if "rtx" in gpu.lower():
df.at[0, "GPU Brand_rtx"] = 1
if "geforce" in gpu.lower():
df.at[0, "GPU Brand_geforce"] = 1


if "apple" in cpu.lower():
df.at[0, "CPU Brand_apple"] = 1
if "apple" not in cpu.lower():
df.at[0, "CPU Brand_apple"] = 0

df.at[0, "Weight"] = float(weight)
df.at[0, "Monitor"] = float(monitor)

if ram[-2:] == "TB":
df.at[0, "RAM"] = float(ram[:-2]) * 1024
else:
df.at[0, "RAM"] = float(ram[:-2])

if storage[-2:] == "TB":
df.at[0, "Storage Amount"] = float(storage[:-2]) * 1024
else:
df.at[0, "Storage Amount"] = float(storage[:-2])

width, height = resolution.split("x")
df.at[0, "Width"] = int(width)
df.at[0, "Height"] = int(height)

_, cpu_mark = get_cpu_name(cpu)
_, gpu_mark = get_gpu_name(gpu)
df.at[0, "GPU Mark"] = float(gpu_mark)
df.at[0, "CPU Mark"] = float(cpu_mark)

return df

def knn_predict_price(new_data: pd.DataFrame, model_path):
try:
knn = joblib.load(open(model_path, "rb"))

print(new_data)
y_pred = knn.predict(new_data)
print(new_data)
print(y_pred)

except FileNotFoundError as e:
print(f"Error: {e}")
except Exception as e:
print(f"Unexpected error: {e}")

return float(y_pred[0])


if __name__=="__main__":
BRAND = "ASUS"
CPU = "Intel Core i5-12500H"
GPU = " GeForce RTX 3050"
MONITOR = "17.3"
RESOLUTION = "1920x1080"
RAM = "16GB"
STORAGE = "512GB"
OS = "Windows 11 Home 64-bit"
WEIGHT = "2.60"

# SCALER_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "knn_default_scaler.pkl"))
MODELPATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "knn_model.pkl"))

new_data = transfer_to_df(brand=BRAND,
cpu=CPU,
gpu=GPU,
monitor=MONITOR,
resolution=RESOLUTION,
ram=RAM,
storage=STORAGE,
os=OS,
weight=WEIGHT)

print(new_data)

y_hat = knn_predict_price(new_data=new_data, model_path=MODELPATH)
print(y_hat)
print(type(y_hat))
Binary file added src/model/__pycache__/map_cpu_gpu.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file added src/model/knn_default_scaler.pkl
Binary file not shown.
Binary file added src/model/save_scaler.pkl
Binary file not shown.
Loading

0 comments on commit a63e668

Please sign in to comment.