Update mobile.py
Browse files
mobile.py
CHANGED
|
@@ -2,8 +2,8 @@ import pandas as pd
|
|
| 2 |
from sklearn.model_selection import train_test_split
|
| 3 |
from sklearn.ensemble import RandomForestRegressor
|
| 4 |
from sklearn.preprocessing import LabelEncoder, MinMaxScaler
|
| 5 |
-
from flask import Flask, request, jsonify
|
| 6 |
import joblib
|
|
|
|
| 7 |
|
| 8 |
# Load the dataset
|
| 9 |
data = pd.read_csv('https://raw.githubusercontent.com/Senka2112/VIS2023-datasets/refs/heads/main/MobilePhonePrice.csv')
|
|
@@ -27,27 +27,29 @@ data[['Storage', 'RAM', 'Screen Size (inches)', 'Camera (MP)', 'Battery Capacity
|
|
| 27 |
X = data.drop('Price ($)', axis=1)
|
| 28 |
y = data['Price ($)']
|
| 29 |
|
| 30 |
-
# Split the dataset into training and testing sets
|
| 31 |
-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 32 |
-
|
| 33 |
# Train the model
|
| 34 |
model = RandomForestRegressor()
|
| 35 |
-
model.fit(
|
| 36 |
|
| 37 |
# Save the model and scaler
|
| 38 |
joblib.dump(model, 'mobile_price_model.pkl')
|
| 39 |
joblib.dump(scaler, 'scaler.pkl')
|
| 40 |
joblib.dump(label_encoders, 'label_encoders.pkl')
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
input_df = pd.DataFrame([input_data])
|
| 50 |
-
|
| 51 |
# Encode categorical variables
|
| 52 |
for column in ['Brand', 'Model']:
|
| 53 |
input_df[column] = label_encoders[column].transform(input_df[column])
|
|
@@ -57,7 +59,23 @@ def predict():
|
|
| 57 |
|
| 58 |
# Make prediction
|
| 59 |
prediction = model.predict(input_df)
|
| 60 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
app.run(debug=True)
|
|
|
|
| 2 |
from sklearn.model_selection import train_test_split
|
| 3 |
from sklearn.ensemble import RandomForestRegressor
|
| 4 |
from sklearn.preprocessing import LabelEncoder, MinMaxScaler
|
|
|
|
| 5 |
import joblib
|
| 6 |
+
import gradio as gr
|
| 7 |
|
| 8 |
# Load the dataset
|
| 9 |
data = pd.read_csv('https://raw.githubusercontent.com/Senka2112/VIS2023-datasets/refs/heads/main/MobilePhonePrice.csv')
|
|
|
|
| 27 |
X = data.drop('Price ($)', axis=1)
|
| 28 |
y = data['Price ($)']
|
| 29 |
|
|
|
|
|
|
|
|
|
|
| 30 |
# Train the model
|
| 31 |
model = RandomForestRegressor()
|
| 32 |
+
model.fit(X, y)
|
| 33 |
|
| 34 |
# Save the model and scaler
|
| 35 |
joblib.dump(model, 'mobile_price_model.pkl')
|
| 36 |
joblib.dump(scaler, 'scaler.pkl')
|
| 37 |
joblib.dump(label_encoders, 'label_encoders.pkl')
|
| 38 |
|
| 39 |
+
# Define the prediction function
|
| 40 |
+
def predict_price(brand, model_name, storage, ram, screen_size, camera, battery):
|
| 41 |
+
input_data = {
|
| 42 |
+
'Brand': brand,
|
| 43 |
+
'Model': model_name,
|
| 44 |
+
'Storage': storage,
|
| 45 |
+
'RAM': ram,
|
| 46 |
+
'Screen Size (inches)': screen_size,
|
| 47 |
+
'Camera (MP)': camera,
|
| 48 |
+
'Battery Capacity (mAh)': battery
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
input_df = pd.DataFrame([input_data])
|
| 52 |
+
|
| 53 |
# Encode categorical variables
|
| 54 |
for column in ['Brand', 'Model']:
|
| 55 |
input_df[column] = label_encoders[column].transform(input_df[column])
|
|
|
|
| 59 |
|
| 60 |
# Make prediction
|
| 61 |
prediction = model.predict(input_df)
|
| 62 |
+
return prediction[0]
|
| 63 |
+
|
| 64 |
+
# Create Gradio interface
|
| 65 |
+
iface = gr.Interface(
|
| 66 |
+
fn=predict_price,
|
| 67 |
+
inputs=[
|
| 68 |
+
gr.inputs.Dropdown(choices=data['Brand'].unique().tolist(), label="Brand"),
|
| 69 |
+
gr.inputs.Dropdown(choices=data['Model'].unique().tolist(), label="Model"),
|
| 70 |
+
gr.inputs.Slider(minimum=32, maximum=512, label="Storage (GB)"),
|
| 71 |
+
gr.inputs.Slider(minimum=2, maximum=16, label="RAM (GB)"),
|
| 72 |
+
gr.inputs.Slider(minimum=4.0, maximum=7.0, label="Screen Size (inches)"),
|
| 73 |
+
gr.inputs.Textbox(label="Camera (MP)"),
|
| 74 |
+
gr.inputs.Slider(minimum=1000, maximum=7000, label="Battery Capacity (mAh)")
|
| 75 |
+
],
|
| 76 |
+
outputs="number",
|
| 77 |
+
title="Mobile Phone Price Predictor",
|
| 78 |
+
description="Enter the specifications of the mobile phone to predict its price."
|
| 79 |
+
)
|
| 80 |
|
| 81 |
+
iface.launch()
|
|
|