File size: 4,264 Bytes
2114915
 
 
a1caf1d
 
 
 
7a6effd
a1caf1d
 
 
 
7a6effd
a1caf1d
 
 
 
 
 
 
7a6effd
 
 
a1caf1d
 
 
 
 
 
 
 
 
 
7a6effd
a1caf1d
 
 
7a6effd
a1caf1d
 
 
7a6effd
a1caf1d
7a6effd
a1caf1d
 
 
 
 
 
 
7a6effd
a1caf1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a6effd
a1caf1d
 
 
 
 
 
 
 
 
 
 
7a6effd
a1caf1d
 
 
 
 
 
 
 
 
7a6effd
a1caf1d
 
 
2114915
a1caf1d
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

import gradio as gr
import numpy as np
import pandas as pd
from joblib import load
from huggingface_hub import hf_hub_download

REPO_ID = "eyu1belay/entrepreneurial-readiness-model"
MODEL_FILE = "readiness_model.joblib"

try:
    local_path = hf_hub_download(repo_id=REPO_ID, filename=MODEL_FILE, repo_type="model")
    bundle = load(local_path)  # expects {"model": clf, "feature_cols": [...]}
    model = bundle["model"]
    feature_cols = bundle["feature_cols"]
    load_err = None
except Exception as e:
    model, feature_cols = None, []
    load_err = f"⚠️ Could not load model bundle: {e}"

def _validate(savings_amount, income, bills, entertainment, sales, dependents, assets, age, risk, confidence, idea_diff):
    for name, v in [("savings_amount", savings_amount), ("income", income), ("bills", bills),
                    ("entertainment", entertainment), ("assets", assets)]:
        if v is None or v < 0:
            return f"Invalid {name}: must be >= 0"
    if not (1 <= sales <= 5): return "Sales must be in [1,5]"
    if not (0 <= dependents <= 10): return "Dependents must be in [0,10]"
    if not (18 <= age <= 80): return "Age must be in [18,80]"
    if not (1 <= risk <= 5): return "Risk must be in [1,5]"
    if not (1 <= confidence <= 10): return "Confidence must be in [1,10]"
    if not (1 <= idea_diff <= 5): return "Idea difficulty must be in [1,5]"
    return None

def predict_readiness(savings_amount, income, bills, entertainment, sales, dependents, assets, age, risk, confidence, idea_diff, debug=False):
    if load_err:
        return {"error": load_err}

    msg = _validate(savings_amount, income, bills, entertainment, sales, dependents, assets, age, risk, confidence, idea_diff)
    if msg:
        return {"error": msg}

    # IMPORTANT: keys must match the training feature names exactly
    row_dict = {
        "savings_amount": float(savings_amount),
        "monthly_income": float(income),
        "monthly_bills": float(bills),
        "monthly_entertainment": float(entertainment),
        "sales_skills": float(sales),
        "dependents": float(dependents),
        "assets": float(assets),
        "age": float(age),
        "risk": float(risk),
        "confidence": float(confidence),
        "idea_difficulty": float(idea_diff),
    }

    try:
        X = pd.DataFrame([row_dict])[feature_cols]
    except Exception as e:
        return {"error": f"Feature mismatch. Expected: {feature_cols}, Got: {list(row_dict.keys())}, Err: {e}"}

    y_hat = int(model.predict(X)[0])
    proba = model.predict_proba(X)[0].tolist() if hasattr(model, "predict_proba") else None

    out = {"model_label_1_5": y_hat, "proba": proba}
    if debug:
        out["echo_row_in_training_order"] = X.to_dict(orient="records")[0]
        out["feature_cols"] = feature_cols
    return out

with gr.Blocks(title="Entrepreneurial Readiness Predictor") as app:
    gr.Markdown("## Entrepreneurial Readiness Predictor")

    with gr.Row():
        savings_amount = gr.Number(label="Savings Amount ($)", value=1000)
        income  = gr.Number(label="Monthly Income ($)", value=4000)
        bills   = gr.Number(label="Monthly Bills ($)", value=2500)
        entertainment = gr.Number(label="Monthly Entertainment ($)", value=300)

    with gr.Row():
        sales   = gr.Slider(1, 5, step=1, value=3, label="Sales Skills (1–5)")
        dependents = gr.Slider(0, 10, step=1, value=1, label="Dependents (0–10)")
        assets  = gr.Number(label="Assets ($)", value=8000)
        age     = gr.Slider(18, 80, step=1, value=28, label="Age")

    with gr.Row():
        risk    = gr.Slider(1, 5, step=1, value=3, label="Risk (1–5)")
        confidence = gr.Slider(1, 10, step=1, value=6, label="Confidence (1–10)")
        idea_diff  = gr.Slider(1, 5, step=1, value=2, label="Idea Difficulty (1=easy, 5=hard)")
        debug_ck   = gr.Checkbox(label="Debug", value=False)

    predict_btn = gr.Button("Predict")
    pred_out    = gr.JSON(label="Prediction")

    predict_btn.click(
        predict_readiness,
        inputs=[savings_amount, income, bills, entertainment, sales, dependents, assets, age, risk, confidence, idea_diff, debug_ck],
        outputs=pred_out,
        api_name="predict_readiness"
    )

app.launch()