Spaces:
Runtime error
Runtime error
File size: 5,613 Bytes
6a112d0 8e9513f 6a112d0 2ad594d 6a112d0 2ad594d 6a112d0 2ad594d 6a112d0 2ad594d 6a112d0 d5a1a51 6a112d0 d5a1a51 6a112d0 d5a1a51 6a112d0 d5a1a51 6a112d0 d5a1a51 6a112d0 d5a1a51 6a112d0 d5a1a51 6a112d0 d5a1a51 6a112d0 d5a1a51 6a112d0 d5a1a51 6a112d0 4da6941 6a112d0 d5a1a51 6a112d0 d5a1a51 6a112d0 d5a1a51 6a112d0 d5a1a51 6a112d0 d5a1a51 6a112d0 d5a1a51 6a112d0 d5a1a51 6a112d0 d5a1a51 6a112d0 d5a1a51 6a112d0 d5a1a51 6a112d0 d5a1a51 6a112d0 d5a1a51 6a112d0 d5a1a51 6a112d0 d5a1a51 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
import gradio as gr
import pandas as pd
import numpy as np
import pickle
categorical_features = ['Manufacturer', 'CPU', 'RAM Type', "Screen Resolution"]
numerical_features = ['CPU Speed (GHz)', 'RAM (GB)', 'Bus (MHz)', 'Storage (GB)', 'CPU brand modifier',
'Screen Size (inch)', 'Refresh Rate (Hz)', 'Weight (kg)', 'Battery']
label = ['Price (VND)']
with open("ohe.pkl", "rb") as f:
ohe = pickle.load(f)
def load_model(model):
if model == "XGBRegressor":
with open("XGBRegressor.pkl", "rb") as f:
pred_model = pickle.load(f)
elif model == "RandomForestRegressor":
with open("RandomForestRegressor.pkl", "rb") as f:
pred_model = pickle.load(f)
elif model == "GradientBoostingRegressor":
with open("GradientBoostingRegressor.pkl", "rb") as f:
pred_model = pickle.load(f)
elif model == "AdaBoostRegressor":
with open("AdaBoostRegressor.pkl", "rb") as f:
pred_model = pickle.load(f)
elif model == "BaggingRegressor":
with open("BaggingRegressor.pkl", "rb") as f:
pred_model = pickle.load(f)
return pred_model
def predict(model, brand, cpu, cpu_brand_type, cpu_hz, gpu, ram_type, ram, ram_bus, storage, screen_resolution,
screen_ratio, refresh_rate, screen_size, battery, weight
):
pred_model = load_model(model)
cate_data = {
"Manufacturer": [brand],
"CPU": [cpu],
"RAM Type": [ram_type],
"Screen Resolution": [screen_resolution],
"GPU manufacturer": [gpu],
"Screen Ratio": [screen_ratio]
}
nume_data = {
"CPU brand modifier": [cpu_brand_type],
"CPU Speed (GHz)": [cpu_hz],
"RAM (GB)": [ram],
"Bus (MHz)": [ram_bus],
"Storage (GB)": [storage],
"Screen Size (inch)": [screen_size],
"Refresh Rate (Hz)": [refresh_rate],
"Weight (kg)": [weight],
"Battery": [battery],
}
cate_data = pd.DataFrame(cate_data)
nume_data = pd.DataFrame(nume_data)
cate_data = ohe.transform(cate_data)
cate_data = pd.DataFrame(cate_data, columns=ohe.get_feature_names_out())
data = pd.concat([nume_data, cate_data], axis=1)
return round(float(np.exp(pred_model.predict(np.array(data))[0])) / 1_000_000, 2)
with gr.Blocks(theme=gr.themes.Soft(primary_hue="green")) as demo:
# add gr title to the middle of the page
gr.Markdown("# Laptop Price Prediction")
with gr.Row():
Model = gr.Dropdown(
label="Model",
choices=["XGBRegressor", "RandomForestRegressor", "GradientBoostingRegressor",
"AdaBoostRegressor", "BaggingRegressor",],
value="XGBRegressor",
)
Brand = gr.Dropdown(
label="Brand",
choices=['acer', 'asus', 'dell', 'hp', 'lenovo', 'lg', 'msi'],
value='acer'
)
gr.Markdown("## **CPU & GPU**")
with gr.Row():
CPUBrand = gr.Dropdown(label="CPU", choices=[
"AMD Gen 4.0th", "AMD Gen 5.0th", "AMD Gen 6.0th", "AMD Gen 7.0th",
"Intel Gen 11.0th", "Intel Gen 12.0th", "Intel Gen 13.0th"],
value="Intel Gen 12.0th"
)
CPUBrandType = gr.Radio(label="CPU Type", choices=[3, 5, 7, 9], value=7)
with gr.Row():
CPUHz = gr.Slider(label="CPU Speed (GHz)", minimum=1.0, maximum=5.0, step=0.1, value=4.2, interactive=True)
GPU = gr.Dropdown(label="GPU", choices=["AMD", "NVIDIA", "Intel"], value="Intel")
gr.Markdown("## **RAM & Storage**")
with gr.Row():
RAMType = gr.Dropdown(
label="RAM Type",
choices=["DDR4", "LPDDR4", "LPDDR4X", "DDR5", "LPDDR5", "LPDDR5X"],
value="DDR5"
)
RAM = gr.Radio(label="RAM (GB)", choices=[8, 16, 32, 64, 128], value=16)
with gr.Row():
RAMBus = gr.Slider(label="Bus (MHz)", minimum=1600, maximum=6400, step=400, value=3200, interactive=True)
Storage = gr.Radio(label="Storage (GB)", choices=[256, 512, 1024, 2048], value=512)
gr.Markdown("## **Screen**")
with gr.Row():
ScreenResolution = gr.Dropdown(label="Screen Resolution", choices=[
"720p", "1080p", "2k", "3k", "4k"], value="1080p"
)
ScreenRatio = gr.Radio(label="Screen Ratio", choices=[
"16:9", "16:10", "3:2"], value="16:9")
with gr.Row():
ScreenSize = gr.Radio(label="Screen Size (inch)", choices=[13, 14, 15, 16, 17], value=14)
RefreshRate = gr.Radio(label="Refresh Rate (Hz)", choices=[60, 90, 120, 165, 144, 240], value=60)
gr.Markdown("## **Other Features**")
with gr.Row():
Battery = gr.Radio(label="Battery (Wh)", choices=[40, 50, 60, 70, 80], value=60)
Weight = gr.Radio(label="Weight (kg)", choices=[1.0, 1.5, 2.0, 2.5, 3.0], value=1.5)
# Output Prediction
gr.Markdown("## **Prediction**")
with gr.Row():
output = gr.Number(label="Prediction (Million VND)", info="Click Submit to predict")
with gr.Row():
submit_button = gr.Button("Submit")
submit_button.click(fn=predict,
outputs=output,
inputs=[Model, Brand, CPUBrand, CPUBrandType, CPUHz, GPU, RAMType, RAM, RAMBus, Storage,
ScreenResolution, ScreenRatio, RefreshRate, ScreenSize, Battery, Weight],
queue=True,
)
clear_button = gr.ClearButton(components=[output], value="Clear")
if __name__ == "__main__":
demo.launch()
|