BinduRP commited on
Commit
12cb1c8
·
verified ·
1 Parent(s): 48c19e8

Upload app.py

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