I have trained my deep learning model and saved it with the name "try_model_in_out.h5", then I want to try to create a website that can receive input and use the model I have trained to predict the results. When I try, it gives me an error like:enter image description here
I do not know why. I'm still a beginner. This is my structure file:enter image description here
api.py
from flask import Flask, request, render_templateimport tensorflow as tfimport osdef clear(): os.system('cls' if os.name == 'nt' else 'clear')clear()# Load the modelmodel = tf.keras.models.load_model("./try_model_in_out.h5")app = Flask(__name__)@app.route('/', methods=['GET'])def hello_world(): return render_template('index.html')@app.route('/', methods=['POST'])def mainFunction(): # Retrieve form data haematocrit = float(request.form['haematocrit']) haemoglobins = float(request.form['haemoglobins']) erythrocyte = float(request.form['erythrocyte']) leucocyte = float(request.form['leucocyte']) thrombocyte = float(request.form['thrombocyte']) mch = float(request.form['mch']) mchc = float(request.form['mchc']) mcv = float(request.form['mcv']) age = float(request.form['age']) sex = request.form['sex'] # Make prediction features = [[haematocrit, haemoglobins, erythrocyte, leucocyte, thrombocyte, mch, mchc, mcv, age, sex]] prediction = model.predict(features) result = "IN" if prediction[0][0] > 0.5 else "OUT" return render_template('index.html', haematocrit=haematocrit, haemoglobins=haemoglobins, erythrocyte=erythrocyte, leucocyte=leucocyte, thrombocyte=thrombocyte, mch=mch, mchc=mchc, mcv=mcv, age=age, sex=sex, prediction=result)if __name__ == '__main__': clear() app.run(port=3000, debug=True) clear()templates/index.html:
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Try Flask</title><script src="https://cdn.tailwindcss.com"></script></head><body class="bg-gray-100 min-h-screen"><div class="min-h-[50vh] bg-white p-8 rounded shadow-md w-[60%] border-0 mx-auto"><h1 class="text-2xl font-bold my-4">Patient Information Form</h1><form class="border-0 w-full my-5" action="/" method="POST"><div class="mb-4"><label for="haematocrit" class="block text-gray-700 font-semibold mb-2">Haematocrit</label><input type="text" id="haematocrit" value="35.1" name="haematocrit" class="w-full border border-gray-300 rounded-md py-2 px-3 focus:outline-none focus:border-blue-400"></div><div class="mb-4"><label for="haemoglobins" class="block text-gray-700 font-semibold mb-2">Haemoglobins</label><input type="text" id="haemoglobins" value="11.8" name="haemoglobins" class="w-full border border-gray-300 rounded-md py-2 px-3 focus:outline-none focus:border-blue-400"></div><div class="mb-4"><label for="erythrocyte" class="block text-gray-700 font-semibold mb-2">Erythrocyte</label><input type="text" id="erythrocyte" value="4.65" name="erythrocyte" class="w-full border border-gray-300 rounded-md py-2 px-3 focus:outline-none focus:border-blue-400"></div><div class="mb-4"><label for="leucocyte" class="block text-gray-700 font-semibold mb-2">Leucocyte</label><input type="text" id="leucocyte" value="6.3" name="leucocyte" class="w-full border border-gray-300 rounded-md py-2 px-3 focus:outline-none focus:border-blue-400"></div><div class="mb-4"><label for="thrombocyte" class="block text-gray-700 font-semibold mb-2">Thrombocyte</label><input type="text" id="thrombocyte" value="310" name="thrombocyte" class="w-full border border-gray-300 rounded-md py-2 px-3 focus:outline-none focus:border-blue-400"></div><div class="mb-4"><label for="mch" class="block text-gray-700 font-semibold mb-2">MCH</label><input type="text" id="mch" value="25.4" name="mch" class="w-full border border-gray-300 rounded-md py-2 px-3 focus:outline-none focus:border-blue-400"></div><div class="mb-4"><label for="mchc" class="block text-gray-700 font-semibold mb-2">MCHC</label><input type="text" id="mchc" value="33.6" name="mchc" class="w-full border border-gray-300 rounded-md py-2 px-3 focus:outline-none focus:border-blue-400"></div><div class="mb-4"><label for="mcv" class="block text-gray-700 font-semibold mb-2">MCV</label><input type="text" id="mcv" value="75.5" name="mcv" class="w-full border border-gray-300 rounded-md py-2 px-3 focus:outline-none focus:border-blue-400"></div><div class="mb-4"><label for="age" class="block text-gray-700 font-semibold mb-2">Age</label><input type="text" id="age" value="1" name="age" class="w-full border border-gray-300 rounded-md py-2 px-3 focus:outline-none focus:border-blue-400"></div><div class="mb-4"><label for="sex" class="block text-gray-700 font-semibold mb-2">Sex</label><select id="sex" value="F" name="sex" class="w-full border border-gray-300 rounded-md py-2 px-3 focus:outline-none focus:border-blue-400"><option value="1">Male</option><option value="0">Female</option></select></div><button type="submit" class="bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-600 w-full">Submit</button></form><h1>Prediction Result</h1><p>Haematocrit: {{ haematocrit }}</p><p>Haemoglobins: {{ haemoglobins }}</p><p>Erythrocyte: {{ erythrocyte }}</p><p>Leucocyte: {{ leucocyte }}</p><p>Thrombocyte: {{ thrombocyte }}</p><p>MCH: {{ mch }}</p><p>MCHC: {{ mchc }}</p><p>MCV: {{ mcv }}</p><p>Age: {{ age }}</p><p>Sex: {{ sex }}</p><p>The prediction result is: {{ prediction }}</p></div></body></html>When I remove this line of code:
api.py
prediction = model.predict(features) result = "IN" if prediction[0][0] > 0.5 else "OUT"To indicate whether the value of the input exists or not, it turns out it does.enter image description here
I want the prediction results to be displayed too.