dinhdat1110 commited on
Commit
6a112d0
·
1 Parent(s): da64bd6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +126 -0
app.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import numpy as np
4
+ import pickle
5
+
6
+ categorical_features = ['Manufacturer', 'CPU', 'RAM Type', "Screen Resolution"]
7
+ numerical_features = ['CPU Speed (GHz)', 'RAM (GB)', 'Bus (MHz)', 'Storage (GB)', 'CPU brand modifier',
8
+ 'Screen Size (inch)', 'Refresh Rate (Hz)', 'Weight (kg)', 'Battery']
9
+ label = ['Price (VND)']
10
+
11
+ with open("./checkpoint/ohe.pkl", "rb") as f:
12
+ ohe = pickle.load(f)
13
+
14
+
15
+ def load_model(model):
16
+ if model == "XGBRegressor":
17
+ with open("./checkpoint/XGBRegressor.pkl", "rb") as f:
18
+ pred_model = pickle.load(f)
19
+ elif model == "RandomForestRegressor":
20
+ with open("./checkpoint/RandomForestRegressor.pkl", "rb") as f:
21
+ pred_model = pickle.load(f)
22
+ elif model == "GradientBoostingRegressor":
23
+ with open("./checkpoint/GradientBoostingRegressor.pkl", "rb") as f:
24
+ pred_model = pickle.load(f)
25
+ return pred_model
26
+
27
+
28
+ def predict(brand, cpu, cpu_brand_type, cpu_hz, ram_type, ram, ram_bus,
29
+ screen_resolution, refresh_rate, screen_size,
30
+ storage, battery, weight, model
31
+ ):
32
+ pred_model = load_model(model)
33
+ cate_data = {
34
+ "Manufacturer": [brand],
35
+ "CPU": [cpu],
36
+ "RAM Type": [ram_type],
37
+ "Screen Resolution": [screen_resolution]
38
+ }
39
+ nume_data = {
40
+ "CPU Speed (GHz)": [cpu_hz],
41
+ "RAM (GB)": [ram],
42
+ "Bus (MHz)": [ram_bus],
43
+ "CPU brand modifier": [cpu_brand_type],
44
+ "Screen Size (inch)": [screen_size],
45
+ "Refresh Rate (Hz)": [refresh_rate],
46
+ "Storage (GB)": [storage],
47
+ "Battery": [battery],
48
+ "Weight (kg)": [weight]
49
+ }
50
+ cate_data = pd.DataFrame(cate_data)
51
+ nume_data = pd.DataFrame(nume_data)
52
+ cate_data = ohe.transform(cate_data)
53
+ cate_data = pd.DataFrame(cate_data, columns=ohe.get_feature_names_out())
54
+ data = pd.concat([cate_data, nume_data], axis=1)
55
+ return round(float(pred_model.predict(np.array(data))[0]), 2)
56
+
57
+
58
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="green")) as demo:
59
+ # add gr title to the middle of the page
60
+ gr.Markdown("# Laptop Price Prediction")
61
+
62
+ with gr.Row():
63
+ model = gr.Dropdown(
64
+ label="Model",
65
+ choices=["XGBRegressor", "RandomForestRegressor", "GradientBoostingRegressor"],
66
+ value="XGBRegressor",
67
+ )
68
+
69
+ Brand = gr.Radio(
70
+ label="Brand",
71
+ choices=['acer', 'asus', 'dell', 'hp', 'lenovo', 'lg', 'msi'],
72
+ value='acer'
73
+ )
74
+ gr.Markdown("## **CPU**")
75
+ with gr.Row():
76
+
77
+ CPUBrand = gr.Dropdown(label="CPU", choices=[
78
+ "AMD Gen 4.0th", "AMD Gen 5.0th", "AMD Gen 6.0th", "AMD Gen 7.0th",
79
+ "Intel Gen 11.0th", "Intel Gen 12.0th", "Intel Gen 13.0th"],
80
+ value="Intel Gen 12.0th"
81
+ )
82
+ CPUHz = gr.Slider(label="CPU Speed (GHz)", minimum=1.0, maximum=5.0, step=0.1, value=2.0, interactive=True)
83
+ CPUBrandType = gr.Radio(label="CPU Type", choices=[3, 5, 7, 9], value=7)
84
+
85
+ gr.Markdown("## **RAM**")
86
+ with gr.Row():
87
+ RAMType = gr.Dropdown(
88
+ label="RAM Type",
89
+ choices=["DDR4", "LPDDR4", "LPDDR4X", "DDR5", "LPDDR5", "LPDDR5X"],
90
+ value="DDR5"
91
+ )
92
+ RAMBus = gr.Slider(label="Bus (MHz)", minimum=1600, maximum=6400, step=400, value=3200, interactive=True)
93
+ RAM = gr.Radio(label="RAM (GB)", choices=[8, 16, 32, 64, 128], value=16)
94
+ gr.Markdown("## **Screen**")
95
+ with gr.Row():
96
+ ScreenResolution = gr.Dropdown(
97
+ label="Screen Resolution",
98
+ choices=["1366x768", "1920x1080", "1920x1200", "2560x1440", "2560x1600", "3840x2160"],
99
+ value="1920x1080"
100
+ )
101
+ ScreenSize = gr.Radio(label="Screen Size (inch)", choices=[13.3, 14.0, 15.6, 17.3], value=15.6)
102
+ RefreshRate = gr.Radio(label="Refresh Rate (Hz)", choices=[60, 120, 144, 240], value=60)
103
+
104
+ gr.Markdown("## **Other Features**")
105
+ with gr.Row():
106
+ Battery = gr.Radio(label="Battery (Wh)", choices=[40, 50, 60, 70, 80])
107
+ Weight = gr.Radio(label="Weight (kg)", choices=[1.0, 1.5, 2.0, 2.5, 3.0])
108
+ Storage = gr.Radio(label="Storage (GB)", choices=[256, 512, 1024, 2048], value=512)
109
+ # Output Prediction
110
+ gr.Markdown("## **Prediction**")
111
+ with gr.Row():
112
+
113
+ output = gr.Number(label="Prediction (Million VND)", info="Click Submit to predict")
114
+ with gr.Row():
115
+ submit_button = gr.Button("Submit")
116
+ submit_button.click(fn=predict,
117
+ outputs=output,
118
+ inputs=[Brand, CPUBrand, CPUBrandType, CPUHz, RAMType, RAM, RAMBus,
119
+ ScreenResolution, RefreshRate, ScreenSize, Storage, Battery, Weight, model
120
+ ],
121
+ queue=True,
122
+ )
123
+ clear_button = gr.ClearButton(components=[output], value="Clear")
124
+
125
+ if __name__ == "__main__":
126
+ demo.launch(max_threads=50, debug=True, prevent_thread_lock=True, show_error=True, share=True)