from flask import Flask, render_template, request, jsonify import gradio as gr import pandas as pd from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression import warnings # Ignore all warnings warnings.filterwarnings("ignore") # Initialize Flask app app = Flask(__name__) # Load Data df_lending_data = pd.read_csv('Resources/lending_data.csv') # Prepare Features and Labels y = df_lending_data['loan_status'] X = df_lending_data.drop(columns=['loan_status']) # Split Data X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1) # Train Logistic Regression Model model = LogisticRegression(max_iter=200, random_state=1) model.fit(X_train, y_train) # Gradio Function for Prediction def predict_loan_status(loan_size, interest_rate, borrower_income, debt_to_income, num_of_accounts, derogatory_marks, total_debt): input_data = pd.DataFrame({ 'loan_size': [loan_size], 'interest_rate': [interest_rate], 'borrower_income': [borrower_income], 'debt_to_income': [debt_to_income], 'num_of_accounts': [num_of_accounts], 'derogatory_marks': [derogatory_marks], 'total_debt': [total_debt] }) prediction = model.predict(input_data) return "Healthy Loan (0)" if prediction[0] == 0 else "High-Risk Loan (1)" # Flask route for home page @app.route('/') def home(): return render_template('index.html') # Flask route for prediction @app.route('/predict', methods=['POST']) def predict(): # Get form data loan_size = float(request.form['loan_size']) interest_rate = float(request.form['interest_rate']) borrower_income = float(request.form['borrower_income']) debt_to_income = float(request.form['debt_to_income']) num_of_accounts = int(request.form['num_of_accounts']) derogatory_marks = int(request.form['derogatory_marks']) total_debt = float(request.form['total_debt']) # Prepare input data input_data = pd.DataFrame({ 'loan_size': [loan_size], 'interest_rate': [interest_rate], 'borrower_income': [borrower_income], 'debt_to_income': [debt_to_income], 'num_of_accounts': [num_of_accounts], 'derogatory_marks': [derogatory_marks], 'total_debt': [total_debt] }) # Make prediction prediction = model.predict(input_data) result = "Healthy Loan (0)" if prediction[0] == 0 else "High-Risk Loan (1)" return render_template('index.html', prediction_text=f'Prediction: {result}') # Flask route to serve Gradio interface @app.route('/gradio') def gradio_interface(): # Create the Gradio interface interface = gr.Interface( fn=predict_loan_status, inputs=[ gr.Number(label="Loan Size"), gr.Number(label="Interest Rate"), gr.Number(label="Borrower Income"), gr.Number(label="Debt-to-Income Ratio"), gr.Number(label="Number of Accounts"), gr.Number(label="Derogatory Marks"), gr.Number(label="Total Debt"), ], outputs="text", title="Loan Status Prediction", description="Input loan details to predict whether the loan is healthy or high-risk." ) # Launch Gradio interface on a different port return interface.launch(share=True, inline=True) # Run Flask app if __name__ == "__main__": app.run(debug=True)