Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -186,3 +186,29 @@ def test_model():
|
|
| 186 |
return {"sample_input": sample_text, "predictions": response}
|
| 187 |
except Exception as e:
|
| 188 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
return {"sample_input": sample_text, "predictions": response}
|
| 187 |
except Exception as e:
|
| 188 |
raise HTTPException(status_code=500, detail=str(e))
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
@app.post("/train")
|
| 192 |
+
def train_model():
|
| 193 |
+
try:
|
| 194 |
+
df = pd.read_csv(DATA_PATH).fillna("")
|
| 195 |
+
df["text_input"] = df.apply(create_text_input, axis=1)
|
| 196 |
+
X = df["text_input"]
|
| 197 |
+
y = df[["Maker_Action", "Escalation_Level", "Risk_Category",
|
| 198 |
+
"Risk_Drivers", "Investigation_Outcome", "Red_Flag_Reason"]]
|
| 199 |
+
|
| 200 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y,
|
| 201 |
+
test_size=0.2,
|
| 202 |
+
random_state=42)
|
| 203 |
+
pipeline = Pipeline([
|
| 204 |
+
("vectorizer", TfidfVectorizer()),
|
| 205 |
+
("classifier", MultiOutputClassifier(LogisticRegression(max_iter=1000)))
|
| 206 |
+
])
|
| 207 |
+
pipeline.fit(X_train, y_train)
|
| 208 |
+
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 209 |
+
joblib.dump(pipeline, MODEL_PATH)
|
| 210 |
+
accuracy = pipeline.score(X_test, y_test)
|
| 211 |
+
return {"message": "Model trained successfully", "accuracy": accuracy}
|
| 212 |
+
except Exception as e:
|
| 213 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 214 |
+
|