cap / app.py
tharu22's picture
first commit
0a4d346
import streamlit as st
import requests
import pandas as pd
# Set FastAPI backend URL
API_URL = "http://127.0.0.1:8000" # Change this if deployed elsewhere
# Streamlit UI
st.set_page_config(page_title="Loan Risk Analysis Dashboard", layout="wide")
st.title("πŸ“Š Loan Risk Analysis Dashboard")
# Sidebar for Navigation
st.sidebar.header("Navigation")
page = st.sidebar.radio(
"Go to",
[
"Loan Status Distribution",
"Payment Timeline Analysis",
"Principal Amount Patterns",
"Credit History Impact",
"Customer Profile Analysis",
"Loan Intent Analysis",
"Collection Effectiveness",
"Risk Score Development"
],
)
# Function to fetch data from FastAPI backend
def fetch_data(endpoint):
try:
response = requests.get(f"{API_URL}/{endpoint}")
if response.status_code == 200:
return response.json()
else:
st.error(f"Error fetching data: {response.json()['detail']}")
return None
except requests.exceptions.RequestException as e:
st.error(f"API request failed: {e}")
return None
# Loan Status Distribution
if page == "Loan Status Distribution":
st.subheader("πŸ“Œ Loan Status Distribution")
data = fetch_data("loan_status_distribution")
if data:
st.write(data)
st.bar_chart(pd.DataFrame([data], index=["Loan Status"]).T)
# Payment Timeline Analysis
elif page == "Payment Timeline Analysis":
st.subheader("πŸ“Œ Payment Timeline Analysis")
data = fetch_data("payment_timeline_analysis")
if data:
st.write(data)
st.bar_chart(pd.DataFrame(data["average_loan_amount_by_status"], index=["Loan Amount"]).T)
# Principal Amount Patterns
elif page == "Principal Amount Patterns":
st.subheader("πŸ“Œ Principal Amount Patterns")
data = fetch_data("principal_amount_patterns")
if data:
df = pd.DataFrame(data)
st.write(df)
st.bar_chart(df.set_index("loan_status")["count"])
# Credit History Impact
elif page == "Credit History Impact":
st.subheader("πŸ“Œ Credit History Impact")
data = fetch_data("credit_history_impact")
if data:
st.write(data)
# Customer Profile Analysis
elif page == "Customer Profile Analysis":
st.subheader("πŸ“Œ Customer Profile Analysis")
data = fetch_data("customer_profile_analysis")
if data:
df = pd.DataFrame(data["customer_profile_analysis"])
st.write(df)
st.bar_chart(df.set_index("person_age")["success_rate"])
# Loan Intent Analysis
elif page == "Loan Intent Analysis":
st.subheader("πŸ“Œ Loan Intent Analysis")
data = fetch_data("loan_intent_analysis")
if data:
st.write(data)
# Collection Effectiveness
elif page == "Collection Effectiveness":
st.subheader("πŸ“Œ Collection Effectiveness")
data = fetch_data("collection_effectiveness")
if data:
st.write(data)
# Risk Score Development
elif page == "Risk Score Development":
st.subheader("πŸ“Œ Risk Score Development")
data = fetch_data("risk_score_development")
if data:
st.write(data)
st.bar_chart(pd.DataFrame(data, index=["Risk Score"]).T)
# Run Streamlit
st.sidebar.info("πŸ“’ Select an option from the navigation to analyze loan risk insights.")