Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,98 @@
|
|
| 1 |
-
from flask import Flask, render_template
|
| 2 |
import gradio as gr
|
| 3 |
import threading
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
@app.route("/")
|
| 9 |
def home():
|
| 10 |
-
return render_template("re.html")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
def run_gradio():
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
if __name__ == "__main__":
|
| 17 |
threading.Thread(target=run_gradio).start()
|
| 18 |
-
app.run(host="0.0.0.0", port=7860, debug=True) #
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, send_file
|
| 2 |
import gradio as gr
|
| 3 |
import threading
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
import numpy as np
|
| 6 |
+
from tensorflow.keras.preprocessing import image
|
| 7 |
+
from PIL import Image
|
| 8 |
+
from reportlab.lib.pagesizes import letter
|
| 9 |
+
from reportlab.pdfgen import canvas
|
| 10 |
+
import os
|
| 11 |
|
| 12 |
+
# Load the trained model
|
| 13 |
+
model = tf.keras.models.load_model("my_keras_model.h5")
|
| 14 |
|
| 15 |
+
app = Flask(__name__, template_folder="templates", static_folder="static") # Ensure correct paths
|
| 16 |
+
|
| 17 |
+
# Function to process X-rays and generate a PDF report
|
| 18 |
+
def generate_report(name, age, gender, xray1, xray2):
|
| 19 |
+
image_size = (224, 224)
|
| 20 |
+
|
| 21 |
+
def predict_fracture(xray):
|
| 22 |
+
img = Image.open(xray).resize(image_size)
|
| 23 |
+
img_array = image.img_to_array(img) / 255.0
|
| 24 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 25 |
+
prediction = model.predict(img_array)[0][0]
|
| 26 |
+
return prediction
|
| 27 |
+
|
| 28 |
+
# Predict on both X-rays
|
| 29 |
+
prediction1 = predict_fracture(xray1)
|
| 30 |
+
prediction2 = predict_fracture(xray2)
|
| 31 |
+
avg_prediction = (prediction1 + prediction2) / 2
|
| 32 |
+
diagnosed_class = "Fractured" if avg_prediction > 0.5 else "Normal"
|
| 33 |
+
|
| 34 |
+
# Injury severity classification
|
| 35 |
+
severity = "Mild" if avg_prediction < 0.3 else "Moderate" if avg_prediction < 0.7 else "Severe"
|
| 36 |
+
treatment = {
|
| 37 |
+
"Mild": "Rest, pain relievers, follow-up X-ray.",
|
| 38 |
+
"Moderate": "Plaster cast, possible minor surgery.",
|
| 39 |
+
"Severe": "Major surgery, metal implants, physiotherapy."
|
| 40 |
+
}[severity]
|
| 41 |
+
gov_cost = {"Mild": "₹2,000 - ₹5,000", "Moderate": "₹8,000 - ₹15,000", "Severe": "₹20,000 - ₹50,000"}[severity]
|
| 42 |
+
private_cost = {"Mild": "₹10,000 - ₹20,000", "Moderate": "₹30,000 - ₹60,000", "Severe": "₹1,00,000+"}[severity]
|
| 43 |
+
|
| 44 |
+
# Generate PDF report
|
| 45 |
+
report_path = f"{name}_fracture_report.pdf"
|
| 46 |
+
c = canvas.Canvas(report_path, pagesize=letter)
|
| 47 |
+
c.setFont("Helvetica", 12)
|
| 48 |
+
c.drawString(100, 750, f"Patient Name: {name}")
|
| 49 |
+
c.drawString(100, 730, f"Age: {age}")
|
| 50 |
+
c.drawString(100, 710, f"Gender: {gender}")
|
| 51 |
+
c.drawString(100, 690, f"Diagnosis: {diagnosed_class}")
|
| 52 |
+
c.drawString(100, 670, f"Injury Severity: {severity}")
|
| 53 |
+
c.drawString(100, 650, f"Recommended Treatment: {treatment}")
|
| 54 |
+
c.drawString(100, 630, f"Estimated Cost (Govt Hospital): {gov_cost}")
|
| 55 |
+
c.drawString(100, 610, f"Estimated Cost (Private Hospital): {private_cost}")
|
| 56 |
+
c.save()
|
| 57 |
+
|
| 58 |
+
return report_path # Return path for auto-download
|
| 59 |
+
|
| 60 |
+
# Flask Route: Serve HTML Page
|
| 61 |
@app.route("/")
|
| 62 |
def home():
|
| 63 |
+
return render_template("re.html")
|
| 64 |
+
|
| 65 |
+
# Flask Route: Handle Form Submission
|
| 66 |
+
@app.route("/submit_report", methods=["POST"])
|
| 67 |
+
def submit_report():
|
| 68 |
+
name = request.form["first_name"] + " " + request.form["surname"]
|
| 69 |
+
age = request.form["age"]
|
| 70 |
+
gender = request.form["gender"]
|
| 71 |
+
xray1 = request.files["xray_side"]
|
| 72 |
+
xray2 = request.files["xray_top"]
|
| 73 |
+
|
| 74 |
+
# Generate PDF report
|
| 75 |
+
pdf_path = generate_report(name, age, gender, xray1, xray2)
|
| 76 |
+
|
| 77 |
+
return send_file(pdf_path, as_attachment=True) # Auto-download report
|
| 78 |
|
| 79 |
+
# Run Gradio in a separate thread
|
| 80 |
def run_gradio():
|
| 81 |
+
interface = gr.Interface(
|
| 82 |
+
fn=generate_report,
|
| 83 |
+
inputs=[
|
| 84 |
+
gr.Textbox(label="Patient Name"),
|
| 85 |
+
gr.Number(label="Age"),
|
| 86 |
+
gr.Radio(["Male", "Female", "Other"], label="Gender"),
|
| 87 |
+
gr.Image(type="file", label="Upload X-ray Image 1"),
|
| 88 |
+
gr.Image(type="file", label="Upload X-ray Image 2"),
|
| 89 |
+
],
|
| 90 |
+
outputs=gr.File(label="Download Report"),
|
| 91 |
+
title="Bone Fracture Detection & Medical Report",
|
| 92 |
+
description="Enter patient details, upload two X-ray images, and generate a detailed medical report."
|
| 93 |
+
)
|
| 94 |
+
interface.launch(share=True)
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|
| 97 |
threading.Thread(target=run_gradio).start()
|
| 98 |
+
app.run(host="0.0.0.0", port=7860, debug=True) # Flask runs separately
|