Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -41,7 +41,6 @@ cdss_data = {
|
|
| 41 |
"Glaucoma": {"status": "Glaucoma Suspect", "triage": "REFERRAL", "advice": "Optic nerve damage detected.", "action": "Refer for IOP Test."}
|
| 42 |
}
|
| 43 |
|
| 44 |
-
# 2. XAI Heatmap Functions
|
| 45 |
# 2. XAI Heatmap Functions (ACCURATE VERSION)
|
| 46 |
def generate_robust_heatmap(img_array, model, class_index, patch_size=16, step=8):
|
| 47 |
try:
|
|
@@ -91,6 +90,7 @@ def overlay_heatmap(img, heatmap, alpha=0.5):
|
|
| 91 |
superimposed_img = tf.keras.preprocessing.image.img_to_array(jet_heatmap) * alpha + tf.keras.preprocessing.image.img_to_array(img)
|
| 92 |
return tf.keras.preprocessing.image.array_to_img(superimposed_img)
|
| 93 |
|
|
|
|
| 94 |
# 3. Routes
|
| 95 |
@app.route('/')
|
| 96 |
def login(): return render_template('index.html')
|
|
@@ -104,6 +104,16 @@ def upload(): return render_template('upload.html')
|
|
| 104 |
@app.route('/result')
|
| 105 |
def result(): return render_template('result.html')
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
@app.route('/predict', methods=['POST'])
|
| 108 |
def predict():
|
| 109 |
file = request.files['image']
|
|
@@ -124,6 +134,21 @@ def predict():
|
|
| 124 |
if "Diabetic" in class_name: disease = "Diabetic"
|
| 125 |
elif "Glaucoma" in class_name: disease = "Glaucoma"
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
# Convert Original Image to Base64
|
| 128 |
buf = BytesIO()
|
| 129 |
image_resized.save(buf, format="JPEG")
|
|
@@ -141,8 +166,6 @@ def predict():
|
|
| 141 |
heat_b64 = base64.b64encode(buf2.getvalue()).decode('utf-8')
|
| 142 |
heatmap_generated = True
|
| 143 |
|
| 144 |
-
report = cdss_data[disease]
|
| 145 |
-
|
| 146 |
return jsonify({
|
| 147 |
'disease': class_name,
|
| 148 |
'confidence': f"{confidence:.1f}%",
|
|
|
|
| 41 |
"Glaucoma": {"status": "Glaucoma Suspect", "triage": "REFERRAL", "advice": "Optic nerve damage detected.", "action": "Refer for IOP Test."}
|
| 42 |
}
|
| 43 |
|
|
|
|
| 44 |
# 2. XAI Heatmap Functions (ACCURATE VERSION)
|
| 45 |
def generate_robust_heatmap(img_array, model, class_index, patch_size=16, step=8):
|
| 46 |
try:
|
|
|
|
| 90 |
superimposed_img = tf.keras.preprocessing.image.img_to_array(jet_heatmap) * alpha + tf.keras.preprocessing.image.img_to_array(img)
|
| 91 |
return tf.keras.preprocessing.image.array_to_img(superimposed_img)
|
| 92 |
|
| 93 |
+
|
| 94 |
# 3. Routes
|
| 95 |
@app.route('/')
|
| 96 |
def login(): return render_template('index.html')
|
|
|
|
| 104 |
@app.route('/result')
|
| 105 |
def result(): return render_template('result.html')
|
| 106 |
|
| 107 |
+
# DATABASE க்கான புது ரூட் (NEW ROUTE FOR DATABASE)
|
| 108 |
+
@app.route('/database')
|
| 109 |
+
def database():
|
| 110 |
+
conn = sqlite3.connect('reticheck_patients.db')
|
| 111 |
+
c = conn.cursor()
|
| 112 |
+
c.execute("SELECT * FROM history ORDER BY id DESC") # கடைசியாக செக் பண்ணியதை முதலில் காட்ட
|
| 113 |
+
records = c.fetchall()
|
| 114 |
+
conn.close()
|
| 115 |
+
return render_template('database.html', records=records)
|
| 116 |
+
|
| 117 |
@app.route('/predict', methods=['POST'])
|
| 118 |
def predict():
|
| 119 |
file = request.files['image']
|
|
|
|
| 134 |
if "Diabetic" in class_name: disease = "Diabetic"
|
| 135 |
elif "Glaucoma" in class_name: disease = "Glaucoma"
|
| 136 |
|
| 137 |
+
report = cdss_data[disease]
|
| 138 |
+
|
| 139 |
+
# --- DATABASE ல் சேமிப்பதற்கான கோடு (NEW CODE) ---
|
| 140 |
+
try:
|
| 141 |
+
conn = sqlite3.connect('reticheck_patients.db')
|
| 142 |
+
c = conn.cursor()
|
| 143 |
+
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 144 |
+
c.execute("INSERT INTO history (date, disease, confidence, status) VALUES (?, ?, ?, ?)",
|
| 145 |
+
(current_time, class_name, f"{confidence:.1f}%", report['triage']))
|
| 146 |
+
conn.commit()
|
| 147 |
+
conn.close()
|
| 148 |
+
except Exception as db_error:
|
| 149 |
+
print(f"Database Save Error: {db_error}")
|
| 150 |
+
# ------------------------------------------------
|
| 151 |
+
|
| 152 |
# Convert Original Image to Base64
|
| 153 |
buf = BytesIO()
|
| 154 |
image_resized.save(buf, format="JPEG")
|
|
|
|
| 166 |
heat_b64 = base64.b64encode(buf2.getvalue()).decode('utf-8')
|
| 167 |
heatmap_generated = True
|
| 168 |
|
|
|
|
|
|
|
| 169 |
return jsonify({
|
| 170 |
'disease': class_name,
|
| 171 |
'confidence': f"{confidence:.1f}%",
|