Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,73 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
|
7 |
-
|
8 |
-
|
|
|
9 |
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
+
from tensorflow.keras.preprocessing import image
|
5 |
+
from PIL import Image
|
6 |
+
from reportlab.lib.pagesizes import letter
|
7 |
+
from reportlab.pdfgen import canvas
|
8 |
+
import os
|
9 |
|
10 |
+
# Load the trained model
|
11 |
+
model = tf.keras.models.load_model("my_keras_model.h5")
|
12 |
|
13 |
+
# Function to process X-rays and generate a PDF report
|
14 |
+
def generate_report(name, age, gender, xray1, xray2):
|
15 |
+
image_size = (224, 224)
|
16 |
|
17 |
+
def predict_fracture(xray):
|
18 |
+
img = Image.open(xray).resize(image_size)
|
19 |
+
img_array = image.img_to_array(img) / 255.0
|
20 |
+
img_array = np.expand_dims(img_array, axis=0)
|
21 |
+
prediction = model.predict(img_array)[0][0]
|
22 |
+
return prediction
|
23 |
+
|
24 |
+
# Predict on both X-rays
|
25 |
+
prediction1 = predict_fracture(xray1)
|
26 |
+
prediction2 = predict_fracture(xray2)
|
27 |
+
avg_prediction = (prediction1 + prediction2) / 2
|
28 |
+
diagnosed_class = "Fractured" if avg_prediction > 0.5 else "Normal"
|
29 |
+
|
30 |
+
# Injury severity classification
|
31 |
+
severity = "Mild" if avg_prediction < 0.3 else "Moderate" if avg_prediction < 0.7 else "Severe"
|
32 |
+
treatment = {
|
33 |
+
"Mild": "Rest, pain relievers, follow-up X-ray.",
|
34 |
+
"Moderate": "Plaster cast, possible minor surgery.",
|
35 |
+
"Severe": "Major surgery, metal implants, physiotherapy."
|
36 |
+
}[severity]
|
37 |
+
gov_cost = {"Mild": "₹2,000 - ₹5,000", "Moderate": "₹8,000 - ₹15,000", "Severe": "₹20,000 - ₹50,000"}[severity]
|
38 |
+
private_cost = {"Mild": "₹10,000 - ₹20,000", "Moderate": "₹30,000 - ₹60,000", "Severe": "₹1,00,000+"}[severity]
|
39 |
+
|
40 |
+
# Generate PDF report
|
41 |
+
report_path = f"{name}_fracture_report.pdf"
|
42 |
+
c = canvas.Canvas(report_path, pagesize=letter)
|
43 |
+
c.setFont("Helvetica", 12)
|
44 |
+
c.drawString(100, 750, f"Patient Name: {name}")
|
45 |
+
c.drawString(100, 730, f"Age: {age}")
|
46 |
+
c.drawString(100, 710, f"Gender: {gender}")
|
47 |
+
c.drawString(100, 690, f"Diagnosis: {diagnosed_class}")
|
48 |
+
c.drawString(100, 670, f"Injury Severity: {severity}")
|
49 |
+
c.drawString(100, 650, f"Recommended Treatment: {treatment}")
|
50 |
+
c.drawString(100, 630, f"Estimated Cost (Govt Hospital): {gov_cost}")
|
51 |
+
c.drawString(100, 610, f"Estimated Cost (Private Hospital): {private_cost}")
|
52 |
+
c.save()
|
53 |
+
|
54 |
+
return report_path # Return path for auto-download
|
55 |
+
|
56 |
+
# Define Gradio Interface
|
57 |
+
interface = gr.Interface(
|
58 |
+
fn=generate_report,
|
59 |
+
inputs=[
|
60 |
+
gr.Textbox(label="Patient Name"),
|
61 |
+
gr.Number(label="Age"),
|
62 |
+
gr.Radio(["Male", "Female", "Other"], label="Gender"),
|
63 |
+
gr.Image(type="file", label="Upload X-ray Image 1"),
|
64 |
+
gr.Image(type="file", label="Upload X-ray Image 2"),
|
65 |
+
],
|
66 |
+
outputs=gr.File(label="Download Report"),
|
67 |
+
title="Bone Fracture Detection & Medical Report",
|
68 |
+
description="Enter patient details, upload two X-ray images, and generate a detailed medical report with treatment suggestions and cost estimates."
|
69 |
+
)
|
70 |
+
|
71 |
+
# Launch the Gradio app
|
72 |
+
if __name__ == "__main__":
|
73 |
+
interface.launch()
|