File size: 1,239 Bytes
533ae68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pickle
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans

with open('kmeans_model.pkl', 'rb') as file:
    kmeans = pickle.load(file)
with open('scaler.pkl', 'rb') as file:
    scaler = pickle.load(file)

def predict_spending_score(annual_income, family_size, work_experience):
    df = pd.DataFrame({
        'Annual Income ($)': [annual_income],
        'Family Size': [family_size],
        'Work Experience': [work_experience]
    })
    
    df['Family_Income_Product'] = df['Family Size'] * df['Annual Income ($)']
    df['Family_Income_Ratio'] = df['Family Size'] / (df['Annual Income ($)'] + 1e-5)
    
    features = df[['Annual Income ($)', 'Family Size', 'Family_Income_Product', 'Family_Income_Ratio']]
    features_scaled = scaler.transform(features)
    
 
    cluster = kmeans.predict(features_scaled)
    return f'Cluster: {int(cluster[0])}'

iface = gr.Interface(
    fn=predict_spending_score,
    inputs=[
        gr.Number(label="Annual Income ($)", default=50000),
        gr.Number(label="Family Size", default=2),
        gr.Number(label="Work Experience (years)", default=5)
    ],
    outputs="text",
    live=True
)

iface.launch()