Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import pandas as pd
|
4 |
+
from sklearn.model_selection import train_test_split
|
5 |
+
from sklearn.neighbors import KNeighborsClassifier
|
6 |
+
from sklearn.metrics import accuracy_score
|
7 |
+
|
8 |
+
# Load dataset
|
9 |
+
data = pd.read_csv('knn.csv')
|
10 |
+
|
11 |
+
# Separate features and target
|
12 |
+
x = data.drop('price_range', axis=1).values # Features
|
13 |
+
y = data['price_range'].values # Target
|
14 |
+
|
15 |
+
# Train-test split
|
16 |
+
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)
|
17 |
+
|
18 |
+
# Train the KNN classifier
|
19 |
+
knnClassifier = KNeighborsClassifier(n_neighbors=5, metric='minkowski', p=1)
|
20 |
+
knnClassifier.fit(x_train, y_train)
|
21 |
+
|
22 |
+
# Define the prediction function for Gradio
|
23 |
+
def predict_price_range(battery_power, blue, clock_speed, dual_sim, fc, four_g, int_memory, m_dep, mobile_wt, n_cores, pc, px_height, px_width, ram, sc_h, sc_w, talk_time, three_g, touch_screen, wifi):
|
24 |
+
# Prepare the input for prediction
|
25 |
+
input_data = np.array([[battery_power, blue, clock_speed, dual_sim, fc, four_g, int_memory, m_dep, mobile_wt, n_cores, pc, px_height, px_width, ram, sc_h, sc_w, talk_time, three_g, touch_screen, wifi]])
|
26 |
+
|
27 |
+
# Make prediction using the trained KNN model
|
28 |
+
prediction = knnClassifier.predict(input_data)
|
29 |
+
|
30 |
+
# Return the predicted price range
|
31 |
+
price_ranges = ['Low', 'Medium', 'High', 'Very High'] # Modify this according to your dataset
|
32 |
+
return f"Predicted Price Range: {price_ranges[prediction[0]]}"
|
33 |
+
|
34 |
+
# Create Gradio interface with all the features as inputs using the updated Gradio components
|
35 |
+
interface = gr.Interface(
|
36 |
+
fn=predict_price_range, # Function that does the prediction
|
37 |
+
inputs=[
|
38 |
+
gr.Slider(500, 2000, step=1, label="Battery Power"),
|
39 |
+
gr.Checkbox(label="Bluetooth"), # 1 for Yes, 0 for No
|
40 |
+
gr.Slider(0.5, 3.0, step=0.1, label="Clock Speed"),
|
41 |
+
gr.Checkbox(label="Dual SIM"), # 1 for Yes, 0 for No
|
42 |
+
gr.Slider(0, 20, step=1, label="Front Camera (FC)"),
|
43 |
+
gr.Checkbox(label="4G"), # 1 for Yes, 0 for No
|
44 |
+
gr.Slider(4, 64, step=1, label="Internal Memory (GB)"),
|
45 |
+
gr.Slider(0.1, 1.0, step=0.1, label="Mobile Depth (cm)"),
|
46 |
+
gr.Slider(80, 250, step=1, label="Mobile Weight (g)"),
|
47 |
+
gr.Slider(1, 8, step=1, label="Number of Cores"),
|
48 |
+
gr.Slider(0, 20, step=1, label="Primary Camera (PC)"),
|
49 |
+
gr.Slider(0, 1960, step=1, label="Pixel Height"),
|
50 |
+
gr.Slider(500, 2000, step=1, label="Pixel Width"),
|
51 |
+
gr.Slider(500, 4000, step=1, label="RAM"),
|
52 |
+
gr.Slider(5, 20, step=1, label="Screen Height (cm)"),
|
53 |
+
gr.Slider(0, 18, step=1, label="Screen Width (cm)"),
|
54 |
+
gr.Slider(2, 20, step=1, label="Talk Time (hours)"),
|
55 |
+
gr.Checkbox(label="3G"), # 1 for Yes, 0 for No
|
56 |
+
gr.Checkbox(label="Touch Screen"), # 1 for Yes, 0 for No
|
57 |
+
gr.Checkbox(label="WiFi"), # 1 for Yes, 0 for No
|
58 |
+
],
|
59 |
+
outputs=gr.Textbox(label="Predicted Price Range"), # Output format
|
60 |
+
title="Mobile Price Range Prediction",
|
61 |
+
description="Predict the price range of a mobile based on its specifications."
|
62 |
+
)
|
63 |
+
|
64 |
+
# Launch the Gradio app
|
65 |
+
interface.launch()
|