File size: 4,208 Bytes
906d507 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | import json
import os
import subprocess
import threading
import time
import numpy as np
import tensorflow as tf
from flask import Flask, render_template, jsonify, request
from werkzeug.utils import secure_filename
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads'
os.makedirs('uploads', exist_ok=True)
training_status = {
"running": False,
"finished": False
}
def run_training(file_path):
training_status["running"] = True
training_status["finished"] = False
with open("training_results.json", "w") as f:
json.dump([], f)
server = subprocess.Popen(["python3", "server.py", file_path])
time.sleep(3)
client0 = subprocess.Popen(["python3", "client.py", "0", file_path])
client1 = subprocess.Popen(["python3", "client.py", "1", file_path])
server.wait()
client0.wait()
client1.wait()
training_status["running"] = False
training_status["finished"] = True
@app.route("/")
def index():
return render_template("landing.html")
@app.route("/dashboard")
def dashboard():
return render_template("index.html")
@app.route("/predict")
def predict():
return render_template("predict.html")
@app.route("/predict_result", methods=["POST"])
def predict_result():
diagnosis_type = request.form.get("diagnosis_type", "diabetes")
if diagnosis_type == "diabetes":
features = [
float(request.form.get("pregnancies", 0)),
float(request.form.get("glucose", 0)),
float(request.form.get("blood_pressure", 0)),
float(request.form.get("skin_thickness", 0)),
float(request.form.get("insulin", 0)),
float(request.form.get("bmi", 0)),
float(request.form.get("diabetes_pedigree", 0)),
float(request.form.get("age", 0)),
]
model_path = "diabetes_model.keras"
disease_name = "Diabetes"
elif diagnosis_type == "heart":
features = [
float(request.form.get("age", 0)),
float(request.form.get("sex", 0)),
float(request.form.get("cp", 0)),
float(request.form.get("trestbps", 0)),
float(request.form.get("chol", 0)),
float(request.form.get("fbs", 0)),
float(request.form.get("restecg", 0)),
float(request.form.get("thalach", 0)),
float(request.form.get("exang", 0)),
float(request.form.get("oldpeak", 0)),
float(request.form.get("slope", 0)),
float(request.form.get("ca", 0)),
float(request.form.get("thal", 0)),
]
model_path = "heart_model.keras"
disease_name = "Heart Disease"
else:
return jsonify({"error": "Invalid diagnosis type"})
X = np.array([features])
model = tf.keras.models.load_model(model_path)
prediction = model.predict(X)[0][0]
risk = "High Risk" if prediction > 0.5 else "Low Risk"
confidence = round(float(prediction) * 100, 1) if prediction > 0.5 else round((1 - float(prediction)) * 100, 1)
return jsonify({
"risk": risk,
"confidence": confidence,
"probability": round(float(prediction) * 100, 1),
"disease": disease_name
})
@app.route("/start_training", methods=["POST"])
def start_training():
if training_status["running"]:
return jsonify({"status": "already running"})
file_path = "diabetes.csv"
if "file" in request.files and request.files["file"].filename != "":
file = request.files["file"]
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
thread = threading.Thread(target=run_training, args=(file_path,))
thread.start()
return jsonify({"status": "started"})
@app.route("/status")
def status():
results = []
if os.path.exists("training_results.json"):
with open("training_results.json", "r") as f:
results = json.load(f)
return jsonify({
"running": training_status["running"],
"finished": training_status["finished"],
"rounds": results
})
if __name__ == "__main__":
app.run(debug=False, port=8000) |