Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -164,7 +164,14 @@ def Preprocessor(eda_input_dataframe : pd.DataFrame) -> pd.DataFrame:
|
|
| 164 |
numeric_cols = eda_input_dataframe.select_dtypes(include=['number']).columns
|
| 165 |
corr = eda_input_dataframe[numeric_cols].corr()
|
| 166 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
|
|
|
| 168 |
|
| 169 |
if existing_columns_to_drop:
|
| 170 |
eda_input_dataframe.drop(columns=existing_columns_to_drop, inplace=True, axis=1)
|
|
@@ -172,7 +179,8 @@ def Preprocessor(eda_input_dataframe : pd.DataFrame) -> pd.DataFrame:
|
|
| 172 |
else:
|
| 173 |
print("No columns to drop (already removed or not present).")
|
| 174 |
|
| 175 |
-
|
|
|
|
| 176 |
print("✅ Preprocessing complete.")
|
| 177 |
|
| 178 |
return eda_input_dataframe
|
|
@@ -249,7 +257,21 @@ class PredictionResponse(BaseModel):
|
|
| 249 |
|
| 250 |
|
| 251 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
|
| 254 |
|
| 255 |
|
|
@@ -330,8 +352,11 @@ async def predict(transaction: Transaction):
|
|
| 330 |
|
| 331 |
# Prédiction
|
| 332 |
prediction = model.predict(trans_dict)
|
|
|
|
|
|
|
|
|
|
| 333 |
|
| 334 |
-
print(f"✅ Prediction result with
|
| 335 |
|
| 336 |
return PredictionResponse(
|
| 337 |
prediction=int(prediction[0]),
|
|
@@ -348,37 +373,4 @@ async def predict(transaction: Transaction):
|
|
| 348 |
import traceback
|
| 349 |
traceback.print_exc()
|
| 350 |
raise HTTPException(status_code=500, detail=f"Prediction error: {str(e)}")
|
| 351 |
-
|
| 352 |
-
@app.post("/spredict", response_model=PredictionResponse)
|
| 353 |
-
async def presdict(transaction: Transaction):
|
| 354 |
-
"""
|
| 355 |
-
Make a fraud prediction for a single transaction.
|
| 356 |
-
"""
|
| 357 |
-
# Prepare the input data for prediction
|
| 358 |
-
input_data = pd.DataFrame([transaction.model_dump()])
|
| 359 |
-
|
| 360 |
-
print(f"Data for prediction: {input_data.to_dict(orient='records')}")
|
| 361 |
-
|
| 362 |
-
#Make the prediction
|
| 363 |
-
prediction = model.predict(input_data)
|
| 364 |
-
print(separator)
|
| 365 |
-
print(transaction)
|
| 366 |
-
print("🔮 Received transaction for prediction.")
|
| 367 |
-
|
| 368 |
-
# if model is None:
|
| 369 |
-
# raise HTTPException(status_code=503, detail="Model not loaded")
|
| 370 |
-
|
| 371 |
-
try:
|
| 372 |
-
df = pd.DataFrame([transaction.model_dump()])
|
| 373 |
-
|
| 374 |
-
print(f"Data for prediction: {df.to_dict(orient='records')}")
|
| 375 |
-
|
| 376 |
-
prediction = model.predict(df)
|
| 377 |
-
|
| 378 |
-
return PredictionResponse(
|
| 379 |
-
prediction=int(prediction[0]),
|
| 380 |
-
is_fraud=bool(prediction[0] == 1)
|
| 381 |
-
)
|
| 382 |
-
except Exception as e:
|
| 383 |
-
raise HTTPException(status_code=500, detail=f"❌ 🐛 Prediction error: {str(e)}")
|
| 384 |
-
|
|
|
|
| 164 |
numeric_cols = eda_input_dataframe.select_dtypes(include=['number']).columns
|
| 165 |
corr = eda_input_dataframe[numeric_cols].corr()
|
| 166 |
|
| 167 |
+
# plt.figure(figsize=(10, 8))
|
| 168 |
+
# sns.heatmap(corr, annot=True, fmt=".2f", cmap=jedhaCMInverted, square=True)
|
| 169 |
+
# plt.title("Matrice de corrélation des variables numériques")
|
| 170 |
+
# #plt.show()
|
| 171 |
+
# plt.savefig(current_path + '/outputs/Analysis_correlationMatrix_' + datetime.datetime.now().strftime('%Y%m%d_%H%M%S') + '.pdf')
|
| 172 |
+
# plt.close()
|
| 173 |
|
| 174 |
+
#drawCorrelationMatrix(df.drop(columns=["is_fraud"], inplace=False), title_suffix="_before_preprocessing", current_path=current_path)
|
| 175 |
|
| 176 |
if existing_columns_to_drop:
|
| 177 |
eda_input_dataframe.drop(columns=existing_columns_to_drop, inplace=True, axis=1)
|
|
|
|
| 179 |
else:
|
| 180 |
print("No columns to drop (already removed or not present).")
|
| 181 |
|
| 182 |
+
#drawCorrelationMatrix(df.drop(columns=["is_fraud"], inplace=False), title_suffix="_after_preprocessing", current_path=current_path)
|
| 183 |
+
|
| 184 |
print("✅ Preprocessing complete.")
|
| 185 |
|
| 186 |
return eda_input_dataframe
|
|
|
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
+
# class ExperimentInfo(BaseModel):
|
| 261 |
+
# experiment_id: str
|
| 262 |
+
# name: str
|
| 263 |
+
# artifact_location: str
|
| 264 |
+
# lifecycle_stage: str
|
| 265 |
+
# total_runs: int
|
| 266 |
+
# last_update: Optional[str] = None
|
| 267 |
|
| 268 |
+
# class MLflowTestResponse(BaseModel):
|
| 269 |
+
# status: str
|
| 270 |
+
# tracking_uri: str
|
| 271 |
+
# connection_success: bool
|
| 272 |
+
# total_experiments: int
|
| 273 |
+
# experiments: List[ExperimentInfo]
|
| 274 |
+
# error: Optional[str] = None
|
| 275 |
|
| 276 |
|
| 277 |
|
|
|
|
| 352 |
|
| 353 |
# Prédiction
|
| 354 |
prediction = model.predict(trans_dict)
|
| 355 |
+
best_model = getModelRunID()
|
| 356 |
+
model_name = best_model.name
|
| 357 |
+
model_version = best_model.latest_versions[0].version
|
| 358 |
|
| 359 |
+
print(f"✅ Prediction result with model {model_name} (v{model_version}) : {prediction}")
|
| 360 |
|
| 361 |
return PredictionResponse(
|
| 362 |
prediction=int(prediction[0]),
|
|
|
|
| 373 |
import traceback
|
| 374 |
traceback.print_exc()
|
| 375 |
raise HTTPException(status_code=500, detail=f"Prediction error: {str(e)}")
|
| 376 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|