From 88c2489d186f3a55fcf17d32d195278725f5bf0e Mon Sep 17 00:00:00 2001 From: Admin User Date: Wed, 5 Feb 2025 19:19:23 +0000 Subject: [PATCH] PD v1 Processing block --- README.md | 10 ++++- block.py | 75 ++++++++++++++++++++++++-------- request_schema.json | 100 ++++++++++++++++++++++++++++++++++++++++++- requirements.txt | 3 +- response_schema.json | 11 ++++- 5 files changed, 176 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 59a3efc..ee00b4c 100644 --- a/README.md +++ b/README.md @@ -1 +1,9 @@ -**Hello world!!!** +## Overview +This block (`block.py`) is responsible for loading and scoring the model for the passed in probability. + +## Key Inputs & Outputs +- **Request**: Refer to `request_schema.json` for detailed input fields and validation rules. +- **Response**: Refer to `response_schema.json` for the returned structure and data types. + +## Implementation Details +- All core logic resides in `block.py` within the `__main__` function. diff --git a/block.py b/block.py index 3b227f9..905a399 100644 --- a/block.py +++ b/block.py @@ -1,21 +1,58 @@ -@flowx_block -def example_function(request: dict) -> dict: +import logging +import requests - # Processing logic here... +# Configure logging +logging.basicConfig( + level=logging.INFO, + format="%(asctime)s [%(levelname)s] %(name)s - %(message)s", +) +logger = logging.getLogger(__name__) - return { - "meta_info": [ - { - "name": "created_date", - "type": "string", - "value": "2024-11-05" - } - ], - "fields": [ - { - "name": "", - "type": "", - "value": "" - } - ] - } +url = "http://centurion-h2o-server.default.svc.cluster.local:8000/scoring" +headers = { + "accept": "application/json", + "Content-Type": "application/json" +} + +def __main__(pti: float, score_results: int, revolving_amount_monthly_payment: int, AT31S: int, total_amount_high_credit: int, AT20S: int, BALMAG01: int, record_counts_revolving_trade_count: int, PAYMNT10: int, closed_with_balance_amount_current_balance: int, REV83: int, AGG102: int, BC21S: int, record_counts_total_trade_count: int, revolving_amount_current_balance: int, total_amount_current_balance: int, revolving_amount_high_credit: int, US01S: int, AGG101: int, closed_with_balance_amount_monthly_payment: int, revolving_amount_credit_limit: int, revolving_amount_percent_available_credit: int, AT09S: int) -> dict: + input_data = { + "pti": pti, "score_results": score_results, "revolving_amount_monthly_payment": revolving_amount_monthly_payment, + "AT31S": AT31S, "total_amount_high_credit": total_amount_high_credit, "AT20S": AT20S, "BALMAG01": BALMAG01, + "record_counts_revolving_trade_count": record_counts_revolving_trade_count, "PAYMNT10": PAYMNT10, + "closed_with_balance_amount_current_balance": closed_with_balance_amount_current_balance, "REV83": REV83, + "AGG102": AGG102, "BC21S": BC21S, "record_counts_total_trade_count": record_counts_total_trade_count, + "revolving_amount_current_balance": revolving_amount_current_balance, "total_amount_current_balance": total_amount_current_balance, + "revolving_amount_high_credit": revolving_amount_high_credit, "US01S": US01S, "AGG101": AGG101, + "closed_with_balance_amount_monthly_payment": closed_with_balance_amount_monthly_payment, + "revolving_amount_credit_limit": revolving_amount_credit_limit, "revolving_amount_percent_available_credit": revolving_amount_percent_available_credit, + "AT09S": AT09S + } + + filtered_data = {key: value for key, value in input_data.items() if value is not None} + data = { + "model_name": "pd_v1", + "features": filtered_data + } + try: + try: + response = requests.post(url, headers=headers, json=data) + response.raise_for_status() # Raise an error for HTTP issues + + # Parse the response and return the "p1" value + response_data = response.json() + print("response json", response_data) + p1_value = response_data.get("prediction", {}).get("p1") + except requests.RequestException as error: + logger.error(f"Error while predicting: {error}") + return None + + if p1_value is not None: + logger.info(f"Prediction p1: {p1_value}") + return {'probability': float(p1_value)} + else: + logger.error("Response does not contain 'p1'") + return None + + except Exception as error: + logger.error(f"Error while predicting: {error}") + return {'error': str(error)} \ No newline at end of file diff --git a/request_schema.json b/request_schema.json index 0967ef4..c7d43f8 100644 --- a/request_schema.json +++ b/request_schema.json @@ -1 +1,99 @@ -{} +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + "properties": { + "pti": { + "type": ["number", "null"], + "description": "External + internal monthly payment to income ratio" + }, + "score_results": { + "type": ["number", "null"], + "description": "TransUnion score" + }, + "revolving_amount_monthly_payment": { + "type": ["number", "null"], + "description": "Minimum amount the borrower is required to pay each month to maintain the account in good standing" + }, + "AT31S": { + "type": ["integer", "null"], + "description": "Percentage of open trades > 75% of credit line verified in past 12 months" + }, + "total_amount_high_credit": { + "type": ["number", "null"], + "description": "The highest credit amount extended across all credit accounts" + }, + "AT20S": { + "type": ["integer", "null"], + "description": "Months since oldest trade opened" + }, + "BALMAG01": { + "type": ["number", "null"], + "description": "Non-mortgage balance magnitude" + }, + "record_counts_revolving_trade_count": { + "type": ["integer", "null"], + "description": "Records in the database related to revolving trade accounts (a credit card account)" + }, + "PAYMNT10": { + "type": ["number", "null"], + "description": "Number of payments in the last quarter" + }, + "closed_with_balance_amount_current_balance": { + "type": ["number", "null"], + "description": "The current balance of closed credit accounts" + }, + "REV83": { + "type": ["number", "null"], + "description": "Months since a revolving account last exceeded 75% utilization" + }, + "AGG102": { + "type": ["number", "null"], + "description": "Aggregate non-mortgage balances for month 2" + }, + "BC21S": { + "type": ["integer", "null"], + "description": "Months since most recent credit card trade opened" + }, + "record_counts_total_trade_count": { + "type": ["integer", "null"], + "description": "Total number of trade-related (transaction) records" + }, + "revolving_amount_current_balance": { + "type": ["number", "null"], + "description": "The current owed balance on revolving credit accounts" + }, + "total_amount_current_balance": { + "type": ["number", "null"], + "description": "The total current balance across all credit accounts" + }, + "revolving_amount_high_credit": { + "type": ["number", "null"], + "description": "The highest credit amount that has been extended to the borrower in revolving credit accounts" + }, + "US01S": { + "type": ["integer", "null"], + "description": "Number of unsecured installment trades" + }, + "AGG101": { + "type": ["number", "null"], + "description": "Aggregate non-mortgage balances for month 1" + }, + "closed_with_balance_amount_monthly_payment": { + "type": ["number", "null"], + "description": "The monthly payment amount for closed credit accounts (loans)" + }, + "revolving_amount_credit_limit": { + "type": ["number", "null"], + "description": "The total credit limit on revolving credit accounts" + }, + "revolving_amount_percent_available_credit": { + "type": ["number", "null"], + "description": "The percentage of available credit that has been utilized in revolving credit accounts" + }, + "AT09S": { + "type": ["integer", "null"], + "description": "Number of trades opened in past 24 months" + } + }, + "required": [] +} diff --git a/requirements.txt b/requirements.txt index 0967ef4..f0461cb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -{} +jsonschema==4.23.0 +requests==2.32.3 \ No newline at end of file diff --git a/response_schema.json b/response_schema.json index 0967ef4..5ccc416 100644 --- a/response_schema.json +++ b/response_schema.json @@ -1 +1,10 @@ -{} + { + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + "properties": { + "probability": { + "type": "number", + "description": "Model predicted score." + } + } + } \ No newline at end of file