Upload 4 files
Browse files- Bank Loan Interest Rate Policy.pdf +0 -0
- Bank Loan Overall Risk Policy.pdf +0 -0
- app.py.txt +94 -0
- requirements.txt +3 -0
Bank Loan Interest Rate Policy.pdf
ADDED
|
Binary file (32.4 kB). View file
|
|
|
Bank Loan Overall Risk Policy.pdf
ADDED
|
Binary file (35.7 kB). View file
|
|
|
app.py.txt
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from google import genai
|
| 6 |
+
from google.genai import types
|
| 7 |
+
from google.api_core import exceptions
|
| 8 |
+
|
| 9 |
+
# 1. Access the API Key from Hugging Face Secrets
|
| 10 |
+
api_key = os.environ.get('GOOGLE_API_KEY')
|
| 11 |
+
|
| 12 |
+
# Initialize the Client
|
| 13 |
+
# Note: Using v1 for stable API version
|
| 14 |
+
client = genai.Client(
|
| 15 |
+
api_key=api_key,
|
| 16 |
+
http_options=types.HttpOptions(api_version='v1')
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Use gemini-1.5-flash for the best free-tier stability
|
| 20 |
+
FREE_STABLE_MODEL = "gemini-1.5-flash"
|
| 21 |
+
|
| 22 |
+
# --- Data Preparation (Internal Systems) ---
|
| 23 |
+
df_credit = pd.DataFrame({
|
| 24 |
+
'ID': [1111, 2222, 3333, 4444, 5555],
|
| 25 |
+
'Credit_Score': [455, 685, 825, 840, 350]
|
| 26 |
+
})
|
| 27 |
+
|
| 28 |
+
df_account = pd.DataFrame({
|
| 29 |
+
'ID': [1111, 2222, 3333, 4444, 5555],
|
| 30 |
+
'Name': ['Loren', 'Matt', 'Hilda', 'Andy', 'Kit'],
|
| 31 |
+
'Status': ['good-standing', 'closed', 'delinquent', 'good-standing', 'delinquent'],
|
| 32 |
+
'Nationality': ['Singaporean', 'NonSingaporean', 'Singaporean', 'NonSingaporean', 'Singaporean']
|
| 33 |
+
})
|
| 34 |
+
|
| 35 |
+
df_gov = pd.DataFrame({'ID': [2222, 4444], 'PR_Status': [True, False]})
|
| 36 |
+
df_merged = df_account.merge(df_credit, on="ID").merge(df_gov, on="ID", how="left").fillna(False)
|
| 37 |
+
|
| 38 |
+
# --- Upload Policies to Files API ---
|
| 39 |
+
# In Spaces, these files must be in the same folder as app.py
|
| 40 |
+
risk_policy_file = client.files.upload(file="Bank Loan Overall Risk Policy.pdf")
|
| 41 |
+
interest_policy_file = client.files.upload(file="Bank Loan Interest Rate Policy.pdf")
|
| 42 |
+
|
| 43 |
+
# --- Assessment Logic ---
|
| 44 |
+
def process_loan_request(applicant_id, customer_name_optional, max_retries=3):
|
| 45 |
+
for attempt in range(max_retries):
|
| 46 |
+
try:
|
| 47 |
+
row = df_merged[df_merged['ID'] == int(applicant_id)]
|
| 48 |
+
if row.empty: return "Error: Applicant ID not found in system."
|
| 49 |
+
applicant = row.iloc[0]
|
| 50 |
+
|
| 51 |
+
prompt = f"""
|
| 52 |
+
You are a Senior Loan Officer. Assess this application using the provided PDF policies.
|
| 53 |
+
|
| 54 |
+
APPLICANT DATA:
|
| 55 |
+
Name: {applicant['Name']} (ID: {applicant_id})
|
| 56 |
+
Credit Score: {applicant['Credit_Score']}
|
| 57 |
+
Account Status: {applicant['Status']}
|
| 58 |
+
Nationality: {applicant['Nationality']}
|
| 59 |
+
PR Status: {applicant['PR_Status']}
|
| 60 |
+
|
| 61 |
+
REQUIRED STEPS:
|
| 62 |
+
Step 1. Retrieve Information: State the name, score, status, and nationality.
|
| 63 |
+
Step 2. PR Status Check: Mandatory for Non-Singaporeans.
|
| 64 |
+
Step 3. Check Overall Risk: Use the 'Bank Loan Overall Risk Policy' table.
|
| 65 |
+
Step 4. Check Interest Rate: Use the 'Bank Loan Interest Rate Policy' table.
|
| 66 |
+
Step 5. Report: Recommend only if Singaporean OR (Non-Singaporean AND PR Status is True).
|
| 67 |
+
"""
|
| 68 |
+
|
| 69 |
+
response = client.models.generate_content(
|
| 70 |
+
model=FREE_STABLE_MODEL,
|
| 71 |
+
contents=[risk_policy_file, interest_policy_file, prompt]
|
| 72 |
+
)
|
| 73 |
+
return response.text
|
| 74 |
+
|
| 75 |
+
except exceptions.ResourceExhausted:
|
| 76 |
+
wait_time = (2 ** attempt) + 5
|
| 77 |
+
time.sleep(wait_time)
|
| 78 |
+
|
| 79 |
+
return "Error: System currently at max capacity. Please retry in 1 minute."
|
| 80 |
+
|
| 81 |
+
# --- Gradio Interface ---
|
| 82 |
+
demo = gr.Interface(
|
| 83 |
+
fn=process_loan_request,
|
| 84 |
+
inputs=[
|
| 85 |
+
gr.Textbox(label="Enter Applicant ID"),
|
| 86 |
+
gr.Textbox(label="Enter Customer Name (Optional)")
|
| 87 |
+
],
|
| 88 |
+
outputs=gr.Markdown(label="Underwriter Assessment Report"),
|
| 89 |
+
title="🏦 Enterprise AI Loan Underwriter",
|
| 90 |
+
description="Automated risk assessment via multi-system data integration and PDF policy compliance."
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
if __name__ == "__main__":
|
| 94 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
google-genai
|
| 2 |
+
pandas
|
| 3 |
+
gradio
|