Delete app.py
Browse files
app.py
DELETED
@@ -1,150 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import smtplib
|
3 |
-
import ssl
|
4 |
-
from email.message import EmailMessage
|
5 |
-
|
6 |
-
# Force TensorFlow to use CPU
|
7 |
-
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
8 |
-
|
9 |
-
import gradio as gr
|
10 |
-
import tensorflow as tf
|
11 |
-
import numpy as np
|
12 |
-
from tensorflow.keras.preprocessing import image
|
13 |
-
from PIL import Image
|
14 |
-
from reportlab.lib.pagesizes import letter
|
15 |
-
from reportlab.pdfgen import canvas
|
16 |
-
|
17 |
-
# Load the trained model
|
18 |
-
model = tf.keras.models.load_model("my_keras_model.h5")
|
19 |
-
|
20 |
-
# Store generated report paths
|
21 |
-
report_paths = {}
|
22 |
-
|
23 |
-
# Function to send email
|
24 |
-
def send_email(patient_email, patient_name):
|
25 |
-
if patient_name not in report_paths or not os.path.exists(report_paths[patient_name]):
|
26 |
-
return "Error: Generate the report first before sending."
|
27 |
-
|
28 |
-
report_path = report_paths[patient_name]
|
29 |
-
|
30 |
-
sender_email = "[email protected]"
|
31 |
-
sender_password = "your_email_password"
|
32 |
-
|
33 |
-
subject = f"Bone Fracture Report for {patient_name}"
|
34 |
-
body = f"Dear {patient_name},\n\nYour bone fracture diagnosis report is attached.\n\nBest Regards,\nHospital Team"
|
35 |
-
|
36 |
-
msg = EmailMessage()
|
37 |
-
msg["From"] = sender_email
|
38 |
-
msg["To"] = patient_email
|
39 |
-
msg["Subject"] = subject
|
40 |
-
msg.set_content(body)
|
41 |
-
|
42 |
-
# Attach PDF
|
43 |
-
with open(report_path, "rb") as file:
|
44 |
-
msg.add_attachment(file.read(), maintype="application", subtype="pdf", filename=os.path.basename(report_path))
|
45 |
-
|
46 |
-
# Send email securely
|
47 |
-
context = ssl.create_default_context()
|
48 |
-
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
|
49 |
-
server.login(sender_email, sender_password)
|
50 |
-
server.send_message(msg)
|
51 |
-
|
52 |
-
return f"Report sent successfully to {patient_email}!"
|
53 |
-
|
54 |
-
# Function to generate report
|
55 |
-
def generate_report(name, age, gender, weight, height, allergies, cause, xray):
|
56 |
-
if not name:
|
57 |
-
return "Error: Please enter a patient name."
|
58 |
-
|
59 |
-
image_size = (224, 224)
|
60 |
-
|
61 |
-
def predict_fracture(xray_path):
|
62 |
-
img = Image.open(xray_path).resize(image_size)
|
63 |
-
img_array = image.img_to_array(img) / 255.0
|
64 |
-
img_array = np.expand_dims(img_array, axis=0)
|
65 |
-
prediction = model.predict(img_array)[0][0]
|
66 |
-
return prediction
|
67 |
-
|
68 |
-
# Predict fracture
|
69 |
-
prediction = predict_fracture(xray)
|
70 |
-
diagnosed_class = "Normal" if prediction > 0.5 else "Fractured"
|
71 |
-
|
72 |
-
# Generate PDF
|
73 |
-
report_path = f"{name}_fracture_report.pdf"
|
74 |
-
c = canvas.Canvas(report_path, pagesize=letter)
|
75 |
-
|
76 |
-
c.setFont("Helvetica-Bold", 16)
|
77 |
-
c.drawString(200, 770, "Bone Fracture Detection Report")
|
78 |
-
c.drawString(120, 290, f"Fractured: {'Yes' if diagnosed_class == 'Fractured' else 'No'}")
|
79 |
-
|
80 |
-
# Save X-ray image for report
|
81 |
-
img = Image.open(xray).resize((300, 300))
|
82 |
-
img_path = f"{name}_xray.png"
|
83 |
-
img.save(img_path)
|
84 |
-
|
85 |
-
c.drawInlineImage(img_path, 50, 320, width=250, height=250)
|
86 |
-
c.save()
|
87 |
-
|
88 |
-
# Store file path for sending email
|
89 |
-
report_paths[name] = report_path
|
90 |
-
|
91 |
-
return report_path # Return file path
|
92 |
-
|
93 |
-
# Path to samples folder
|
94 |
-
samples_folder = "samples"
|
95 |
-
|
96 |
-
# Ensure samples directory exists
|
97 |
-
if os.path.exists(samples_folder) and os.path.isdir(samples_folder):
|
98 |
-
sample_images = [os.path.join(samples_folder, f) for f in os.listdir(samples_folder) if f.endswith(('.png', '.jpg', '.jpeg'))]
|
99 |
-
else:
|
100 |
-
sample_images = []
|
101 |
-
|
102 |
-
# Preloaded image (if available)
|
103 |
-
preloaded_image = sample_images[0] if sample_images else None
|
104 |
-
|
105 |
-
# Gradio Interface
|
106 |
-
with gr.Blocks() as app:
|
107 |
-
gr.Markdown("## Bone Fracture Detection System")
|
108 |
-
|
109 |
-
with gr.Row():
|
110 |
-
name = gr.Textbox(label="Patient Name")
|
111 |
-
age = gr.Number(label="Age")
|
112 |
-
gender = gr.Radio(["Male", "Female", "Other"], label="Gender")
|
113 |
-
|
114 |
-
with gr.Row():
|
115 |
-
weight = gr.Number(label="Weight (kg)")
|
116 |
-
height = gr.Number(label="Height (cm)")
|
117 |
-
|
118 |
-
with gr.Row():
|
119 |
-
allergies = gr.Textbox(label="Allergies")
|
120 |
-
cause = gr.Textbox(label="Cause of Injury")
|
121 |
-
|
122 |
-
with gr.Row():
|
123 |
-
email = gr.Textbox(label="Patient Email", type="email")
|
124 |
-
|
125 |
-
# X-ray image upload with preloaded image from "samples"
|
126 |
-
xray = gr.Image(type="filepath", label="Upload X-ray Image", value=preloaded_image)
|
127 |
-
|
128 |
-
with gr.Row():
|
129 |
-
submit_button = gr.Button("Generate Report")
|
130 |
-
send_email_button = gr.Button("Send Report via Email")
|
131 |
-
|
132 |
-
output_file = gr.File(label="Download Report")
|
133 |
-
|
134 |
-
# Generate Report Button
|
135 |
-
submit_button.click(
|
136 |
-
generate_report,
|
137 |
-
inputs=[name, age, gender, weight, height, allergies, cause, xray],
|
138 |
-
outputs=[output_file]
|
139 |
-
)
|
140 |
-
|
141 |
-
# Send Email Button
|
142 |
-
send_email_button.click(
|
143 |
-
send_email,
|
144 |
-
inputs=[email, name],
|
145 |
-
outputs=[gr.Textbox(label="Status")]
|
146 |
-
)
|
147 |
-
|
148 |
-
# Launch app
|
149 |
-
if __name__ == "__main__":
|
150 |
-
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|