This enhanced version of your credit risk model integrates Large Language Models (LLMs) to provide intelligent, interpretable insights alongside your machine learning predictions.
- Credit risk prediction using logistic regression
- Credit score calculation (300-900 range)
- Risk rating (Poor, Average, Good, Excellent)
- Interactive Streamlit interface
- AI-Powered Risk Analysis: Detailed explanations of risk factors
- Personalized Recommendations: Actionable advice for both lenders and borrowers
- Risk Factor Identification: Highlights key factors affecting creditworthiness
- Alternative Solutions: Suggests loan restructuring or mitigation strategies
- Natural Language Insights: Human-readable explanations of model outputs
pip install streamlit openai joblib numpy pandas scikit-learnOr create a requirements.txt:
streamlit==1.31.0
joblib==1.3.2
numpy==1.24.3
pandas==2.0.3
scikit-learn==1.3.0
openaiThen install:
pip install -r requirements.txt- Visit https://platform.openai.com/settings/organization/api-keys
- Sign up or log in
- Navigate to API Keys section
- Create a new API key
- Copy the key (starts with
sk-proj-)
Linux/Mac:
export openai_api_key='your-api-key-here'Windows (Command Prompt):
set openai_api_key=your-api-key-hereWindows (PowerShell):
$env: openai_api_key='your-api-key-here'Or create a .env file:
openai_api_key=your-api-key-here
Then load it in your code:
from dotenv import load_dotenv
load_dotenv()credit_risk_model/app
│
├── main.py # Enhanced Streamlit app
├── prediction_llm_helper.py # ML + LLM integration # Original Streamlit app
├── artifacts/
│ └── model_data.joblib # Your trained model
streamlit run main_with_llm.py- Takes user inputs (age, income, loan details, etc.)
- Preprocesses data with scaling
- Uses logistic regression for prediction
- Calculates credit score and rating
- Receives ML model outputs + input features
- Constructs a detailed prompt with financial context
- Calls Claude API for intelligent analysis
- Returns structured insights with:
- Risk summary
- Key risk factors
- Lender recommendations
- Borrower improvement tips
- Alternative strategies
Traditional Output:
- Default Probability: 15.32%
- Credit Score: 678
- Rating: Good
Enhanced LLM Output:
### Risk Summary
This applicant presents a MODERATE risk profile with a Good credit rating...
### Key Risk Factors
1. **Positive**: Low loan-to-income ratio (2.13) indicates strong repayment capacity
2. **Concern**: Delinquency ratio of 30% suggests past payment issues
3. **Concern**: Average DPD of 20 days shows pattern of late payments
### Recommendations
**For the Lender:**
- Approve with conditions: higher interest rate (prime + 2-3%)
- Require additional collateral or guarantor
- Set up automated payment reminders
**For the Borrower:**
- Focus on consistent on-time payments for next 6-12 months
- Reduce credit utilization below 20%
- Consider debt consolidation to simplify payments
### Alternative Actions
- Offer a smaller loan amount initially (₹18L instead of ₹25.6L)
- Implement graduated payment structure
- Provide financial literacy counselingOpenAI (GPT-4):
from openai import OpenAI
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
response = client.chat.completions.create(
model="gpt-4-turbo-preview",
messages=[{"role": "user", "content": prompt}]
)
insights = response.choices[0].message.contentGoogle (Gemini):
import google.generativeai as genai
genai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))
model = genai.GenerativeModel('gemini-pro')
response = model.generate_content(prompt)
insights = response.textModify the prompt in get_llm_insights() to:
- Focus on specific risk factors
- Change the tone (more technical/casual)
- Add regulatory compliance checks
- Include industry-specific guidelines
For repeated requests with similar parameters:
import functools
from functools import lru_cache
@lru_cache(maxsize=100)
def get_llm_insights_cached(credit_score, rating, probability):
# Cached version for similar scores
passFor real-time output in Streamlit:
with client.messages.stream(
model="claude-sonnet-4-20250514",
max_tokens=1500,
messages=[{"role": "user", "content": prompt}]
) as stream:
for text in stream.text_stream:
st.write(text, end="")