ftx7go commited on
Commit
c3b9c24
·
verified ·
1 Parent(s): b90e22d

Create flask_app.py

Browse files
Files changed (1) hide show
  1. flask_app.py +78 -0
flask_app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request, send_file
2
+ import os
3
+ import tensorflow as tf
4
+ import numpy as np
5
+ from tensorflow.keras.preprocessing import image
6
+ from PIL import Image
7
+ from reportlab.lib.pagesizes import letter
8
+ from reportlab.pdfgen import canvas
9
+
10
+ # Load the trained model
11
+ model = tf.keras.models.load_model("my_keras_model.h5")
12
+
13
+ app = Flask(__name__, template_folder="templates", static_folder="static")
14
+
15
+ # Function to process X-rays and generate a PDF report
16
+ def generate_report(name, age, gender, xray1, xray2):
17
+ image_size = (224, 224)
18
+
19
+ def predict_fracture(xray):
20
+ img = Image.open(xray).resize(image_size)
21
+ img_array = image.img_to_array(img) / 255.0
22
+ img_array = np.expand_dims(img_array, axis=0)
23
+ prediction = model.predict(img_array)[0][0]
24
+ return prediction
25
+
26
+ # Predict on both X-rays
27
+ prediction1 = predict_fracture(xray1)
28
+ prediction2 = predict_fracture(xray2)
29
+ avg_prediction = (prediction1 + prediction2) / 2
30
+ diagnosed_class = "Fractured" if avg_prediction > 0.5 else "Normal"
31
+
32
+ # Injury severity classification
33
+ severity = "Mild" if avg_prediction < 0.3 else "Moderate" if avg_prediction < 0.7 else "Severe"
34
+ treatment = {
35
+ "Mild": "Rest, pain relievers, follow-up X-ray.",
36
+ "Moderate": "Plaster cast, possible minor surgery.",
37
+ "Severe": "Major surgery, metal implants, physiotherapy."
38
+ }[severity]
39
+ gov_cost = {"Mild": "₹2,000 - ₹5,000", "Moderate": "₹8,000 - ₹15,000", "Severe": "₹20,000 - ₹50,000"}[severity]
40
+ private_cost = {"Mild": "₹10,000 - ₹20,000", "Moderate": "₹30,000 - ₹60,000", "Severe": "₹1,00,000+"}[severity]
41
+
42
+ # Generate PDF report
43
+ report_path = f"{name}_fracture_report.pdf"
44
+ c = canvas.Canvas(report_path, pagesize=letter)
45
+ c.setFont("Helvetica", 12)
46
+ c.drawString(100, 750, f"Patient Name: {name}")
47
+ c.drawString(100, 730, f"Age: {age}")
48
+ c.drawString(100, 710, f"Gender: {gender}")
49
+ c.drawString(100, 690, f"Diagnosis: {diagnosed_class}")
50
+ c.drawString(100, 670, f"Injury Severity: {severity}")
51
+ c.drawString(100, 650, f"Recommended Treatment: {treatment}")
52
+ c.drawString(100, 630, f"Estimated Cost (Govt Hospital): {gov_cost}")
53
+ c.drawString(100, 610, f"Estimated Cost (Private Hospital): {private_cost}")
54
+ c.save()
55
+
56
+ return report_path # Return path for auto-download
57
+
58
+ # Flask Route: Serve HTML Page
59
+ @app.route("/")
60
+ def home():
61
+ return render_template("re.html")
62
+
63
+ # Flask Route: Handle Form Submission
64
+ @app.route("/submit_report", methods=["POST"])
65
+ def submit_report():
66
+ name = request.form["first_name"] + " " + request.form["surname"]
67
+ age = request.form["age"]
68
+ gender = request.form["gender"]
69
+ xray1 = request.files["xray_side"]
70
+ xray2 = request.files["xray_top"]
71
+
72
+ # Generate PDF report
73
+ pdf_path = generate_report(name, age, gender, xray1, xray2)
74
+
75
+ return send_file(pdf_path, as_attachment=True) # Auto-download report
76
+
77
+ if __name__ == "__main__":
78
+ app.run(host="0.0.0.0", port=7860, debug=False) # Run Flask on 7860