ftx7go commited on
Commit
bc1208d
·
verified ·
1 Parent(s): 0077984

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -37
app.py CHANGED
@@ -1,10 +1,13 @@
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
10
  from reportlab.pdfgen import canvas
@@ -18,11 +21,14 @@ model = tf.keras.models.load_model("my_keras_model.h5")
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,7 +40,7 @@ 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"
@@ -56,11 +62,11 @@ 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
@@ -117,46 +123,57 @@ 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 tensorflow.keras.preprocessing import image
7
+ from email.mime.multipart import MIMEMultipart
8
+ from email.mime.text import MIMEText
9
+ from email.mime.base import MIMEBase
10
+ from email import encoders
11
  from PIL import Image
12
  from reportlab.lib.pagesizes import letter
13
  from reportlab.pdfgen import canvas
 
21
  with open("templates/re.html", "r", encoding="utf-8") as file:
22
  html_content = file.read()
23
 
24
+ # Ensure reports directory exists
25
+ os.makedirs("reports", exist_ok=True)
26
+
27
  # List of sample images
28
  sample_images = [f"samples/{img}" for img in os.listdir("samples") if img.endswith((".png", ".jpg", ".jpeg"))]
29
 
30
  # Function to process X-ray and generate a PDF report
31
+ def generate_report(name, age, gender, weight, height, allergies, cause, email, xray):
32
  image_size = (224, 224)
33
 
34
  def predict_fracture(xray_path):
 
40
 
41
  # Predict fracture
42
  prediction = predict_fracture(xray)
43
+ diagnosed_class = "Normal" if prediction > 0.5 else "Fractured"
44
 
45
  # Injury severity classification
46
  severity = "Mild" if prediction < 0.3 else "Moderate" if prediction < 0.7 else "Severe"
 
62
 
63
  # Save X-ray image for report
64
  img = Image.open(xray).resize((300, 300))
65
+ img_path = f"reports/{name}_xray.png"
66
  img.save(img_path)
67
 
68
  # Generate PDF report
69
+ report_path = f"reports/{name}_fracture_report.pdf"
70
  c = canvas.Canvas(report_path, pagesize=letter)
71
 
72
  # Report title
 
123
 
124
  return report_path # Return path for auto-download
125
 
126
+ # Function to send email with the report attached
127
+ def send_email_report(email, report_path):
128
+ sender_email = "[email protected]"
129
+ sender_password = "1w3r5y7i9pW$" # Use an app password or environment variable
130
+ subject = "Your Bone Fracture Detection Report"
131
+
132
+ msg = MIMEMultipart()
133
+ msg["From"] = sender_email
134
+ msg["To"] = email
135
+ msg["Subject"] = subject
136
+
137
+ body = "Dear Patient,\n\nPlease find attached your bone fracture detection report.\n\nBest Regards,\nYour Medical Team"
138
+ msg.attach(MIMEText(body, "plain"))
139
+
140
+ with open(report_path, "rb") as attachment:
141
+ part = MIMEBase("application", "octet-stream")
142
+ part.set_payload(attachment.read())
143
+ encoders.encode_base64(part)
144
+ part.add_header("Content-Disposition", f"attachment; filename={os.path.basename(report_path)}")
145
+ msg.attach(part)
146
+
147
+ try:
148
+ with smtplib.SMTP("smtp.gmail.com", 587) as server:
149
+ server.starttls()
150
+ server.login(sender_email, sender_password)
151
+ server.sendmail(sender_email, email, msg.as_string())
152
+ return "Email Sent Successfully"
153
+ except Exception as e:
154
+ return f"Failed to Send Email: {str(e)}"
155
 
156
  # Define Gradio Interface
157
  with gr.Blocks() as app:
158
+ gr.HTML(html_content)
159
  gr.Markdown("## Bone Fracture Detection System")
 
 
 
 
 
 
 
 
 
 
 
 
 
160
 
161
+ name = gr.Textbox(label="Patient Name")
162
+ age = gr.Number(label="Age")
163
+ gender = gr.Radio(["Male", "Female", "Other"], label="Gender")
164
+ weight = gr.Number(label="Weight (kg)")
165
+ height = gr.Number(label="Height (cm)")
166
+ allergies = gr.Textbox(label="Allergies (if any)")
167
+ cause = gr.Textbox(label="Cause of Injury")
168
+ email = gr.Textbox(label="Patient Email")
169
+ xray = gr.Image(type="filepath", label="Upload X-ray Image")
170
 
171
  submit_button = gr.Button("Generate Report")
172
  output_file = gr.File(label="Download Report")
173
+ email_button = gr.Button("Send Email Report")
174
 
175
+ submit_button.click(generate_report, inputs=[name, age, gender, weight, height, allergies, cause, email, xray], outputs=[output_file])
176
+ email_button.click(send_email_report, inputs=[email, output_file], outputs=[])
 
 
 
 
 
177
 
 
178
  if __name__ == "__main__":
179
  app.launch()