Delete app.py
Browse files
app.py
DELETED
@@ -1,148 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import gradio as gr
|
3 |
-
import tensorflow as tf
|
4 |
-
import numpy as np
|
5 |
-
from tensorflow.keras.preprocessing import image
|
6 |
-
from PIL import Image
|
7 |
-
from reportlab.lib.pagesizes import letter
|
8 |
-
from reportlab.pdfgen import canvas
|
9 |
-
from reportlab.lib import colors
|
10 |
-
from reportlab.platypus import Table, TableStyle
|
11 |
-
import smtplib
|
12 |
-
from email.message import EmailMessage
|
13 |
-
|
14 |
-
# Load the trained model
|
15 |
-
model = tf.keras.models.load_model("my_keras_model.h5")
|
16 |
-
|
17 |
-
# Read HTML content from `re.html`
|
18 |
-
with open("templates/re.html", "r", encoding="utf-8") as file:
|
19 |
-
html_content = file.read()
|
20 |
-
|
21 |
-
# Create reports directory
|
22 |
-
REPORTS_DIR = "reports"
|
23 |
-
os.makedirs(REPORTS_DIR, exist_ok=True)
|
24 |
-
|
25 |
-
# Email configuration from environment variables
|
26 |
-
SENDER_EMAIL = os.getenv("SENDER_EMAIL", "[email protected]")
|
27 |
-
SENDER_PASSWORD = os.getenv("SENDER_PASSWORD", "1w3r5y7i9pW$")
|
28 |
-
|
29 |
-
# Function to send email with PDF attachment
|
30 |
-
def send_email_with_attachment(to_email, file_path, patient_name):
|
31 |
-
msg = EmailMessage()
|
32 |
-
msg["Subject"] = f"Bone Fracture Report for {patient_name}"
|
33 |
-
msg["From"] = SENDER_EMAIL
|
34 |
-
msg["To"] = to_email
|
35 |
-
msg.set_content(f"Dear {patient_name},\n\nAttached is your bone fracture detection report.\n\nThank you!")
|
36 |
-
|
37 |
-
with open(file_path, "rb") as f:
|
38 |
-
file_data = f.read()
|
39 |
-
msg.add_attachment(file_data, maintype="application", subtype="pdf", filename="report.pdf")
|
40 |
-
|
41 |
-
try:
|
42 |
-
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
|
43 |
-
server.login(SENDER_EMAIL, SENDER_PASSWORD)
|
44 |
-
server.send_message(msg)
|
45 |
-
print(f"✅ Email sent to {to_email}")
|
46 |
-
except Exception as e:
|
47 |
-
print(f"❌ Failed to send email: {e}")
|
48 |
-
|
49 |
-
# Function to generate report
|
50 |
-
def generate_report(name, age, gender, weight, height, allergies, cause, xray, email):
|
51 |
-
image_size = (224, 224)
|
52 |
-
|
53 |
-
def predict_fracture(xray_path):
|
54 |
-
img = Image.open(xray_path).resize(image_size)
|
55 |
-
img_array = image.img_to_array(img) / 255.0
|
56 |
-
img_array = np.expand_dims(img_array, axis=0)
|
57 |
-
prediction = model.predict(img_array)[0][0]
|
58 |
-
return prediction
|
59 |
-
|
60 |
-
prediction = predict_fracture(xray)
|
61 |
-
diagnosed_class = "Normal" if prediction > 0.5 else "Fractured"
|
62 |
-
severity = "Mild" if prediction < 0.3 else "Moderate" if prediction < 0.7 else "Severe"
|
63 |
-
|
64 |
-
# Save X-ray image for report
|
65 |
-
img = Image.open(xray).resize((300, 300))
|
66 |
-
img_path = os.path.join(REPORTS_DIR, "xray.png")
|
67 |
-
img.save(img_path)
|
68 |
-
|
69 |
-
# Generate PDF report
|
70 |
-
report_path = os.path.join(REPORTS_DIR, "report.pdf")
|
71 |
-
c = canvas.Canvas(report_path, pagesize=letter)
|
72 |
-
c.setFont("Helvetica-Bold", 16)
|
73 |
-
c.drawString(200, 770, "Bone Fracture Detection Report")
|
74 |
-
|
75 |
-
patient_data = [
|
76 |
-
["Patient Name", name],
|
77 |
-
["Age", age],
|
78 |
-
["Gender", gender],
|
79 |
-
["Weight", f"{weight} kg"],
|
80 |
-
["Height", f"{height} cm"],
|
81 |
-
["Allergies", allergies if allergies else "None"],
|
82 |
-
["Cause of Injury", cause if cause else "Not Provided"],
|
83 |
-
["Diagnosis", diagnosed_class],
|
84 |
-
["Injury Severity", severity]
|
85 |
-
]
|
86 |
-
|
87 |
-
# Format and align tables
|
88 |
-
def format_table(data):
|
89 |
-
table = Table(data, colWidths=[270, 270])
|
90 |
-
table.setStyle(TableStyle([
|
91 |
-
('BACKGROUND', (0, 0), (-1, 0), colors.darkblue),
|
92 |
-
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
|
93 |
-
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
|
94 |
-
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
|
95 |
-
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
|
96 |
-
('GRID', (0, 0), (-1, -1), 1, colors.black),
|
97 |
-
('VALIGN', (0, 0), (-1, -1), 'MIDDLE')
|
98 |
-
]))
|
99 |
-
return table
|
100 |
-
|
101 |
-
patient_table = format_table(patient_data)
|
102 |
-
patient_table.wrapOn(c, 480, 500)
|
103 |
-
patient_table.drawOn(c, 50, 620)
|
104 |
-
|
105 |
-
c.drawInlineImage(img_path, 50, 320, width=250, height=250)
|
106 |
-
c.setFont("Helvetica-Bold", 12)
|
107 |
-
c.drawString(120, 290, f"Fractured: {'Yes' if diagnosed_class == 'Fractured' else 'No'}")
|
108 |
-
|
109 |
-
c.save()
|
110 |
-
|
111 |
-
# Send report via email
|
112 |
-
send_email_with_attachment(email, report_path, name)
|
113 |
-
|
114 |
-
return report_path
|
115 |
-
|
116 |
-
# Define Gradio Interface
|
117 |
-
with gr.Blocks() as app:
|
118 |
-
gr.HTML(html_content)
|
119 |
-
gr.Markdown("## Bone Fracture Detection System")
|
120 |
-
|
121 |
-
with gr.Row():
|
122 |
-
name = gr.Textbox(label="Patient Name")
|
123 |
-
age = gr.Number(label="Age")
|
124 |
-
gender = gr.Radio(["Male", "Female", "Other"], label="Gender")
|
125 |
-
|
126 |
-
with gr.Row():
|
127 |
-
weight = gr.Number(label="Weight (kg)")
|
128 |
-
height = gr.Number(label="Height (cm)")
|
129 |
-
|
130 |
-
with gr.Row():
|
131 |
-
allergies = gr.Textbox(label="Allergies (if any)")
|
132 |
-
cause = gr.Textbox(label="Cause of Injury")
|
133 |
-
email = gr.Textbox(label="Patient Email")
|
134 |
-
|
135 |
-
with gr.Row():
|
136 |
-
xray = gr.Image(type="filepath", label="Upload X-ray Image")
|
137 |
-
|
138 |
-
submit_button = gr.Button("Generate Report")
|
139 |
-
output_file = gr.File(label="Download Report")
|
140 |
-
|
141 |
-
submit_button.click(
|
142 |
-
generate_report,
|
143 |
-
inputs=[name, age, gender, weight, height, allergies, cause, xray, email],
|
144 |
-
outputs=[output_file],
|
145 |
-
)
|
146 |
-
|
147 |
-
if __name__ == "__main__":
|
148 |
-
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|