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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
os.system("pip install pandas")
|
| 5 |
+
os.system("pip install gradio")
|
| 6 |
+
os.system("pip install scikit-learn==1.3.2 matplotlib")
|
| 7 |
+
|
| 8 |
+
import gradio as gr
|
| 9 |
+
import pandas as pd
|
| 10 |
+
import pickle
|
| 11 |
+
|
| 12 |
+
connections_list = ['2G', '3G', '4G', '5G', 'WIFI', 'BLUETOOTH', 'GPS', 'NFC', 'RADIO', 'FACE_UNLOCK', 'SINGLE_SOM', 'USB_TYPE_C', 'OTG', 'USB', 'TECH', 'DUAL_SIM', 'ETHERNET']
|
| 13 |
+
colors_list = ['BLACK', 'WHITE', 'BLUE', 'PURPLE', 'GOLD', 'GREEN', 'YELLOW', 'RED', 'ORANGE', 'PEACH', 'SUNLIGHT', 'STARLIGHT', 'NAVY', 'KHAKI', 'HAZEL', 'RAINFOREST', 'CHARCOAL', 'SNOW', 'SEA', 'CORAL', 'COPPER', 'BROWN', 'TRANSPARENT', 'MINT', 'GRAPHITE', 'CREAM', 'LAVENDER', 'GRAY', 'LIME', 'CYAN', 'INDIGO', 'TANGERINE', 'VIOLET', 'SILVER', 'MIDNIGHT', 'PLATINUM', 'AURORA', 'PORCELAIN', 'OBSIDIAN', 'CHALK', 'SAGE', 'OLIVE', 'DUSK', 'TURQUOISE', 'SAND', 'NEON', 'VOILET', 'TITANIUM', 'NIGHT', 'STEEL', 'MIST', 'PEARL', 'GRADATION', 'TWILIGHT.', 'LEMONGRASS', 'GREY', 'PINK', 'BRONZE', 'NO_COLOR', 'OTHER_COLORS']
|
| 14 |
+
processors_list = ['UNISOC', 'QUALCOMM', 'MEDIATEK', 'OCTA_CORE', 'QUAD_CORE', 'HEXA_CORE', 'EXYNOS', 'GOOGLE_TENSOR', 'APPLE_M_CHIP', 'APPLE_A_CHIP', 'HUAWEI', 'SAMSUNG', 'INTEL', 'DIMENSITY', 'OTHERS']
|
| 15 |
+
brands_list = ['XIAOMI', 'SAMSUNG', 'TECNO', 'INFINIX', 'OPPO', 'APPLE', 'NOKIA', 'ONEPLUS', 'HUAWEI', 'REALME', 'XTIGI', 'ITEL', 'VIVO', 'GOOGLE_PIXEL', 'ASUS', 'LENOVO', 'ZTE', 'OTHERS']
|
| 16 |
+
model_column_list = ['ram', 'internal_storage', 'battery', 'main_camera', 'front_camera', 'display', 'connectivity_2G', 'connectivity_3G', 'connectivity_4G', 'connectivity_5G', 'connectivity_BLUETOOTH', 'connectivity_DUAL_SIM', 'connectivity_ETHERNET', 'connectivity_FACE_UNLOCK', 'connectivity_GPS', 'connectivity_NFC', 'connectivity_OTG', 'connectivity_RADIO', 'connectivity_SINGLE_SOM', 'connectivity_TECH', 'connectivity_USB', 'connectivity_USB_TYPE_C', 'connectivity_WIFI', 'colors_AURORA', 'colors_BLACK', 'colors_BLUE', 'colors_BRONZE', 'colors_BROWN', 'colors_CHALK', 'colors_CHARCOAL', 'colors_COPPER', 'colors_CORAL', 'colors_CREAM', 'colors_CYAN', 'colors_DUSK', 'colors_GOLD', 'colors_GRADATION', 'colors_GRAPHITE', 'colors_GRAY', 'colors_GREEN', 'colors_GREY', 'colors_HAZEL', 'colors_INDIGO', 'colors_KHAKI', 'colors_LAVENDER', 'colors_LEMONGRASS', 'colors_LIME', 'colors_MIDNIGHT', 'colors_MINT', 'colors_MIST', 'colors_NAVY', 'colors_NEON', 'colors_NIGHT', 'colors_NO_COLOR', 'colors_OBSIDIAN', 'colors_OLIVE', 'colors_ORANGE', 'colors_OTHER_COLORS', 'colors_PEACH', 'colors_PEARL', 'colors_PINK', 'colors_PLATINUM', 'colors_PORCELAIN', 'colors_PURPLE', 'colors_RAINFOREST', 'colors_RED', 'colors_SAGE', 'colors_SAND', 'colors_SEA', 'colors_SILVER', 'colors_SNOW', 'colors_STARLIGHT', 'colors_STEEL', 'colors_SUNLIGHT', 'colors_TANGERINE', 'colors_TITANIUM', 'colors_TRANSPARENT', 'colors_TURQUOISE', 'colors_TWILIGHT.', 'colors_VIOLET', 'colors_VOILET', 'colors_WHITE', 'colors_YELLOW', 'brand_APPLE', 'brand_ASUS', 'brand_GOOGLE_PIXEL', 'brand_HUAWEI', 'brand_INFINIX', 'brand_ITEL', 'brand_LENOVO', 'brand_NOKIA', 'brand_NO_BRAND', 'brand_ONEPLUS', 'brand_OPPO', 'brand_OTHERS', 'brand_REALME', 'brand_SAMSUNG', 'brand_TECNO', 'brand_VIVO', 'brand_XIAOMI', 'brand_XTIGI', 'brand_ZTE', 'processor_APPLE_A_CHIP', 'processor_APPLE_M_CHIP', 'processor_DIMENSITY', 'processor_EXYNOS', 'processor_GOOGLE_TENSOR', 'processor_HEXA_CORE', 'processor_HUAWEI', 'processor_INTEL', 'processor_MEDIATEK', 'processor_OCTA_CORE', 'processor_OTHERS', 'processor_QUAD_CORE', 'processor_QUALCOMM', 'processor_SAMSUNG', 'processor_UNISOC']
|
| 17 |
+
with open('./model_results.pkl', 'rb') as f:
|
| 18 |
+
model_results = pickle.load(f)
|
| 19 |
+
|
| 20 |
+
def prediction_factory(launch=True):
|
| 21 |
+
import traceback
|
| 22 |
+
import pandas as pd
|
| 23 |
+
import gradio as gr
|
| 24 |
+
import json
|
| 25 |
+
|
| 26 |
+
def fn(model, data: dict):
|
| 27 |
+
try:
|
| 28 |
+
data_df = pd.DataFrame([data])
|
| 29 |
+
onehot_data_df = pd.get_dummies(
|
| 30 |
+
data_df.explode('connectivity'),
|
| 31 |
+
columns=['connectivity', 'colors', 'brand', 'processor']).copy()
|
| 32 |
+
columns_not_in_data = [col for col in model_column_list if col not in onehot_data_df.columns]
|
| 33 |
+
onehot_data_df[columns_not_in_data] = [0] * len(columns_not_in_data) # Add the missing column with the default value
|
| 34 |
+
onehot_data_df = onehot_data_df[model_column_list] # To ensure the order of columns is the same as the training set
|
| 35 |
+
[predicted_price,*_] = model.predict(onehot_data_df)
|
| 36 |
+
return f"Predicted selling price: {predicted_price:,.0f} Ksh"
|
| 37 |
+
except Exception as e:
|
| 38 |
+
error = traceback.format_exc()
|
| 39 |
+
return error
|
| 40 |
+
if not launch:
|
| 41 |
+
return fn
|
| 42 |
+
|
| 43 |
+
###########################################################
|
| 44 |
+
################### Start of Gradio setup #################
|
| 45 |
+
###########################################################
|
| 46 |
+
title = "Mobile Phone Price Prediction"
|
| 47 |
+
css = '''
|
| 48 |
+
.prediction-output {
|
| 49 |
+
display: flex;
|
| 50 |
+
align-items: center;
|
| 51 |
+
justify-content: center;
|
| 52 |
+
justify-items: center;
|
| 53 |
+
}'''
|
| 54 |
+
formatted_model_results = [(f'{name} - rmse: {model_data["rmse"]}', model_data['model']) for name, model_data in model_results]
|
| 55 |
+
with gr.Blocks(title = title, css = css) as app:
|
| 56 |
+
with gr.Row():
|
| 57 |
+
with gr.Column():
|
| 58 |
+
gr.Markdown(f"# {title}")
|
| 59 |
+
gr.Markdown("### Fill in the details of the mobile phone to get the predicted price")
|
| 60 |
+
model_name = gr.Dropdown(
|
| 61 |
+
choices = [name for name,_ in formatted_model_results],
|
| 62 |
+
value = formatted_model_results[0][0],
|
| 63 |
+
label = "Model",
|
| 64 |
+
info = "Select the model to use for the prediction")
|
| 65 |
+
connectivity = gr.Dropdown(
|
| 66 |
+
choices = connections_list,
|
| 67 |
+
value = [],
|
| 68 |
+
multiselect = True,
|
| 69 |
+
label = "Connectivity",
|
| 70 |
+
info = "Select the connectivity options of the mobile phone")
|
| 71 |
+
colors = gr.Dropdown(
|
| 72 |
+
choices = colors_list,
|
| 73 |
+
value = '',
|
| 74 |
+
label = "Colors",
|
| 75 |
+
info = "Select the colors of the mobile phone")
|
| 76 |
+
processor = gr.Dropdown(
|
| 77 |
+
choices = processors_list,
|
| 78 |
+
value = '',
|
| 79 |
+
label = "Processor",
|
| 80 |
+
info = "Select the processor of the mobile phone")
|
| 81 |
+
brand = gr.Dropdown(
|
| 82 |
+
choices = brands_list,
|
| 83 |
+
value = '',
|
| 84 |
+
label = "Brand",
|
| 85 |
+
info = "Select the brand of the mobile phone")
|
| 86 |
+
ram = gr.Slider(
|
| 87 |
+
0,
|
| 88 |
+
100,
|
| 89 |
+
label="RAM",
|
| 90 |
+
info="Select the RAM of the mobile phone (in GB)")
|
| 91 |
+
internal_storage= gr.Slider(
|
| 92 |
+
0,
|
| 93 |
+
1000,
|
| 94 |
+
label="Internal Storage",
|
| 95 |
+
info="Select the internal storage of the mobile phone (in GB)")
|
| 96 |
+
battery = gr.Slider(
|
| 97 |
+
0,
|
| 98 |
+
10000,
|
| 99 |
+
label="Battery",
|
| 100 |
+
info="Select the battery capacity of the mobile phone (in mAh)")
|
| 101 |
+
main_camera = gr.Slider(
|
| 102 |
+
0,
|
| 103 |
+
100,
|
| 104 |
+
label="Main Camera",
|
| 105 |
+
info="Select the main camera capacity of the mobile phone (in MP)")
|
| 106 |
+
front_camera = gr.Slider(
|
| 107 |
+
0,
|
| 108 |
+
100,
|
| 109 |
+
label="Front Camera",
|
| 110 |
+
info="Select the front camera capacity of the mobile phone (in MP)")
|
| 111 |
+
display = gr.Slider(
|
| 112 |
+
0,
|
| 113 |
+
10,
|
| 114 |
+
label="Display",
|
| 115 |
+
info="Select the display size of the mobile phone (in inches)")
|
| 116 |
+
def fn_proxy(*args):
|
| 117 |
+
# convert args to dict
|
| 118 |
+
keys = ['connectivity', 'colors', 'processor', 'brand', 'ram', 'internal_storage', 'battery', 'main_camera', 'front_camera', 'display']
|
| 119 |
+
data = {key: value for key, value in zip(keys, args)}
|
| 120 |
+
data['ram'] *= 1024 # convert to MB
|
| 121 |
+
data['internal_storage'] *= 1024
|
| 122 |
+
model_name_ = args[-1]
|
| 123 |
+
model = [model for name, model in formatted_model_results if name == model_name_][0]
|
| 124 |
+
result = fn(model, data)
|
| 125 |
+
return f'''<div class="prediction-output">
|
| 126 |
+
<div>
|
| 127 |
+
<h1>Model Prediction</h1>
|
| 128 |
+
<h3>{result}</h3>
|
| 129 |
+
<h3>Model Name: {model_name_}</h3>
|
| 130 |
+
<pre>{json.dumps(data, indent = 4)}</pre>
|
| 131 |
+
</div>
|
| 132 |
+
</div>
|
| 133 |
+
'''
|
| 134 |
+
with gr.Column():
|
| 135 |
+
value = fn_proxy(
|
| 136 |
+
connectivity.value,
|
| 137 |
+
colors.value,
|
| 138 |
+
processor.value,
|
| 139 |
+
brand.value,
|
| 140 |
+
ram.value,
|
| 141 |
+
internal_storage.value,
|
| 142 |
+
battery.value,
|
| 143 |
+
main_camera.value,
|
| 144 |
+
front_camera.value,
|
| 145 |
+
display.value,
|
| 146 |
+
model_name.value)
|
| 147 |
+
prediction = gr.HTML(value=value, label="Prediction")
|
| 148 |
+
model_name.change(
|
| 149 |
+
fn=fn_proxy,
|
| 150 |
+
inputs=[connectivity, colors, processor, brand, ram, internal_storage, battery, main_camera, front_camera, display, model_name],
|
| 151 |
+
outputs=[prediction])
|
| 152 |
+
connectivity.change(
|
| 153 |
+
fn=fn_proxy,
|
| 154 |
+
inputs=[connectivity, colors, processor, brand, ram, internal_storage, battery, main_camera, front_camera, display, model_name],
|
| 155 |
+
outputs=[prediction])
|
| 156 |
+
colors.change(
|
| 157 |
+
fn=fn_proxy,
|
| 158 |
+
inputs=[connectivity, colors, processor, brand, ram, internal_storage, battery, main_camera, front_camera, display, model_name],
|
| 159 |
+
outputs=[prediction])
|
| 160 |
+
processor.change(
|
| 161 |
+
fn=fn_proxy,
|
| 162 |
+
inputs=[connectivity, colors, processor, brand, ram, internal_storage, battery, main_camera, front_camera, display, model_name],
|
| 163 |
+
outputs=[prediction])
|
| 164 |
+
brand.change(
|
| 165 |
+
fn=fn_proxy,
|
| 166 |
+
inputs=[connectivity, colors, processor, brand, ram, internal_storage, battery, main_camera, front_camera, display, model_name],
|
| 167 |
+
outputs=[prediction])
|
| 168 |
+
ram.change(
|
| 169 |
+
fn=fn_proxy,
|
| 170 |
+
inputs=[connectivity, colors, processor, brand, ram, internal_storage, battery, main_camera, front_camera, display, model_name],
|
| 171 |
+
outputs=[prediction])
|
| 172 |
+
internal_storage.change(
|
| 173 |
+
fn=fn_proxy,
|
| 174 |
+
inputs=[connectivity, colors, processor, brand, ram, internal_storage, battery, main_camera, front_camera, display, model_name],
|
| 175 |
+
outputs=[prediction])
|
| 176 |
+
battery.change(
|
| 177 |
+
fn=fn_proxy,
|
| 178 |
+
inputs=[connectivity, colors, processor, brand, ram, internal_storage, battery, main_camera, front_camera, display, model_name],
|
| 179 |
+
outputs=[prediction])
|
| 180 |
+
main_camera.change(
|
| 181 |
+
fn=fn_proxy,
|
| 182 |
+
inputs=[connectivity, colors, processor, brand, ram, internal_storage, battery, main_camera, front_camera, display, model_name],
|
| 183 |
+
outputs=[prediction])
|
| 184 |
+
front_camera.change(
|
| 185 |
+
fn=fn_proxy,
|
| 186 |
+
inputs=[connectivity, colors, processor, brand, ram, internal_storage, battery, main_camera, front_camera, display, model_name],
|
| 187 |
+
outputs=[prediction])
|
| 188 |
+
display.change(
|
| 189 |
+
fn=fn_proxy,
|
| 190 |
+
inputs=[connectivity, colors, processor, brand, ram, internal_storage, battery, main_camera, front_camera, display, model_name],
|
| 191 |
+
outputs=[prediction])
|
| 192 |
+
###########################################################
|
| 193 |
+
################### End of Gradio setup ###################
|
| 194 |
+
###########################################################
|
| 195 |
+
return app.launch
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
prediction_factory()()
|