Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pickle
|
3 |
+
import pandas as pd
|
4 |
+
from sklearn.preprocessing import StandardScaler
|
5 |
+
from sklearn.cluster import KMeans
|
6 |
+
|
7 |
+
with open('kmeans_model.pkl', 'rb') as file:
|
8 |
+
kmeans = pickle.load(file)
|
9 |
+
with open('scaler.pkl', 'rb') as file:
|
10 |
+
scaler = pickle.load(file)
|
11 |
+
|
12 |
+
def predict_spending_score(annual_income, family_size, work_experience):
|
13 |
+
df = pd.DataFrame({
|
14 |
+
'Annual Income ($)': [annual_income],
|
15 |
+
'Family Size': [family_size],
|
16 |
+
'Work Experience': [work_experience]
|
17 |
+
})
|
18 |
+
|
19 |
+
df['Family_Income_Product'] = df['Family Size'] * df['Annual Income ($)']
|
20 |
+
df['Family_Income_Ratio'] = df['Family Size'] / (df['Annual Income ($)'] + 1e-5)
|
21 |
+
|
22 |
+
features = df[['Annual Income ($)', 'Family Size', 'Family_Income_Product', 'Family_Income_Ratio']]
|
23 |
+
features_scaled = scaler.transform(features)
|
24 |
+
|
25 |
+
|
26 |
+
cluster = kmeans.predict(features_scaled)
|
27 |
+
return f'Cluster: {int(cluster[0])}'
|
28 |
+
|
29 |
+
iface = gr.Interface(
|
30 |
+
fn=predict_spending_score,
|
31 |
+
inputs=[
|
32 |
+
gr.Number(label="Annual Income ($)", default=50000),
|
33 |
+
gr.Number(label="Family Size", default=2),
|
34 |
+
gr.Number(label="Work Experience (years)", default=5)
|
35 |
+
],
|
36 |
+
outputs="text",
|
37 |
+
live=True
|
38 |
+
)
|
39 |
+
|
40 |
+
iface.launch()
|