ftx7go commited on
Commit
a41e479
·
verified ·
1 Parent(s): 4aa981f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -33
app.py CHANGED
@@ -1,9 +1,12 @@
1
  import os
2
- os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # Force TensorFlow to use CPU
3
-
4
  import gradio as gr
5
  import tensorflow as tf
6
  import numpy as np
 
 
 
 
7
  from tensorflow.keras.preprocessing import image
8
  from PIL import Image
9
  from reportlab.lib.pagesizes import letter
@@ -11,6 +14,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")
16
 
@@ -21,7 +28,7 @@ with open("templates/re.html", "r", encoding="utf-8") as file:
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
 
@@ -56,13 +63,13 @@ def generate_report(name, age, gender, weight, height, allergies, cause, xray):
56
 
57
  # Save X-ray image for report
58
  img = Image.open(xray).resize((300, 300))
59
- img_path = f"{name}_xray.png"
60
  img.save(img_path)
61
 
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")
@@ -82,7 +89,7 @@ def generate_report(name, age, gender, weight, height, allergies, cause, xray):
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),
@@ -94,17 +101,15 @@ def generate_report(name, age, gender, weight, height, allergies, cause, xray):
94
  ]))
95
  return table
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)
@@ -117,46 +122,63 @@ def generate_report(name, age, gender, weight, height, allergies, cause, xray):
117
 
118
  return report_path # Return path for auto-download
119
 
120
- # Function to select a sample image
121
- def use_sample_image(sample_image_path):
122
- return sample_image_path # Returns selected sample image filepath
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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")
148
 
149
- submit_button = gr.Button("Generate Report")
 
 
150
  output_file = gr.File(label="Download Report")
 
151
 
152
- select_button.click(use_sample_image, inputs=[sample_selector], outputs=[xray])
153
-
154
- submit_button.click(
155
- generate_report,
156
- inputs=[name, age, gender, weight, height, allergies, cause, xray],
157
- outputs=[output_file],
158
- )
159
 
160
- # Launch the Gradio app
161
  if __name__ == "__main__":
162
  app.launch()
 
1
  import os
2
+ import smtplib
 
3
  import gradio as gr
4
  import tensorflow as tf
5
  import numpy as np
6
+ from email.mime.multipart import MIMEMultipart
7
+ from email.mime.text import MIMEText
8
+ from email.mime.base import MIMEBase
9
+ from email import encoders
10
  from tensorflow.keras.preprocessing import image
11
  from PIL import Image
12
  from reportlab.lib.pagesizes import letter
 
14
  from reportlab.lib import colors
15
  from reportlab.platypus import Table, TableStyle
16
 
17
+ # Ensure the "reports" directory exists
18
+ if not os.path.exists("reports"):
19
+ os.makedirs("reports")
20
+
21
  # Load the trained model
22
  model = tf.keras.models.load_model("my_keras_model.h5")
23
 
 
28
  # List of sample images
29
  sample_images = [f"samples/{img}" for img in os.listdir("samples") if img.endswith((".png", ".jpg", ".jpeg"))]
30
 
31
+ # Function to generate and save the report
32
  def generate_report(name, age, gender, weight, height, allergies, cause, xray):
33
  image_size = (224, 224)
34
 
 
63
 
64
  # Save X-ray image for report
65
  img = Image.open(xray).resize((300, 300))
66
+ img_path = f"reports/{name}_xray.png"
67
  img.save(img_path)
68
 
69
  # Generate PDF report
70
+ report_path = f"reports/{name}_fracture_report.pdf"
71
  c = canvas.Canvas(report_path, pagesize=letter)
72
+
73
  # Report title
74
  c.setFont("Helvetica-Bold", 16)
75
  c.drawString(200, 770, "Bone Fracture Detection Report")
 
89
 
90
  # Format and align tables
91
  def format_table(data):
92
+ table = Table(data, colWidths=[270, 270])
93
  table.setStyle(TableStyle([
94
  ('BACKGROUND', (0, 0), (-1, 0), colors.darkblue),
95
  ('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
 
101
  ]))
102
  return table
103
 
104
+ # Draw tables and images
105
  patient_table = format_table(patient_data)
106
  patient_table.wrapOn(c, 480, 500)
107
  patient_table.drawOn(c, 50, 620)
108
 
 
109
  c.drawInlineImage(img_path, 50, 320, width=250, height=250)
110
  c.setFont("Helvetica-Bold", 12)
111
  c.drawString(120, 290, f"Fractured: {'Yes' if diagnosed_class == 'Fractured' else 'No'}")
112
 
 
113
  treatment_table = format_table(treatment_data)
114
  treatment_table.wrapOn(c, 480, 200)
115
  treatment_table.drawOn(c, 50, 200)
 
122
 
123
  return report_path # Return path for auto-download
124
 
125
+ # Function to send email with attachment
126
+ def send_email(patient_email, report_path):
127
+ sender_email = "[email protected]"
128
+ sender_password = "your_email_password"
129
+ subject = "Bone Fracture Detection Report"
130
+ body = "Attached is your bone fracture detection report."
131
+
132
+ msg = MIMEMultipart()
133
+ msg["From"] = sender_email
134
+ msg["To"] = patient_email
135
+ msg["Subject"] = subject
136
+ msg.attach(MIMEText(body, "plain"))
137
+
138
+ with open(report_path, "rb") as attachment:
139
+ part = MIMEBase("application", "octet-stream")
140
+ part.set_payload(attachment.read())
141
+ encoders.encode_base64(part)
142
+ part.add_header("Content-Disposition", f"attachment; filename={os.path.basename(report_path)}")
143
+ msg.attach(part)
144
+
145
+ try:
146
+ server = smtplib.SMTP("smtp.gmail.com", 587)
147
+ server.starttls()
148
+ server.login(sender_email, sender_password)
149
+ server.sendmail(sender_email, patient_email, msg.as_string())
150
+ server.quit()
151
+ return "Email Sent Successfully!"
152
+ except Exception as e:
153
+ return f"Error sending email: {str(e)}"
154
 
155
  # Define Gradio Interface
156
  with gr.Blocks() as app:
157
+ gr.HTML(html_content)
158
  gr.Markdown("## Bone Fracture Detection System")
159
 
160
  with gr.Row():
161
  name = gr.Textbox(label="Patient Name")
162
  age = gr.Number(label="Age")
163
  gender = gr.Radio(["Male", "Female", "Other"], label="Gender")
164
+ patient_email = gr.Textbox(label="Patient Email")
165
+
166
  with gr.Row():
167
  weight = gr.Number(label="Weight (kg)")
168
  height = gr.Number(label="Height (cm)")
 
 
 
 
169
 
170
  with gr.Row():
171
+ allergies = gr.Textbox(label="Allergies")
172
+ cause = gr.Textbox(label="Cause of Injury")
 
 
 
173
 
174
+ xray = gr.Image(type="filepath", label="Upload X-ray Image")
175
+ generate_button = gr.Button("Generate & Download Report")
176
+ send_email_button = gr.Button("Send Report via Email")
177
  output_file = gr.File(label="Download Report")
178
+ status = gr.Textbox(label="Email Status", interactive=False)
179
 
180
+ generate_button.click(generate_report, inputs=[name, age, gender, weight, height, allergies, cause, xray], outputs=[output_file])
181
+ send_email_button.click(send_email, inputs=[patient_email, output_file], outputs=[status])
 
 
 
 
 
182
 
 
183
  if __name__ == "__main__":
184
  app.launch()