ftx7go commited on
Commit
e1eb8aa
·
verified ·
1 Parent(s): 29c4191

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -71
app.py CHANGED
@@ -18,15 +18,16 @@ from reportlab.platypus import Table, TableStyle
18
  # Load the trained model
19
  model = tf.keras.models.load_model("my_keras_model.h5")
20
 
21
- # Read HTML content from `re.html`
22
- with open("templates/re.html", "r", encoding="utf-8") as file:
23
- html_content = file.read()
24
-
25
- # List of sample images
26
- sample_images = [f"samples/{img}" for img in os.listdir("samples") if img.endswith((".png", ".jpg", ".jpeg"))]
27
 
28
  # Function to send email
29
- def send_email(patient_email, report_path, patient_name):
 
 
 
 
 
30
  sender_email = "[email protected]"
31
  sender_password = "your_email_password"
32
 
@@ -60,8 +61,11 @@ def send_email(patient_email, report_path, patient_name):
60
 
61
  return f"Report sent successfully to {patient_email}!"
62
 
63
- # Function to process X-ray and generate a PDF report
64
  def generate_report(name, age, gender, weight, height, allergies, cause, xray):
 
 
 
65
  image_size = (224, 224)
66
 
67
  def predict_fracture(xray_path):
@@ -78,21 +82,6 @@ def generate_report(name, age, gender, weight, height, allergies, cause, xray):
78
  # Injury severity classification
79
  severity = "Mild" if prediction < 0.3 else "Moderate" if prediction < 0.7 else "Severe"
80
 
81
- # Treatment details table
82
- treatment_data = [
83
- ["Severity Level", "Recommended Treatment", "Recovery Duration"],
84
- ["Mild", "Rest, pain relievers, and follow-up X-ray", "4-6 weeks"],
85
- ["Moderate", "Plaster cast, minor surgery if needed", "6-10 weeks"],
86
- ["Severe", "Major surgery, metal implants, physiotherapy", "Several months"]
87
- ]
88
-
89
- # Estimated cost & duration table
90
- cost_duration_data = [
91
- ["Hospital Type", "Estimated Cost", "Recovery Time"],
92
- ["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"],
93
- ["Private Hospital", f"₹{10000 if severity == 'Mild' else 30000 if severity == 'Moderate' else 100000}+", "6 weeks - Several months"]
94
- ]
95
-
96
  # Save X-ray image for report
97
  img = Image.open(xray).resize((300, 300))
98
  img_path = f"{name}_xray.png"
@@ -102,63 +91,22 @@ def generate_report(name, age, gender, weight, height, allergies, cause, xray):
102
  report_path = f"{name}_fracture_report.pdf"
103
  c = canvas.Canvas(report_path, pagesize=letter)
104
 
105
- # Report title
106
  c.setFont("Helvetica-Bold", 16)
107
  c.drawString(200, 770, "Bone Fracture Detection Report")
108
 
109
- # Patient details table
110
- patient_data = [
111
- ["Patient Name", name],
112
- ["Age", age],
113
- ["Gender", gender],
114
- ["Weight", f"{weight} kg"],
115
- ["Height", f"{height} cm"],
116
- ["Allergies", allergies if allergies else "None"],
117
- ["Cause of Injury", cause if cause else "Not Provided"],
118
- ["Diagnosis", diagnosed_class],
119
- ["Injury Severity", severity]
120
- ]
121
-
122
- # Format and align tables
123
- def format_table(data):
124
- table = Table(data, colWidths=[270, 270]) # Set 90% width
125
- table.setStyle(TableStyle([
126
- ('BACKGROUND', (0, 0), (-1, 0), colors.darkblue),
127
- ('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
128
- ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
129
- ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
130
- ('BOTTOMPADDING', (0, 0), (-1, 0), 12),
131
- ('GRID', (0, 0), (-1, -1), 1, colors.black),
132
- ('VALIGN', (0, 0), (-1, -1), 'MIDDLE')
133
- ]))
134
- return table
135
-
136
- # Draw patient details table
137
- patient_table = format_table(patient_data)
138
- patient_table.wrapOn(c, 480, 500)
139
- patient_table.drawOn(c, 50, 620)
140
-
141
- # Load and insert X-ray image
142
- c.drawInlineImage(img_path, 50, 320, width=250, height=250)
143
- c.setFont("Helvetica-Bold", 12)
144
  c.drawString(120, 290, f"Fractured: {'Yes' if diagnosed_class == 'Fractured' else 'No'}")
145
 
146
- # Draw treatment and cost tables
147
- treatment_table = format_table(treatment_data)
148
- treatment_table.wrapOn(c, 480, 200)
149
- treatment_table.drawOn(c, 50, 200)
150
-
151
- cost_table = format_table(cost_duration_data)
152
- cost_table.wrapOn(c, 480, 150)
153
- cost_table.drawOn(c, 50, 80)
154
 
155
  c.save()
156
 
157
- return report_path # Return path for auto-download
 
 
 
158
 
159
  # Define Gradio Interface
160
  with gr.Blocks() as app:
161
- gr.HTML(html_content) # Display `re.html` content in Gradio
162
  gr.Markdown("## Bone Fracture Detection System")
163
 
164
  with gr.Row():
@@ -180,8 +128,19 @@ with gr.Blocks() as app:
180
  send_email_button = gr.Button("Send Report via Email")
181
  output_file = gr.File(label="Download Report")
182
 
183
- submit_button.click(generate_report, inputs=[name, age, gender, weight, height, allergies, cause, xray], outputs=[output_file])
184
- send_email_button.click(send_email, inputs=[email, output_file, name], outputs=[gr.Textbox(label="Status")])
 
 
 
 
 
 
 
 
 
 
 
185
 
186
  # Launch the Gradio app
187
  if __name__ == "__main__":
 
18
  # Load the trained model
19
  model = tf.keras.models.load_model("my_keras_model.h5")
20
 
21
+ # Store generated report file path
22
+ report_paths = {}
 
 
 
 
23
 
24
  # Function to send email
25
+ def send_email(patient_email, patient_name):
26
+ if patient_name not in report_paths:
27
+ return "Error: Generate the report first before sending."
28
+
29
+ report_path = report_paths[patient_name]
30
+
31
  sender_email = "[email protected]"
32
  sender_password = "your_email_password"
33
 
 
61
 
62
  return f"Report sent successfully to {patient_email}!"
63
 
64
+ # Function to generate report
65
  def generate_report(name, age, gender, weight, height, allergies, cause, xray):
66
+ if not name:
67
+ return "Error: Please enter a patient name."
68
+
69
  image_size = (224, 224)
70
 
71
  def predict_fracture(xray_path):
 
82
  # Injury severity classification
83
  severity = "Mild" if prediction < 0.3 else "Moderate" if prediction < 0.7 else "Severe"
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  # Save X-ray image for report
86
  img = Image.open(xray).resize((300, 300))
87
  img_path = f"{name}_xray.png"
 
91
  report_path = f"{name}_fracture_report.pdf"
92
  c = canvas.Canvas(report_path, pagesize=letter)
93
 
 
94
  c.setFont("Helvetica-Bold", 16)
95
  c.drawString(200, 770, "Bone Fracture Detection Report")
96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  c.drawString(120, 290, f"Fractured: {'Yes' if diagnosed_class == 'Fractured' else 'No'}")
98
 
99
+ c.drawInlineImage(img_path, 50, 320, width=250, height=250)
 
 
 
 
 
 
 
100
 
101
  c.save()
102
 
103
+ # Store the file path
104
+ report_paths[name] = report_path
105
+
106
+ return report_path # Return file path
107
 
108
  # Define Gradio Interface
109
  with gr.Blocks() as app:
 
110
  gr.Markdown("## Bone Fracture Detection System")
111
 
112
  with gr.Row():
 
128
  send_email_button = gr.Button("Send Report via Email")
129
  output_file = gr.File(label="Download Report")
130
 
131
+ # When clicking "Generate Report", save the file path and allow downloading
132
+ submit_button.click(
133
+ generate_report,
134
+ inputs=[name, age, gender, weight, height, allergies, cause, xray],
135
+ outputs=[output_file]
136
+ )
137
+
138
+ # When clicking "Send Report via Email", send the stored report file
139
+ send_email_button.click(
140
+ send_email,
141
+ inputs=[email, name],
142
+ outputs=[gr.Textbox(label="Status")]
143
+ )
144
 
145
  # Launch the Gradio app
146
  if __name__ == "__main__":