-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStreamlit_App.py
191 lines (181 loc) · 10.1 KB
/
Streamlit_App.py
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import streamlit as st
import pickle as pk
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#Set page configuration for the webpage
st.set_page_config(page_title="Smartphone Price Predictor", page_icon="📱", layout="centered")
#import the datasets and the trained model
db1 = pd.read_csv('phone_data.csv')
db2 = pd.read_csv("dfAfterCleaning.csv")
pipe = pk.load(open("trained_model.pkl", "rb"))
#Set title to the webpage
st.title("Smartphone Price Predictor")
#Set tabs for the webpage
tab1, tab2 = st.tabs(["Prediction", "About Us"])
#Sort the brands alphabetically
brands = sorted(db1['Brand'].unique())
storage = sorted(db2['storage'].unique(), key=int)
ram = sorted(db2['Ram'].unique(), key=int)
inches = sorted(db2['Inches'].unique(), key=float)
battery = sorted(db2['Battery'].unique(), key=int)
cam = sorted(db2['Front Camera'].unique(), key=int)
OS = ['Android', 'iOS', 'Another OS']
char=['Price distribution','Categorically','Continuously']
#Deploy the tabs
with tab1:
st.header("Price Prediction")
st.caption("Select the specifications of the smartphone to predict the price and display the statistics")
#Insert Selectboxs for the user to select the smartphone specefications divided into two columns
col1, col2 = st.columns(2, gap="large")
with col1:
brandSelected = st.selectbox("Select brand", brands)
storageSelected = st.selectbox("Select Storage (in gb)", storage)
ramSelected = st.selectbox("Select Ram (in gb)", ram)
incheSeleted = st.selectbox("Select Screen Size (in inches)", inches)
with col2:
resolutionSelected = st.selectbox("Select Screen Resolution", ['1080x1620', '1080x1920', '1080x2400', '1080x2412', '1080x2460',
'1170x2532', '1440x720', '1520x720', '1560x720', '1600x720',
'160x120', '160x128', '1612x720', '1640x720', '1650x720',
'1792x828', '2176x1812', '2208x1768', '2340x1080', '2376x1080',
'2388x1080', '2400x1080', '2404x1080', '2408x1080', '2412x1080',
'2436x1125', '2460x1080', '2532x1170', '2556x1179', '2640x1080',
'2778x1284', '277x1284', '2796x1290', '3040x1440', '3088x1440',
'3120x1440', '3200x1440', '320x240', '480x854', '720x1280',
'720x1560', '720x1600', '720x1612', '720x1640', '720x1650',
'828x1792', '900x1600', '960x480'])
batterySelected = st.selectbox("Select Battery Capacity (in mah)", battery)
camSelected = st.selectbox("Select Front Camera (in mp)", cam)
OSSelected = st.selectbox("Select Operating System", OS)
col1, col2, col3 = st.columns([1,2,1])
# Within the first column
with col1:
pass
with col2:
chartSelect = st.selectbox("Set Chart type", char)
# Display the selected OS
with col3:
pass
#Insert buttons for the user to predict the price and display the statistics
col1, col2, col3, col4 = st.columns([1,1,1,0.8], gap="small")
with col1:
pass
with col2:
predictButton = st.button("Predict Price", type="primary")
with col3:
statsButton = st.button("Display Statistics", type="secondary")
with col4:
pass
#configure variables and np arrays to be used in the prediction query
widthSelected = int(resolutionSelected.split('x')[1])
heightSelected = int(resolutionSelected.split('x')[0])
osIndex = OS.index(OSSelected)
OSSelected = np.zeros((len(OS),), dtype=int)
OSSelected[osIndex] = 1
brandIndex = brands.index(brandSelected)
brandSelected = np.zeros((len(brands),), dtype=int)
brandSelected[brandIndex] = 1
#Function to search for the recommended smartphones
def search_phone(storageSelected, ramSelected, camSelected, batterySelected):
result = db1.loc[(db1['storage'] == str(storageSelected)) & (db1['Ram'] == str(ramSelected)) & (db1['Front Camera'] == str(camSelected)) & (db1['Battery'] == str(batterySelected))]
phones = result.loc[:, ['title', 'storage', 'Ram', 'Front Camera', 'Battery']]
phones.drop_duplicates(inplace=True)
return phones
#Function to add links to the recommended smartphones
def add_links(title):
title = title.replace(' ', '+')
link = f'https://www.amazon.eg/s?k={title}'
return link
#insert statsButton & predictButton functionality
if statsButton:
cat = []
con = []
for i in db2.columns:
if db2[i].nunique() <= 9 and db2[i].nunique() > 2 or i=='category_column':
cat.append(i)
else:
con.append(i)
if str(chartSelect)==char[0]:
st.subheader("Line Chart (Price distribution)")
st.divider()
st.line_chart(
db1['price in EGY'].value_counts(),
y='price in EGY'
)
elif str(chartSelect)==char[1]:
st.subheader("Bar Chart (Categorically)")
st.divider()
for i in cat:
st.bar_chart(db2[f"{i}"].value_counts(),y=i)
else:
st.subheader("Bar Chart (Continuously)")
st.divider()
for i in ['OS','Brand','title','Front Camera','Inches','Resolutions']:
st.bar_chart(db1[f"{i}"].value_counts(),y=i)
# for i in cat:
# # st.bar_chart(db2[f"{i}"][0:100], y=i)
# st.subheader(i+"_hist")
# fig, ax = plt.subplots()
# ax.hist(db2[f"{i}"], bins=20)
# st.pyplot(fig)
#
if predictButton:
#make a query array and pass it to the model to predict the price
query = np.array([storageSelected, ramSelected, camSelected, batterySelected, incheSeleted, widthSelected, heightSelected])
query = np.insert(query, 5, brandSelected)
query = np.insert(query, -2, OSSelected)
query = query.reshape(1, -1)
predictedPrice = pipe.predict(query)
#Display the predicted price range
if(predictedPrice[0] == 1):
st.subheader("The predicted price range is: 440 - 4463 EGP")
elif(predictedPrice[0] == 2):
st.subheader("The predicted price range is: 4463 - 7199 EGP")
elif(predictedPrice[0] == 3):
st.subheader("The predicted price range is: 7199 - 17399 EGP")
elif(predictedPrice[0] == 4):
st.subheader("The predicted price range is: 17399 - 80000 EGP")
#insert divider and subheader
st.divider()
st.subheader("Recommended Smartphones")
#Call the search_phone function to get the recommended smartphones
phones = search_phone(storageSelected, ramSelected, camSelected, batterySelected)
#Insert links to the recommended smartphones
phones['Link'] = phones['title'].apply(add_links)
#Display the recommended smartphones DataFrame in HTML table
phones = phones.to_html(index=False, justify='center', render_links=True)
st.write(phones, unsafe_allow_html=True)
with tab2:
#Insert information about the project
st.subheader("About Our Smartphone Price Prediction Project")
st.write("Welcome to our Smartphone Price Prediction project! We are a team of passionate programmers and university students with a shared interest in machine learning and technology.")
st.subheader("Our Mission")
st.write("Our mission is to simplify the process of buying a smartphone by providing users with a reliable price prediction tool. We understand that choosing the right smartphone can be a daunting task, given the plethora of options available in the market. That's why we've leveraged the power of machine learning to create a user-friendly solution that predicts smartphone prices based on their specifications.")
st.subheader("How It Works?")
st.write("Our project uses advanced machine learning algorithms to analyze and predict smartphone prices. Users can input the specifications of the smartphone they are interested in, and our system processes this data to generate accurate price estimates. Additionally, we provide detailed statistics and insights about the predicted price, helping users make informed decisions.")
st.subheader("Why Choose Us?")
st.markdown('''• **Accuracy**: Our machine learning models have been trained on extensive datasets, ensuring high accuracy in price predictions.''')
st.markdown('''• **User-Friendly**: We have designed our platform to be intuitive and easy to use, making it accessible to both tech-savvy individuals and those new to the world of technology.''')
st.markdown('''• **Constant Improvement**: We are committed to continuous improvement and regularly update our models to reflect the latest market trends and smartphone releases.''')
st.subheader("Meet Our Team")
col1, col2, col3, col4, col5 = st.columns(5, gap="small")
#Insert images and names of the team members
with col1:
st.image("photo.png", width=150)
st.markdown("<h3 style='text-align: center; color: white;'>Ahmed Shaaban</h1>", unsafe_allow_html=True)
with col2:
st.image("photo.png", width=150)
st.markdown("<h3 style='text-align: center; color: white;'>Khaled Farouq</h1>", unsafe_allow_html=True)
with col3:
st.image("photo.png", width=150)
st.markdown("<h3 style='text-align: center; color: white;'>Emad Rabea</h1>", unsafe_allow_html=True)
with col4:
st.image("photo.png", width=150)
st.markdown("<h3 style='text-align: center; color: white;'>Omar Hussien</h1>", unsafe_allow_html=True)
with col5:
st.image("photo.png", width=150)
st.markdown("<h3 style='text-align: center; color: white;'>Mohamed Ahmed</h1>", unsafe_allow_html=True)
#Insert divider
st.divider()
#Insert footer
st.markdown(f'''<img src="https://github.com/fluidicon.png" alt="GitHub Icon" width="30" height="30"> Github Source Code: <a href={'https://github.com/Eng-Omar-Hussein/Predict_Mobile_Price'}>Github|Predict-Mobile-Price</a>''', unsafe_allow_html=True)