ftx7go commited on
Commit
6ff5d91
·
verified ·
1 Parent(s): 9e24dbf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -49
app.py CHANGED
@@ -14,15 +14,11 @@ from reportlab.platypus import Table, TableStyle
14
  # Load the trained model
15
  model = tf.keras.models.load_model("my_keras_model.h5")
16
 
17
- # Read HTML content from `re.html`
18
- with open("templates/re.html", "r", encoding="utf-8") as file:
19
- html_content = file.read()
20
-
21
  # List of sample images
22
  sample_images = [f"samples/{img}" for img in os.listdir("samples") if img.endswith((".png", ".jpg", ".jpeg"))]
23
 
24
  # Function to process X-ray and generate a PDF report
25
- def generate_report(name, age, gender, weight, height, allergies, cause, xray):
26
  image_size = (224, 224)
27
 
28
  def predict_fracture(xray_path):
@@ -34,25 +30,13 @@ def generate_report(name, age, gender, weight, height, allergies, cause, xray):
34
 
35
  # Predict fracture
36
  prediction = predict_fracture(xray)
37
- diagnosed_class = "normal" if prediction > 0.5 else "Fractured"
38
-
39
- # Injury severity classification
40
  severity = "Mild" if prediction < 0.3 else "Moderate" if prediction < 0.7 else "Severe"
41
 
42
- # Treatment details table
43
- treatment_data = [
44
- ["Severity Level", "Recommended Treatment", "Recovery Duration"],
45
- ["Mild", "Rest, pain relievers, and follow-up X-ray", "4-6 weeks"],
46
- ["Moderate", "Plaster cast, minor surgery if needed", "6-10 weeks"],
47
- ["Severe", "Major surgery, metal implants, physiotherapy", "Several months"]
48
- ]
49
-
50
- # Estimated cost & duration table
51
- cost_duration_data = [
52
- ["Hospital Type", "Estimated Cost", "Recovery Time"],
53
- ["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"],
54
- ["Private Hospital", f"₹{10000 if severity == 'Mild' else 30000 if severity == 'Moderate' else 100000}+", "6 weeks - Several months"]
55
- ]
56
 
57
  # Save X-ray image for report
58
  img = Image.open(xray).resize((300, 300))
@@ -62,27 +46,35 @@ def generate_report(name, age, gender, weight, height, allergies, cause, xray):
62
  # Generate PDF report
63
  report_path = f"{name}_fracture_report.pdf"
64
  c = canvas.Canvas(report_path, pagesize=letter)
65
-
 
 
 
66
  # Report title
67
  c.setFont("Helvetica-Bold", 16)
68
- c.drawString(200, 770, "Bone Fracture Detection Report")
 
 
 
69
 
70
- # Patient details table
71
  patient_data = [
72
- ["Patient Name", name],
73
  ["Age", age],
74
  ["Gender", gender],
 
 
75
  ["Weight", f"{weight} kg"],
76
  ["Height", f"{height} cm"],
77
- ["Allergies", allergies if allergies else "None"],
78
- ["Cause of Injury", cause if cause else "Not Provided"],
79
  ["Diagnosis", diagnosed_class],
80
  ["Injury Severity", severity]
81
  ]
82
 
83
  # Format and align tables
84
  def format_table(data):
85
- table = Table(data, colWidths=[270, 270]) # Set 90% width
86
  table.setStyle(TableStyle([
87
  ('BACKGROUND', (0, 0), (-1, 0), colors.darkblue),
88
  ('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
@@ -96,22 +88,22 @@ def generate_report(name, age, gender, weight, height, allergies, cause, xray):
96
 
97
  # Draw patient details table
98
  patient_table = format_table(patient_data)
99
- patient_table.wrapOn(c, 480, 500)
100
  patient_table.drawOn(c, 50, 620)
101
 
102
  # Load and insert X-ray image
103
- c.drawInlineImage(img_path, 50, 320, width=250, height=250)
104
  c.setFont("Helvetica-Bold", 12)
105
- c.drawString(120, 290, f"Fractured: {'Yes' if diagnosed_class == 'Fractured' else 'No'}")
106
-
107
- # Draw treatment and cost tables
108
- treatment_table = format_table(treatment_data)
109
- treatment_table.wrapOn(c, 480, 200)
110
- treatment_table.drawOn(c, 50, 200)
111
 
112
- cost_table = format_table(cost_duration_data)
113
- cost_table.wrapOn(c, 480, 150)
114
- cost_table.drawOn(c, 50, 80)
 
 
 
 
 
115
 
116
  c.save()
117
 
@@ -123,25 +115,54 @@ def use_sample_image(sample_image_path):
123
 
124
  # Define Gradio Interface
125
  with gr.Blocks() as app:
126
- gr.HTML(html_content) # Display `re.html` content in Gradio
127
- gr.Markdown("## Bone Fracture Detection System")
128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
  with gr.Row():
130
- name = gr.Textbox(label="Patient Name")
131
  age = gr.Number(label="Age")
132
  gender = gr.Radio(["Male", "Female", "Other"], label="Gender")
133
-
 
 
 
 
134
  with gr.Row():
135
  weight = gr.Number(label="Weight (kg)")
136
  height = gr.Number(label="Height (cm)")
137
-
138
  with gr.Row():
139
- allergies = gr.Textbox(label="Allergies (if any)")
140
- cause = gr.Textbox(label="Cause of Injury")
141
 
142
  with gr.Row():
143
  xray = gr.Image(type="filepath", label="Upload X-ray Image")
144
-
145
  with gr.Row():
146
  sample_selector = gr.Dropdown(choices=sample_images, label="Use Sample Image")
147
  select_button = gr.Button("Load Sample Image")
@@ -153,7 +174,7 @@ with gr.Blocks() as app:
153
 
154
  submit_button.click(
155
  generate_report,
156
- inputs=[name, age, gender, weight, height, allergies, cause, xray],
157
  outputs=[output_file],
158
  )
159
 
 
14
  # Load the trained model
15
  model = tf.keras.models.load_model("my_keras_model.h5")
16
 
 
 
 
 
17
  # List of sample images
18
  sample_images = [f"samples/{img}" for img in os.listdir("samples") if img.endswith((".png", ".jpg", ".jpeg"))]
19
 
20
  # Function to process X-ray and generate a PDF report
21
+ def generate_report(name, age, gender, weight, height, address, parent_name, allergies, cause, xray):
22
  image_size = (224, 224)
23
 
24
  def predict_fracture(xray_path):
 
30
 
31
  # Predict fracture
32
  prediction = predict_fracture(xray)
33
+ diagnosed_class = "Normal" if prediction > 0.5 else "Fractured"
 
 
34
  severity = "Mild" if prediction < 0.3 else "Moderate" if prediction < 0.7 else "Severe"
35
 
36
+ # Hospital details
37
+ hospital_name = "City Care Hospital"
38
+ hospital_address = "123 Health Street, MedCity, India"
39
+ doctor_name = "Dr. Anil Sharma (Orthopedic Specialist)"
 
 
 
 
 
 
 
 
 
 
40
 
41
  # Save X-ray image for report
42
  img = Image.open(xray).resize((300, 300))
 
46
  # Generate PDF report
47
  report_path = f"{name}_fracture_report.pdf"
48
  c = canvas.Canvas(report_path, pagesize=letter)
49
+
50
+ # Set page margins
51
+ c.translate(20, 20)
52
+
53
  # Report title
54
  c.setFont("Helvetica-Bold", 16)
55
+ c.drawString(180, 750, hospital_name)
56
+ c.setFont("Helvetica", 12)
57
+ c.drawString(140, 735, hospital_address)
58
+ c.drawString(180, 720, f"Attending Doctor: {doctor_name}")
59
 
60
+ # Patient details
61
  patient_data = [
62
+ ["Patient Name", name[:50]],
63
  ["Age", age],
64
  ["Gender", gender],
65
+ ["Parent's Name", parent_name[:50]],
66
+ ["Address", address[:70]],
67
  ["Weight", f"{weight} kg"],
68
  ["Height", f"{height} cm"],
69
+ ["Allergies", allergies[:50] if allergies else "None"],
70
+ ["Cause of Injury", cause[:50] if cause else "Not Provided"],
71
  ["Diagnosis", diagnosed_class],
72
  ["Injury Severity", severity]
73
  ]
74
 
75
  # Format and align tables
76
  def format_table(data):
77
+ table = Table(data, colWidths=[200, 350])
78
  table.setStyle(TableStyle([
79
  ('BACKGROUND', (0, 0), (-1, 0), colors.darkblue),
80
  ('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
 
88
 
89
  # Draw patient details table
90
  patient_table = format_table(patient_data)
91
+ patient_table.wrapOn(c, 450, 500)
92
  patient_table.drawOn(c, 50, 620)
93
 
94
  # Load and insert X-ray image
95
+ c.drawInlineImage(img_path, 50, 350, width=250, height=250)
96
  c.setFont("Helvetica-Bold", 12)
97
+ c.drawString(120, 320, f"Fractured: {'Yes' if diagnosed_class == 'Fractured' else 'No'}")
 
 
 
 
 
98
 
99
+ # Injury details
100
+ c.setFont("Helvetica-Bold", 14)
101
+ c.drawString(50, 270, "Injury Details and Treatment Recommendations")
102
+ c.setFont("Helvetica", 12)
103
+ c.drawString(50, 250, "• Immobilization and pain management")
104
+ c.drawString(50, 235, "• Follow-up X-rays required")
105
+ c.drawString(50, 220, "• Surgical intervention if needed")
106
+ c.drawString(50, 205, "• Physiotherapy for recovery")
107
 
108
  c.save()
109
 
 
115
 
116
  # Define Gradio Interface
117
  with gr.Blocks() as app:
118
+ gr.Markdown("## **Bone Fracture Detection System**")
 
119
 
120
+ # Informative Blog Section
121
+ with gr.Accordion("Bone Fractures - Symptoms, Causes, & Treatment", open=True):
122
+ gr.Markdown("""
123
+ **A fracture** is a break or crack in a bone caused by excessive force.
124
+ **Common Causes:**
125
+ - Traumatic injuries (sports, accidents, falls)
126
+ - Osteoporosis or cancer (weakened bones)
127
+
128
+ **Symptoms:**
129
+ - Severe pain, swelling, bruising
130
+ - Deformity or inability to use the limb
131
+
132
+ **Diagnosis:**
133
+ - X-rays, CT scans, MRI scans
134
+
135
+ **Treatment:**
136
+ - Plaster casts, splints, surgery if needed
137
+ - Pain management and physiotherapy
138
+
139
+ **First Aid:**
140
+ - Immobilize the area
141
+ - Apply a cold pack
142
+ - Seek medical help immediately
143
+ """)
144
+
145
+ # Patient Details Form
146
  with gr.Row():
147
+ name = gr.Textbox(label="Patient Name", max_length=50)
148
  age = gr.Number(label="Age")
149
  gender = gr.Radio(["Male", "Female", "Other"], label="Gender")
150
+
151
+ with gr.Row():
152
+ parent_name = gr.Textbox(label="Parent's Name", max_length=50)
153
+ address = gr.Textbox(label="Address", max_length=70)
154
+
155
  with gr.Row():
156
  weight = gr.Number(label="Weight (kg)")
157
  height = gr.Number(label="Height (cm)")
158
+
159
  with gr.Row():
160
+ allergies = gr.Textbox(label="Allergies (if any, max 50 chars)", max_length=50)
161
+ cause = gr.Textbox(label="Cause of Injury (max 50 chars)", max_length=50)
162
 
163
  with gr.Row():
164
  xray = gr.Image(type="filepath", label="Upload X-ray Image")
165
+
166
  with gr.Row():
167
  sample_selector = gr.Dropdown(choices=sample_images, label="Use Sample Image")
168
  select_button = gr.Button("Load Sample Image")
 
174
 
175
  submit_button.click(
176
  generate_report,
177
+ inputs=[name, age, gender, weight, height, address, parent_name, allergies, cause, xray],
178
  outputs=[output_file],
179
  )
180