File size: 4,246 Bytes
ac573e4
 
d9aad77
 
ac573e4
d9aad77
ac573e4
d02e833
 
 
 
 
 
ac573e4
 
d9aad77
 
 
d02e833
 
 
ac573e4
d9aad77
 
 
 
ac573e4
 
d02e833
ac573e4
 
 
 
 
d9aad77
ac573e4
 
 
 
 
 
 
d9aad77
ac573e4
 
 
d9aad77
d02e833
 
 
ac573e4
d9aad77
 
 
 
ac573e4
 
d02e833
ac573e4
 
d9aad77
 
 
 
d02e833
ac573e4
d9aad77
ac573e4
 
 
d9aad77
d02e833
 
 
ac573e4
d9aad77
 
 
 
ac573e4
 
d02e833
ac573e4
d9aad77
ac573e4
 
d9aad77
d02e833
ac573e4
d9aad77
ac573e4
 
 
d9aad77
d02e833
 
 
ac573e4
d9aad77
ac573e4
d02e833
d9aad77
 
 
 
 
 
d02e833
d9aad77
 
d02e833
d9aad77
 
 
 
 
 
 
 
d02e833
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9aad77
 
 
 
 
 
d02e833
 
ac573e4
 
 
d9aad77
d02e833
 
 
ac573e4
 
d02e833
 
 
 
ac573e4
d9aad77
ac573e4
 
 
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from fastapi import FastAPI, UploadFile, File, HTTPException
import pandas as pd
import os

from backend.core.orchestrator import Orchestrator
from backend.api.schemas import PredictRequest

app = FastAPI(
    title="ModelSmith AI",
    description="Automated ML platform for tabular data",
    version="1.0.0"
)

orchestrator = Orchestrator()

MODEL_PATH = "exports/models/trained_model.pkl"


# -----------------------------
# Analyze Dataset
# -----------------------------
@app.post("/analyze")
async def analyze_dataset(
    file: UploadFile = File(...),
    target_column: str = "target"
):
    try:
        df = pd.read_csv(file.file)

        result = orchestrator.run(df, target_column)

        dataset_info = result.get("dataset_info", {})
        strategy = result.get("strategy", {})

        return {
            "columns": list(df.columns),
            "dataTypes": dataset_info.get("data_types", {}),
            "risks": dataset_info.get("risks", []),
            "problemType": result.get("problem_type"),
            "confidence": strategy.get("confidence", 0),
            "strategy": strategy
        }

    except Exception as e:
        raise HTTPException(status_code=400, detail=str(e))


# -----------------------------
# Train Model
# -----------------------------
@app.post("/train")
async def train_model(
    file: UploadFile = File(...),
    target_column: str = "target"
):
    try:
        df = pd.read_csv(file.file)

        result = orchestrator.run(df, target_column, train=True)

        return {
            "strategy": result.get("strategy"),
            "metrics": result.get("metrics"),
            "model_path": MODEL_PATH,
            "model_id": "trained_model"
        }

    except Exception as e:
        raise HTTPException(status_code=400, detail=str(e))


# -----------------------------
# Explain Model
# -----------------------------
@app.post("/explain")
async def explain_model(
    file: UploadFile = File(...),
    target_column: str = "target"
):
    try:
        df = pd.read_csv(file.file)

        result = orchestrator.run(df, target_column, train=True)

        return {
            "strategy_explanation": result.get("strategy_explanation"),
            "metrics": result.get("metrics"),
            "feature_importance": result.get("feature_importance")
        }

    except Exception as e:
        raise HTTPException(status_code=400, detail=str(e))


# -----------------------------
# Predict
# -----------------------------
@app.post("/predict")
async def predict(request: PredictRequest):
    try:
        # 1. Check model exists
        if not os.path.exists(MODEL_PATH):
            raise HTTPException(
                status_code=400,
                detail="No trained model found. Train a model first."
            )

        # 2. Load model
        model = orchestrator.model_io.load(MODEL_PATH)

        # 3. Convert input to DataFrame
        df = pd.DataFrame(request.instances)

        if df.empty:
            raise HTTPException(
                status_code=400,
                detail="Prediction data is empty."
            )

        # 4. Validate feature columns
        if hasattr(model, "feature_names_in_"):
            expected_features = list(model.feature_names_in_)
            received_features = list(df.columns)

            missing = set(expected_features) - set(received_features)
            extra = set(received_features) - set(expected_features)

            if missing:
                raise HTTPException(
                    status_code=400,
                    detail=f"Missing required features: {sorted(missing)}"
                )

            # Drop extra columns if any
            df = df[expected_features]

        # 5. Predict
        predictions = model.predict(df)

        return {
            "predictions": predictions.tolist()
        }

    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=400, detail=str(e))


# -----------------------------
# Root & Health
# -----------------------------
@app.get("/")
def root():
    return {
        "message": "ModelSmith AI API",
        "status": "running"
    }


@app.get("/health")
def health():
    return {"status": "ok"}