|
|
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) |