Delete app.py
Browse files
app.py
DELETED
@@ -1,160 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import smtplib
|
3 |
-
import gradio as gr
|
4 |
-
import tensorflow as tf
|
5 |
-
import numpy as np
|
6 |
-
from email.message import EmailMessage
|
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
|
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 |
-
|
17 |
-
# Ensure the reports directory exists
|
18 |
-
REPORTS_DIR = "reports"
|
19 |
-
os.makedirs(REPORTS_DIR, exist_ok=True)
|
20 |
-
|
21 |
-
# Read HTML content from `re.html`
|
22 |
-
with open("templates/re.html", "r", encoding="utf-8") as file:
|
23 |
-
html_content = file.read()
|
24 |
-
|
25 |
-
# List of sample images
|
26 |
-
sample_images = [f"samples/{img}" for img in os.listdir("samples") if img.endswith((".png", ".jpg", ".jpeg"))]
|
27 |
-
|
28 |
-
# Function to process X-ray and generate a PDF report
|
29 |
-
def generate_report(name, age, gender, weight, height, allergies, cause, xray, email):
|
30 |
-
image_size = (224, 224)
|
31 |
-
|
32 |
-
def predict_fracture(xray_path):
|
33 |
-
img = Image.open(xray_path).resize(image_size)
|
34 |
-
img_array = image.img_to_array(img) / 255.0
|
35 |
-
img_array = np.expand_dims(img_array, axis=0)
|
36 |
-
prediction = model.predict(img_array)[0][0]
|
37 |
-
return prediction
|
38 |
-
|
39 |
-
# Predict fracture
|
40 |
-
prediction = predict_fracture(xray)
|
41 |
-
diagnosed_class = "Normal" if prediction > 0.5 else "Fractured"
|
42 |
-
severity = "Mild" if prediction < 0.3 else "Moderate" if prediction < 0.7 else "Severe"
|
43 |
-
|
44 |
-
# File paths
|
45 |
-
report_filename = f"{name}_fracture_report.pdf"
|
46 |
-
report_path = os.path.join(REPORTS_DIR, report_filename)
|
47 |
-
|
48 |
-
# Generate PDF report
|
49 |
-
c = canvas.Canvas(report_path, pagesize=letter)
|
50 |
-
c.setFont("Helvetica-Bold", 16)
|
51 |
-
c.drawString(200, 770, "Bone Fracture Detection Report")
|
52 |
-
|
53 |
-
# Patient details
|
54 |
-
patient_data = [
|
55 |
-
["Patient Name", name], ["Age", age], ["Gender", gender],
|
56 |
-
["Weight", f"{weight} kg"], ["Height", f"{height} cm"],
|
57 |
-
["Allergies", allergies if allergies else "None"],
|
58 |
-
["Cause of Injury", cause if cause else "Not Provided"],
|
59 |
-
["Diagnosis", diagnosed_class], ["Injury Severity", severity]
|
60 |
-
]
|
61 |
-
|
62 |
-
# Format table function
|
63 |
-
def format_table(data):
|
64 |
-
table = Table(data, colWidths=[270, 270])
|
65 |
-
table.setStyle(TableStyle([
|
66 |
-
('BACKGROUND', (0, 0), (-1, 0), colors.darkblue),
|
67 |
-
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
|
68 |
-
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
|
69 |
-
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
|
70 |
-
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
|
71 |
-
('GRID', (0, 0), (-1, -1), 1, colors.black),
|
72 |
-
('VALIGN', (0, 0), (-1, -1), 'MIDDLE')
|
73 |
-
]))
|
74 |
-
return table
|
75 |
-
|
76 |
-
# Draw patient details table
|
77 |
-
patient_table = format_table(patient_data)
|
78 |
-
patient_table.wrapOn(c, 480, 500)
|
79 |
-
patient_table.drawOn(c, 50, 620)
|
80 |
-
|
81 |
-
# Save and return report path
|
82 |
-
c.save()
|
83 |
-
|
84 |
-
# Send email with report
|
85 |
-
send_email_with_attachment(email, report_path, name)
|
86 |
-
|
87 |
-
return report_path # For download
|
88 |
-
|
89 |
-
# Function to send email with PDF attachment
|
90 |
-
def send_email_with_attachment(to_email, file_path, patient_name):
|
91 |
-
sender_email = "[email protected]" # Replace with your email
|
92 |
-
sender_password = "your-email-password" # Use App Passwords if using Gmail
|
93 |
-
|
94 |
-
msg = EmailMessage()
|
95 |
-
msg["Subject"] = f"Bone Fracture Report for {patient_name}"
|
96 |
-
msg["From"] = sender_email
|
97 |
-
msg["To"] = to_email
|
98 |
-
msg.set_content(f"Dear {patient_name},\n\nAttached is your bone fracture detection report.\n\nThank you!")
|
99 |
-
|
100 |
-
# Attach PDF file
|
101 |
-
with open(file_path, "rb") as f:
|
102 |
-
file_data = f.read()
|
103 |
-
file_name = os.path.basename(file_path)
|
104 |
-
msg.add_attachment(file_data, maintype="application", subtype="pdf", filename=file_name)
|
105 |
-
|
106 |
-
# Send email
|
107 |
-
try:
|
108 |
-
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
|
109 |
-
server.login(sender_email, sender_password)
|
110 |
-
server.send_message(msg)
|
111 |
-
print(f"Email sent to {to_email}")
|
112 |
-
except Exception as e:
|
113 |
-
print(f"Failed to send email: {e}")
|
114 |
-
|
115 |
-
# Function to select a sample image
|
116 |
-
def use_sample_image(sample_image_path):
|
117 |
-
return sample_image_path
|
118 |
-
|
119 |
-
# Define Gradio Interface
|
120 |
-
with gr.Blocks() as app:
|
121 |
-
gr.HTML(html_content)
|
122 |
-
gr.Markdown("## Bone Fracture Detection System")
|
123 |
-
|
124 |
-
with gr.Row():
|
125 |
-
name = gr.Textbox(label="Patient Name")
|
126 |
-
age = gr.Number(label="Age")
|
127 |
-
gender = gr.Radio(["Male", "Female", "Other"], label="Gender")
|
128 |
-
|
129 |
-
with gr.Row():
|
130 |
-
weight = gr.Number(label="Weight (kg)")
|
131 |
-
height = gr.Number(label="Height (cm)")
|
132 |
-
|
133 |
-
with gr.Row():
|
134 |
-
allergies = gr.Textbox(label="Allergies (if any)")
|
135 |
-
cause = gr.Textbox(label="Cause of Injury")
|
136 |
-
|
137 |
-
with gr.Row():
|
138 |
-
email = gr.Textbox(label="Patient Email")
|
139 |
-
|
140 |
-
with gr.Row():
|
141 |
-
xray = gr.Image(type="filepath", label="Upload X-ray Image")
|
142 |
-
|
143 |
-
with gr.Row():
|
144 |
-
sample_selector = gr.Dropdown(choices=sample_images, label="Use Sample Image")
|
145 |
-
select_button = gr.Button("Load Sample Image")
|
146 |
-
|
147 |
-
submit_button = gr.Button("Generate Report & Send Email")
|
148 |
-
output_file = gr.File(label="Download Report")
|
149 |
-
|
150 |
-
select_button.click(use_sample_image, inputs=[sample_selector], outputs=[xray])
|
151 |
-
|
152 |
-
submit_button.click(
|
153 |
-
generate_report,
|
154 |
-
inputs=[name, age, gender, weight, height, allergies, cause, xray, email],
|
155 |
-
outputs=[output_file],
|
156 |
-
)
|
157 |
-
|
158 |
-
# Launch the Gradio app
|
159 |
-
if __name__ == "__main__":
|
160 |
-
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|