Spaces:
Sleeping
Sleeping
add_app3
Browse files
app.py
CHANGED
|
@@ -14,11 +14,6 @@ import pandas as pd
|
|
| 14 |
import os
|
| 15 |
from typing import List, Optional
|
| 16 |
from datetime import datetime
|
| 17 |
-
import logging
|
| 18 |
-
|
| 19 |
-
Configure logging
|
| 20 |
-
logging.basicConfig(level=logging.INFO)
|
| 21 |
-
logger = logging.getLogger(__name__)
|
| 22 |
|
| 23 |
# ==========================================
|
| 24 |
# Configuration
|
|
@@ -251,23 +246,15 @@ def load_model_from_hf():
|
|
| 251 |
global model
|
| 252 |
|
| 253 |
try:
|
| 254 |
-
logger.info(f"π₯ Downloading model from HuggingFace: {REPO_ID}")
|
| 255 |
-
|
| 256 |
model_path = hf_hub_download(
|
| 257 |
repo_id=REPO_ID,
|
| 258 |
filename=MODEL_FILENAME,
|
| 259 |
revision=MODEL_VERSION
|
| 260 |
)
|
| 261 |
-
|
| 262 |
-
logger.info(f"β
Model downloaded to: {model_path}")
|
| 263 |
-
|
| 264 |
model = joblib.load(model_path)
|
| 265 |
-
logger.info(f"β
Model loaded: {type(model).__name__}")
|
| 266 |
-
|
| 267 |
return True
|
| 268 |
|
| 269 |
except Exception as e:
|
| 270 |
-
logger.error(f"β Failed to load model: {e}")
|
| 271 |
return False
|
| 272 |
|
| 273 |
|
|
@@ -314,15 +301,8 @@ def predict_transaction(data: dict) -> dict:
|
|
| 314 |
|
| 315 |
@app.on_event("startup")
|
| 316 |
async def startup_event():
|
| 317 |
-
"""Load model on startup"""
|
| 318 |
-
logger.info("π Starting Fraud Detection API...")
|
| 319 |
-
|
| 320 |
success = load_model_from_hf()
|
| 321 |
-
|
| 322 |
-
if success:
|
| 323 |
-
logger.info("β
API ready to serve predictions")
|
| 324 |
-
else:
|
| 325 |
-
logger.error("β API started but model failed to load")
|
| 326 |
|
| 327 |
|
| 328 |
# ==========================================
|
|
@@ -465,7 +445,6 @@ async def predict_single(transaction: TransactionInput):
|
|
| 465 |
return result
|
| 466 |
|
| 467 |
except Exception as e:
|
| 468 |
-
logger.error(f"Prediction error: {e}")
|
| 469 |
raise HTTPException(
|
| 470 |
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 471 |
detail=f"Prediction failed: {str(e)}"
|
|
@@ -538,7 +517,6 @@ async def predict_batch(batch: BatchTransactionInput):
|
|
| 538 |
}
|
| 539 |
|
| 540 |
except Exception as e:
|
| 541 |
-
logger.error(f"Batch prediction error: {e}")
|
| 542 |
raise HTTPException(
|
| 543 |
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 544 |
detail=f"Batch prediction failed: {str(e)}"
|
|
@@ -559,7 +537,6 @@ async def value_error_handler(request, exc):
|
|
| 559 |
|
| 560 |
@app.exception_handler(Exception)
|
| 561 |
async def general_exception_handler(request, exc):
|
| 562 |
-
logger.error(f"Unhandled exception: {exc}")
|
| 563 |
return JSONResponse(
|
| 564 |
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 565 |
content={"error": "Internal server error", "detail": "An unexpected error occurred"}
|
|
|
|
| 14 |
import os
|
| 15 |
from typing import List, Optional
|
| 16 |
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# ==========================================
|
| 19 |
# Configuration
|
|
|
|
| 246 |
global model
|
| 247 |
|
| 248 |
try:
|
|
|
|
|
|
|
| 249 |
model_path = hf_hub_download(
|
| 250 |
repo_id=REPO_ID,
|
| 251 |
filename=MODEL_FILENAME,
|
| 252 |
revision=MODEL_VERSION
|
| 253 |
)
|
|
|
|
|
|
|
|
|
|
| 254 |
model = joblib.load(model_path)
|
|
|
|
|
|
|
| 255 |
return True
|
| 256 |
|
| 257 |
except Exception as e:
|
|
|
|
| 258 |
return False
|
| 259 |
|
| 260 |
|
|
|
|
| 301 |
|
| 302 |
@app.on_event("startup")
|
| 303 |
async def startup_event():
|
| 304 |
+
"""Load model on startup"""
|
|
|
|
|
|
|
| 305 |
success = load_model_from_hf()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
|
| 307 |
|
| 308 |
# ==========================================
|
|
|
|
| 445 |
return result
|
| 446 |
|
| 447 |
except Exception as e:
|
|
|
|
| 448 |
raise HTTPException(
|
| 449 |
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 450 |
detail=f"Prediction failed: {str(e)}"
|
|
|
|
| 517 |
}
|
| 518 |
|
| 519 |
except Exception as e:
|
|
|
|
| 520 |
raise HTTPException(
|
| 521 |
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 522 |
detail=f"Batch prediction failed: {str(e)}"
|
|
|
|
| 537 |
|
| 538 |
@app.exception_handler(Exception)
|
| 539 |
async def general_exception_handler(request, exc):
|
|
|
|
| 540 |
return JSONResponse(
|
| 541 |
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 542 |
content={"error": "Internal server error", "detail": "An unexpected error occurred"}
|