-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_ai_loans.py
More file actions
38 lines (33 loc) · 1.58 KB
/
Copy pathfix_ai_loans.py
File metadata and controls
38 lines (33 loc) · 1.58 KB
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
import sys, os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from app import app, db
from models.loan_model import Loan
from models.user_model import User
from services.ai_service import predict_risk
with app.app_context():
loans = db.session.query(Loan).filter(Loan.risk_score == None).all()
print(f"Found {len(loans)} loans with no AI assessment")
for loan in loans:
user = db.session.query(User).filter(User.id == loan.user_id).first()
if not user:
print(f"Loan #{loan.id}: No user found, skipping")
continue
print(f"Running AI assessment for Loan #{loan.id} (Amount: {loan.amount}, Income: {user.income})...")
ai_result = predict_risk(
loan_amount=loan.amount,
tenure=loan.tenure,
interest_rate=loan.interest_rate,
income=user.income
)
if ai_result.get("success"):
loan.risk_score = ai_result.get("risk_score")
loan.risk_category = ai_result.get("risk_category")
loan.ai_recommendation = ai_result.get("ai_recommendation")
loan.ai_confidence = ai_result.get("ai_confidence")
loan.fraud_risk = ai_result.get("fraud_risk")
loan.fraud_confidence = ai_result.get("fraud_confidence")
print(f" -> Risk: {loan.risk_score} ({loan.risk_category}), AI: {loan.ai_recommendation}, Fraud: {loan.fraud_risk}")
else:
print(f" -> AI assessment failed: {ai_result}")
db.session.commit()
print("\nAll loans updated with AI assessment!")