mahesh1209's picture
Update app.py
975a5b9 verified
# app.py
import gradio as gr
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import OneHotEncoder
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from io import StringIO
# Embedded dataset
data = """
age,income,education,marital_status,approved
25,50000,Bachelors,Single,1
45,120000,Masters,Married,1
22,30000,HighSchool,Single,0
35,80000,Bachelors,Divorced,1
29,40000,HighSchool,Single,0
"""
df = pd.read_csv(StringIO(data))
X = df.drop("approved", axis=1)
y = df["approved"]
# Preprocessing + Model
categorical = ["education", "marital_status"]
numeric = ["age", "income"]
preprocessor = ColumnTransformer([
("cat", OneHotEncoder(), categorical),
("num", "passthrough", numeric)
])
model = Pipeline([
("pre", preprocessor),
("clf", LogisticRegression(solver="liblinear", max_iter=200))
])
model.fit(X, y)
# Gradio UI
def predict_credit(age, income, education, marital_status):
df = pd.DataFrame([{
"age": age,
"income": income,
"education": education,
"marital_status": marital_status
}])
pred = model.predict(df)[0]
return "βœ… Approved" if pred == 1 else "❌ Rejected"
demo = gr.Interface(
fn=predict_credit,
inputs=[
gr.Number(label="Age"),
gr.Number(label="Income"),
gr.Dropdown(["HighSchool", "Bachelors", "Masters"], label="Education"),
gr.Dropdown(["Single", "Married", "Divorced"], label="Marital Status")
],
outputs="text",
title="Credit Card Approval Predictor",
description="Predict whether a credit card application should be approved based on applicant details."
)
demo.launch()