ftx7go commited on
Commit
c6b4946
·
verified ·
1 Parent(s): dc8bbca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -17
app.py CHANGED
@@ -8,6 +8,8 @@ from tensorflow.keras.preprocessing import image
8
  from PIL import Image
9
  from reportlab.lib.pagesizes import letter
10
  from reportlab.pdfgen import canvas
 
 
11
 
12
  # Load the trained model
13
  model = tf.keras.models.load_model("my_keras_model.h5")
@@ -17,7 +19,7 @@ with open("templates/re.html", "r", encoding="utf-8") as file:
17
  html_content = file.read()
18
 
19
  # Function to process X-rays and generate a PDF report
20
- def generate_report(name, age, gender, xray1, xray2):
21
  image_size = (224, 224)
22
 
23
  def predict_fracture(xray_path):
@@ -35,26 +37,68 @@ def generate_report(name, age, gender, xray1, xray2):
35
 
36
  # Injury severity classification
37
  severity = "Mild" if avg_prediction < 0.3 else "Moderate" if avg_prediction < 0.7 else "Severe"
38
- treatment = {
39
- "Mild": "Rest, pain relievers, follow-up X-ray.",
40
- "Moderate": "Plaster cast, possible minor surgery.",
41
- "Severe": "Major surgery, metal implants, physiotherapy."
42
- }[severity]
43
- gov_cost = {"Mild": "₹2,000 - ₹5,000", "Moderate": "₹8,000 - ₹15,000", "Severe": "₹20,000 - ₹50,000"}[severity]
44
- private_cost = {"Mild": "₹10,000 - ₹20,000", "Moderate": "₹30,000 - ₹60,000", "Severe": "₹1,00,000+"}[severity]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  # Generate PDF report
47
  report_path = f"{name}_fracture_report.pdf"
48
  c = canvas.Canvas(report_path, pagesize=letter)
 
 
 
 
49
  c.setFont("Helvetica", 12)
50
- c.drawString(100, 750, f"Patient Name: {name}")
51
- c.drawString(100, 730, f"Age: {age}")
52
- c.drawString(100, 710, f"Gender: {gender}")
53
- c.drawString(100, 690, f"Diagnosis: {diagnosed_class}")
54
- c.drawString(100, 670, f"Injury Severity: {severity}")
55
- c.drawString(100, 650, f"Recommended Treatment: {treatment}")
56
- c.drawString(100, 630, f"Estimated Cost (Govt Hospital): {gov_cost}")
57
- c.drawString(100, 610, f"Estimated Cost (Private Hospital): {private_cost}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  c.save()
59
 
60
  return report_path # Return path for auto-download
@@ -69,6 +113,10 @@ with gr.Blocks() as app:
69
  age = gr.Number(label="Age")
70
  gender = gr.Radio(["Male", "Female", "Other"], label="Gender")
71
 
 
 
 
 
72
  with gr.Row():
73
  xray1 = gr.Image(type="filepath", label="Upload X-ray Image 1")
74
  xray2 = gr.Image(type="filepath", label="Upload X-ray Image 2")
@@ -78,7 +126,7 @@ with gr.Blocks() as app:
78
 
79
  submit_button.click(
80
  generate_report,
81
- inputs=[name, age, gender, xray1, xray2],
82
  outputs=[output_file],
83
  )
84
 
 
8
  from PIL import Image
9
  from reportlab.lib.pagesizes import letter
10
  from reportlab.pdfgen import canvas
11
+ from reportlab.lib import colors
12
+ from reportlab.platypus import Table, TableStyle
13
 
14
  # Load the trained model
15
  model = tf.keras.models.load_model("my_keras_model.h5")
 
19
  html_content = file.read()
20
 
21
  # Function to process X-rays and generate a PDF report
22
+ def generate_report(name, age, gender, allergies, cause, xray1, xray2):
23
  image_size = (224, 224)
24
 
25
  def predict_fracture(xray_path):
 
37
 
38
  # Injury severity classification
39
  severity = "Mild" if avg_prediction < 0.3 else "Moderate" if avg_prediction < 0.7 else "Severe"
40
+ treatment_details = {
41
+ "Mild": "Your fracture is classified as **Mild**. It may heal with rest, pain relievers, and a follow-up X-ray. Avoid excessive movement of the affected area.",
42
+ "Moderate": "Your fracture is classified as **Moderate**. You may require a plaster cast, splint, or minor surgery. Recovery takes **4-8 weeks**.",
43
+ "Severe": "Your fracture is classified as **Severe**. Surgery with metal implants and extensive physiotherapy is required. Recovery takes **several months** with proper rehabilitation."
44
+ }
45
+ treatment = treatment_details[severity]
46
+
47
+ # Estimated cost & duration
48
+ cost_duration_data = [
49
+ ["Hospital Type", "Estimated Cost", "Recovery Time"],
50
+ ["Government Hospital", f"₹{2000 if severity == 'Mild' else 8000 if severity == 'Moderate' else 20000} - ₹{5000 if severity == 'Mild' else 15000 if severity == 'Moderate' else 50000}", "4-12 weeks"],
51
+ ["Private Hospital", f"₹{10000 if severity == 'Mild' else 30000 if severity == 'Moderate' else 100000}+", "6 weeks - Several months"]
52
+ ]
53
+
54
+ # Save X-ray images for report
55
+ img1 = Image.open(xray1).resize((300, 300))
56
+ img2 = Image.open(xray2).resize((300, 300))
57
+ img1_path = f"{name}_xray1.png"
58
+ img2_path = f"{name}_xray2.png"
59
+ img1.save(img1_path)
60
+ img2.save(img2_path)
61
 
62
  # Generate PDF report
63
  report_path = f"{name}_fracture_report.pdf"
64
  c = canvas.Canvas(report_path, pagesize=letter)
65
+ c.setFont("Helvetica-Bold", 14)
66
+ c.drawString(200, 770, "Bone Fracture Detection Report")
67
+
68
+ # Patient details
69
  c.setFont("Helvetica", 12)
70
+ c.drawString(100, 740, f"Patient Name: {name}")
71
+ c.drawString(100, 720, f"Age: {age}")
72
+ c.drawString(100, 700, f"Gender: {gender}")
73
+ c.drawString(100, 680, f"Allergies: {allergies if allergies else 'None'}")
74
+ c.drawString(100, 660, f"Cause of Injury: {cause if cause else 'Not Provided'}")
75
+
76
+ # Diagnosis
77
+ c.setFont("Helvetica-Bold", 12)
78
+ c.drawString(100, 630, "Diagnosis & Treatment Plan:")
79
+ c.setFont("Helvetica", 11)
80
+ c.drawString(100, 610, f"Fracture Detected: {diagnosed_class}")
81
+ c.drawString(100, 590, f"Injury Severity: {severity}")
82
+ c.setFont("Helvetica", 10)
83
+ c.drawString(100, 570, f"{treatment}")
84
+
85
+ # Load and insert X-ray images
86
+ c.drawInlineImage(img1_path, 50, 250, width=250, height=250)
87
+ c.drawInlineImage(img2_path, 320, 250, width=250, height=250)
88
+
89
+ # Cost estimation table
90
+ table = Table(cost_duration_data)
91
+ table.setStyle(TableStyle([
92
+ ('BACKGROUND', (0, 0), (-1, 0), colors.grey),
93
+ ('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
94
+ ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
95
+ ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
96
+ ('BOTTOMPADDING', (0, 0), (-1, 0), 12),
97
+ ('GRID', (0, 0), (-1, -1), 1, colors.black)
98
+ ]))
99
+ table.wrapOn(c, 400, 300)
100
+ table.drawOn(c, 100, 150)
101
+
102
  c.save()
103
 
104
  return report_path # Return path for auto-download
 
113
  age = gr.Number(label="Age")
114
  gender = gr.Radio(["Male", "Female", "Other"], label="Gender")
115
 
116
+ with gr.Row():
117
+ allergies = gr.Textbox(label="Allergies (if any)")
118
+ cause = gr.Textbox(label="Cause of Injury")
119
+
120
  with gr.Row():
121
  xray1 = gr.Image(type="filepath", label="Upload X-ray Image 1")
122
  xray2 = gr.Image(type="filepath", label="Upload X-ray Image 2")
 
126
 
127
  submit_button.click(
128
  generate_report,
129
+ inputs=[name, age, gender, allergies, cause, xray1, xray2],
130
  outputs=[output_file],
131
  )
132