Update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,8 @@ 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")
|
@@ -21,8 +23,39 @@ 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 |
|
28 |
def predict_fracture(xray_path):
|
@@ -34,7 +67,7 @@ def generate_report(name, age, gender, weight, height, allergies, cause, xray):
|
|
34 |
|
35 |
# Predict fracture
|
36 |
prediction = predict_fracture(xray)
|
37 |
-
diagnosed_class = "
|
38 |
|
39 |
# Injury severity classification
|
40 |
severity = "Mild" if prediction < 0.3 else "Moderate" if prediction < 0.7 else "Severe"
|
@@ -56,11 +89,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
|
@@ -115,15 +148,14 @@ def generate_report(name, age, gender, weight, height, allergies, cause, xray):
|
|
115 |
|
116 |
c.save()
|
117 |
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
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)
|
127 |
gr.Markdown("## Bone Fracture Detection System")
|
128 |
|
129 |
with gr.Row():
|
@@ -138,25 +170,19 @@ with gr.Blocks() as app:
|
|
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()
|
|
|
10 |
from reportlab.pdfgen import canvas
|
11 |
from reportlab.lib import colors
|
12 |
from reportlab.platypus import Table, TableStyle
|
13 |
+
import smtplib
|
14 |
+
from email.message import EmailMessage
|
15 |
|
16 |
# Load the trained model
|
17 |
model = tf.keras.models.load_model("my_keras_model.h5")
|
|
|
23 |
# List of sample images
|
24 |
sample_images = [f"samples/{img}" for img in os.listdir("samples") if img.endswith((".png", ".jpg", ".jpeg"))]
|
25 |
|
26 |
+
# Create a folder for reports
|
27 |
+
REPORTS_DIR = "reports"
|
28 |
+
os.makedirs(REPORTS_DIR, exist_ok=True)
|
29 |
+
|
30 |
+
# Email Configuration
|
31 |
+
SENDER_EMAIL = "[email protected]" # Change this
|
32 |
+
SENDER_PASSWORD = "your-app-password" # Use an App Password if using Gmail
|
33 |
+
|
34 |
+
# Function to send email with PDF attachment
|
35 |
+
def send_email_with_attachment(to_email, file_path, patient_name):
|
36 |
+
msg = EmailMessage()
|
37 |
+
msg["Subject"] = f"Bone Fracture Report for {patient_name}"
|
38 |
+
msg["From"] = SENDER_EMAIL
|
39 |
+
msg["To"] = to_email
|
40 |
+
msg.set_content(f"Dear {patient_name},\n\nAttached is your bone fracture detection report.\n\nThank you!")
|
41 |
+
|
42 |
+
# Attach PDF
|
43 |
+
with open(file_path, "rb") as f:
|
44 |
+
file_data = f.read()
|
45 |
+
file_name = os.path.basename(file_path)
|
46 |
+
msg.add_attachment(file_data, maintype="application", subtype="pdf", filename=file_name)
|
47 |
+
|
48 |
+
# Send Email
|
49 |
+
try:
|
50 |
+
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
|
51 |
+
server.login(SENDER_EMAIL, SENDER_PASSWORD)
|
52 |
+
server.send_message(msg)
|
53 |
+
print(f"✅ Email sent to {to_email}")
|
54 |
+
except Exception as e:
|
55 |
+
print(f"❌ Failed to send email: {e}")
|
56 |
+
|
57 |
# Function to process X-ray and generate a PDF report
|
58 |
+
def generate_report(name, age, gender, weight, height, allergies, cause, xray, email):
|
59 |
image_size = (224, 224)
|
60 |
|
61 |
def predict_fracture(xray_path):
|
|
|
67 |
|
68 |
# Predict fracture
|
69 |
prediction = predict_fracture(xray)
|
70 |
+
diagnosed_class = "Normal" if prediction > 0.5 else "Fractured"
|
71 |
|
72 |
# Injury severity classification
|
73 |
severity = "Mild" if prediction < 0.3 else "Moderate" if prediction < 0.7 else "Severe"
|
|
|
89 |
|
90 |
# Save X-ray image for report
|
91 |
img = Image.open(xray).resize((300, 300))
|
92 |
+
img_path = os.path.join(REPORTS_DIR, f"{name}_xray.png")
|
93 |
img.save(img_path)
|
94 |
|
95 |
# Generate PDF report
|
96 |
+
report_path = os.path.join(REPORTS_DIR, f"{name}_fracture_report.pdf")
|
97 |
c = canvas.Canvas(report_path, pagesize=letter)
|
98 |
|
99 |
# Report title
|
|
|
148 |
|
149 |
c.save()
|
150 |
|
151 |
+
# Send Email with the Report
|
152 |
+
send_email_with_attachment(email, report_path, name)
|
153 |
+
|
154 |
+
return report_path # Return path for download
|
|
|
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 |
with gr.Row():
|
|
|
170 |
with gr.Row():
|
171 |
allergies = gr.Textbox(label="Allergies (if any)")
|
172 |
cause = gr.Textbox(label="Cause of Injury")
|
173 |
+
email = gr.Textbox(label="Patient Email") # New Email Input Field
|
174 |
|
175 |
with gr.Row():
|
176 |
xray = gr.Image(type="filepath", label="Upload X-ray Image")
|
|
|
|
|
|
|
|
|
177 |
|
178 |
submit_button = gr.Button("Generate Report")
|
179 |
output_file = gr.File(label="Download Report")
|
180 |
|
|
|
|
|
181 |
submit_button.click(
|
182 |
generate_report,
|
183 |
+
inputs=[name, age, gender, weight, height, allergies, cause, xray, email],
|
184 |
outputs=[output_file],
|
185 |
)
|
186 |
|
|
|
187 |
if __name__ == "__main__":
|
188 |
app.launch()
|