Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pickle | |
| import numpy as np | |
| # Load model and scaler | |
| with open("log_reg.pkl", "rb") as f: | |
| model = pickle.load(f) | |
| with open("scaler.pkl", "rb") as f: | |
| scaler = pickle.load(f) | |
| def predict_creditworthiness(age, income, debt, payment_history, employment_status): | |
| try: | |
| debt_to_income = debt / (income + 1) | |
| payment_map = {"Bad": 0, "Average": 1, "Good": 2} | |
| payment_num = payment_map.get(payment_history, 1) | |
| employed_num = 1 if employment_status == "Employed" else 0 | |
| input_data = np.array([[age, income, debt, debt_to_income, payment_num, employed_num]]) | |
| # --- Debug prints --- | |
| print("Input data", input_data) | |
| print("Input shape", input_data.shape) | |
| print("Scaler expects", scaler.mean_.shape) # Should be (6,) | |
| input_scaled = scaler.transform(input_data) | |
| proba = model.predict_proba(input_scaled)[0, 1] | |
| pred_class = model.predict(input_scaled)[0] | |
| credit_status = "Good Credit" if pred_class == 1 else "Bad Credit" | |
| return f"Prediction: {credit_status} (Probability of good credit: {proba:.2f})" | |
| except Exception as e: | |
| # Print error for debug | |
| print("Prediction error:", str(e)) | |
| return f"Error: {str(e)}" | |
| iface = gr.Interface( | |
| fn=predict_creditworthiness, | |
| inputs=[ | |
| gr.Number(label="Age", value=30, precision=0), | |
| gr.Number(label="Income", value=50000, precision=2), | |
| gr.Number(label="Debt", value=5000, precision=2), | |
| gr.Dropdown(label="Payment History", choices=["Bad", "Average", "Good"], value="Average"), | |
| gr.Radio(label="Employment Status", choices=["Employed", "Unemployed"], value="Employed"), | |
| ], | |
| outputs="text", | |
| title="Credit Scoring Model (Logistic Regression)", | |
| description="Enter your financial details to predict creditworthiness." | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() | |