SmartPulse / app.py
hillol7's picture
Create app.py
533ae68 verified
raw
history blame
1.24 kB
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()