Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ 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
|
@@ -17,12 +17,12 @@ from reportlab.pdfgen import canvas
|
|
17 |
# Load the trained model
|
18 |
model = tf.keras.models.load_model("my_keras_model.h5")
|
19 |
|
20 |
-
# Store generated report
|
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:
|
26 |
return "Error: Generate the report first before sending."
|
27 |
|
28 |
report_path = report_paths[patient_name]
|
@@ -31,16 +31,7 @@ def send_email(patient_email, patient_name):
|
|
31 |
sender_password = "your_email_password"
|
32 |
|
33 |
subject = f"Bone Fracture Report for {patient_name}"
|
34 |
-
body = f""
|
35 |
-
Dear {patient_name},
|
36 |
-
|
37 |
-
Your bone fracture diagnosis report is attached.
|
38 |
-
|
39 |
-
If you have any concerns, consult your doctor.
|
40 |
-
|
41 |
-
Regards,
|
42 |
-
Hospital Team
|
43 |
-
"""
|
44 |
|
45 |
msg = EmailMessage()
|
46 |
msg["From"] = sender_email
|
@@ -48,11 +39,11 @@ def send_email(patient_email, patient_name):
|
|
48 |
msg["Subject"] = subject
|
49 |
msg.set_content(body)
|
50 |
|
51 |
-
# Attach PDF
|
52 |
with open(report_path, "rb") as file:
|
53 |
msg.add_attachment(file.read(), maintype="application", subtype="pdf", filename=os.path.basename(report_path))
|
54 |
|
55 |
-
#
|
56 |
context = ssl.create_default_context()
|
57 |
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
|
58 |
server.login(sender_email, sender_password)
|
@@ -78,13 +69,12 @@ def generate_report(name, age, gender, weight, height, allergies, cause, xray):
|
|
78 |
prediction = predict_fracture(xray)
|
79 |
diagnosed_class = "Normal" if prediction > 0.5 else "Fractured"
|
80 |
|
81 |
-
# Generate PDF
|
82 |
report_path = f"{name}_fracture_report.pdf"
|
83 |
c = canvas.Canvas(report_path, pagesize=letter)
|
84 |
|
85 |
c.setFont("Helvetica-Bold", 16)
|
86 |
c.drawString(200, 770, "Bone Fracture Detection Report")
|
87 |
-
|
88 |
c.drawString(120, 290, f"Fractured: {'Yes' if diagnosed_class == 'Fractured' else 'No'}")
|
89 |
|
90 |
# Save X-ray image for report
|
@@ -93,15 +83,14 @@ def generate_report(name, age, gender, weight, height, allergies, cause, xray):
|
|
93 |
img.save(img_path)
|
94 |
|
95 |
c.drawInlineImage(img_path, 50, 320, width=250, height=250)
|
96 |
-
|
97 |
c.save()
|
98 |
|
99 |
-
# Store file path for
|
100 |
report_paths[name] = report_path
|
101 |
|
102 |
return report_path # Return file path
|
103 |
|
104 |
-
#
|
105 |
with gr.Blocks() as app:
|
106 |
gr.Markdown("## Bone Fracture Detection System")
|
107 |
|
@@ -121,7 +110,9 @@ with gr.Blocks() as app:
|
|
121 |
with gr.Row():
|
122 |
email = gr.Textbox(label="Patient Email", type="email")
|
123 |
|
124 |
-
|
|
|
|
|
125 |
|
126 |
with gr.Row():
|
127 |
submit_button = gr.Button("Generate Report")
|
@@ -143,6 +134,6 @@ with gr.Blocks() as app:
|
|
143 |
outputs=[gr.Textbox(label="Status")]
|
144 |
)
|
145 |
|
146 |
-
# Launch
|
147 |
if __name__ == "__main__":
|
148 |
app.launch()
|
|
|
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
|
|
|
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]
|
|
|
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
|
|
|
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)
|
|
|
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
|
|
|
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 |
+
# Gradio Interface
|
94 |
with gr.Blocks() as app:
|
95 |
gr.Markdown("## Bone Fracture Detection System")
|
96 |
|
|
|
110 |
with gr.Row():
|
111 |
email = gr.Textbox(label="Patient Email", type="email")
|
112 |
|
113 |
+
# Preloaded X-ray image
|
114 |
+
default_xray_path = "default_xray.png" # Ensure this file exists in your project
|
115 |
+
xray = gr.Image(type="filepath", label="Upload X-ray Image", value=default_xray_path)
|
116 |
|
117 |
with gr.Row():
|
118 |
submit_button = gr.Button("Generate Report")
|
|
|
134 |
outputs=[gr.Textbox(label="Status")]
|
135 |
)
|
136 |
|
137 |
+
# Launch app
|
138 |
if __name__ == "__main__":
|
139 |
app.launch()
|