File size: 6,006 Bytes
15472c7
6bd8965
 
 
 
15472c7
 
6bd8965
 
15472c7
9b55e30
c2f7d68
9b55e30
 
 
 
c2f7d68
9b55e30
 
 
 
15472c7
 
6bd8965
c2f7d68
6bd8965
9b55e30
 
 
 
 
6bd8965
 
 
 
 
9b55e30
6bd8965
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b55e30
6bd8965
 
 
9b55e30
6bd8965
 
 
 
 
9b55e30
6bd8965
 
 
 
 
 
 
 
9b55e30
6bd8965
9b55e30
 
 
6bd8965
c2f7d68
6bd8965
15472c7
 
6bd8965
 
15472c7
6bd8965
 
 
15472c7
6bd8965
 
 
 
 
 
 
 
 
 
 
15472c7
 
6bd8965
 
 
 
 
 
9b55e30
6bd8965
 
 
 
 
 
 
 
 
 
c2f7d68
15472c7
 
6bd8965
c2f7d68
15472c7
6bd8965
 
c2f7d68
15472c7
 
 
9b55e30
6bd8965
15472c7
 
6bd8965
 
 
 
9b55e30
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
from flask import Flask, request, jsonify, send_from_directory
import tensorflow as tf
import numpy as np
import json
from tensorflow.keras.preprocessing import image
from PIL import Image
import io
import os
from flask_cors import CORS

# ─────────────────────────────────────────────
#  Absolute base directory
# ─────────────────────────────────────────────
BASE_DIR = os.path.dirname(os.path.abspath(__file__))

# ─────────────────────────────────────────────
#  Flask
# ─────────────────────────────────────────────
app = Flask(__name__,
            static_folder=os.path.join(BASE_DIR, 'static'),
            static_url_path='')
CORS(app)

# ─────────────────────────────────────────────
#  File paths
# ─────────────────────────────────────────────
WEIGHTS_PATH     = os.path.join(BASE_DIR, "best_weights.weights.h5")
ARCH_PATH        = os.path.join(BASE_DIR, "model_architecture.json")
CLASS_NAMES_PATH = os.path.join(BASE_DIR, "class_names.json")
SYMPTOMS_PATH    = os.path.join(BASE_DIR, "symptoms.json")
MEDICINES_PATH   = os.path.join(BASE_DIR, "medicines.json")
IMG_SIZE         = (224, 224)

# ─────────────────────────────────────────────
#  Load Model & Data
# ─────────────────────────────────────────────
print("Loading model architecture...")
with open(ARCH_PATH, 'r', encoding='utf-8') as f:
    model_json = f.read()
model = tf.keras.models.model_from_json(model_json)

print("Loading trained weights...")
model.load_weights(WEIGHTS_PATH)

with open(CLASS_NAMES_PATH, 'r', encoding='utf-8') as f:
    class_names = json.load(f)

with open(SYMPTOMS_PATH, 'r', encoding='utf-8') as f:
    DISEASE_SYMPTOMS = json.load(f)

with open(MEDICINES_PATH, 'r', encoding='utf-8') as f:
    MEDICINES_DB = json.load(f)

print(f"βœ… Model loaded! {len(class_names)} classes ready.")


# ─────────────────────────────────────────────
#  Image Preprocessing
# ─────────────────────────────────────────────
def preprocess_image(img_bytes):
    img = Image.open(io.BytesIO(img_bytes)).convert('RGB').resize(IMG_SIZE)
    img_array = image.img_to_array(img)
    img_array = np.expand_dims(img_array, axis=0)
    img_array = (img_array / 127.5) - 1.0
    return img_array


# ─────────────────────────────────────────────
#  Routes
# ─────────────────────────────────────────────
@app.route('/')
def serve_index():
    return send_from_directory(os.path.join(BASE_DIR, 'static'), 'index.html')

@app.route('/<path:filename>')
def serve_static(filename):
    return send_from_directory(os.path.join(BASE_DIR, 'static'), filename)


@app.route('/predict', methods=['POST'])
def predict():
    try:
        if 'file' not in request.files:
            return jsonify({"error": "No file uploaded"}), 400

        file = request.files['file']
        if file.filename == '':
            return jsonify({"error": "No file selected"}), 400

        user_info = {}
        if 'user_info' in request.form:
            try:
                user_info = json.loads(request.form['user_info'])
            except Exception:
                pass

        user_name     = user_info.get("name", "Patient")
        user_age      = user_info.get("age", "N/A")
        symptoms_text = user_info.get("symptoms", "")
        user_symptoms = [s.strip() for s in symptoms_text.replace(",", " ").split() if s.strip()]

        # Predict
        img_bytes     = file.read()
        processed_img = preprocess_image(img_bytes)
        predictions   = model.predict(processed_img)[0]

        top_idx           = int(np.argmax(predictions))
        confidence        = float(predictions[top_idx] * 100)
        predicted_disease = class_names[top_idx]

        # Symptom matching
        known_symptoms = DISEASE_SYMPTOMS.get(predicted_disease, [])
        matching = [s for s in user_symptoms if any(s.lower() == k.lower() for k in known_symptoms)]
        missing  = [k for k in known_symptoms if not any(k.lower() == u.lower() for u in user_symptoms)]
        match_score = (
            f"{len(matching)} of {len(known_symptoms)} typical symptoms match"
            if known_symptoms else "No symptom data available"
        )

        meds = MEDICINES_DB.get(predicted_disease, {})

        return jsonify({
            "disease":     predicted_disease,
            "confidence":  f"{confidence:.2f}",
            "match_score": match_score,
            "matching":    matching,
            "missing":     missing,
            "medicines":   meds
        })

    except Exception as e:
        print("❌ Error:", e)
        return jsonify({"error": "Prediction failed"}), 500


# ─────────────────────────────────────────────
#  Entry Point
# ─────────────────────────────────────────────
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860, debug=False)