from fastapi import FastAPI from fastapi.middleware.wsgi import WSGIMiddleware from flask import Flask, render_template, request from flask_cors import CORS import numpy as np import pickle # Initialize Flask app flask_app = Flask(__name__) CORS(flask_app) def load_model(): with open('rf_model_ilpd_smote.pkl', 'rb') as model_file: return pickle.load(model_file) rf_model = load_model() @flask_app.route('/') def home(): return render_template('index.html') @flask_app.route('/predict', methods=['POST']) def predict(): try: input_data = [ float(request.form['Age']), float(request.form['Gender']), float(request.form['Total_Bilirubin']), float(request.form['Alkaline_Phosphatase']), float(request.form['Alamine_Aminotransferase']), float(request.form['Total_Protiens']), float(request.form['Albumin_and_Globulin_Ratio']), ] input_data = np.array(input_data).reshape(1, -1) prediction = rf_model.predict(input_data) predicted_class = prediction[0] return render_template('index.html', prediction_text=f"Prediction: {'Have Liver Disease' if predicted_class == 1 else 'Not Have Liver Disease'}") except Exception as e: return render_template('index.html', prediction_text="Error: " + str(e)) # Initialize FastAPI app app = FastAPI() # Rename fastapi_app to app # Mount Flask app inside FastAPI using WSGIMiddleware app.mount("/", WSGIMiddleware(flask_app))