Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,4 @@
|
|
1 |
import os
|
2 |
-
os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # Force TensorFlow to use CPU
|
3 |
-
|
4 |
import gradio as gr
|
5 |
import tensorflow as tf
|
6 |
import numpy as np
|
@@ -20,16 +18,13 @@ model = tf.keras.models.load_model("my_keras_model.h5")
|
|
20 |
with open("templates/re.html", "r", encoding="utf-8") as file:
|
21 |
html_content = file.read()
|
22 |
|
23 |
-
#
|
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
|
31 |
-
SENDER_EMAIL = "
|
32 |
-
SENDER_PASSWORD = "
|
33 |
|
34 |
# Function to send email with PDF attachment
|
35 |
def send_email_with_attachment(to_email, file_path, patient_name):
|
@@ -39,13 +34,10 @@ def send_email_with_attachment(to_email, file_path, patient_name):
|
|
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 |
-
|
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)
|
@@ -54,7 +46,7 @@ def send_email_with_attachment(to_email, file_path, patient_name):
|
|
54 |
except Exception as e:
|
55 |
print(f"❌ Failed to send email: {e}")
|
56 |
|
57 |
-
# Function to
|
58 |
def generate_report(name, age, gender, weight, height, allergies, cause, xray, email):
|
59 |
image_size = (224, 224)
|
60 |
|
@@ -65,42 +57,21 @@ def generate_report(name, age, gender, weight, height, allergies, cause, xray, e
|
|
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 |
-
# Injury severity classification
|
73 |
severity = "Mild" if prediction < 0.3 else "Moderate" if prediction < 0.7 else "Severe"
|
74 |
|
75 |
-
# Treatment details table
|
76 |
-
treatment_data = [
|
77 |
-
["Severity Level", "Recommended Treatment", "Recovery Duration"],
|
78 |
-
["Mild", "Rest, pain relievers, and follow-up X-ray", "4-6 weeks"],
|
79 |
-
["Moderate", "Plaster cast, minor surgery if needed", "6-10 weeks"],
|
80 |
-
["Severe", "Major surgery, metal implants, physiotherapy", "Several months"]
|
81 |
-
]
|
82 |
-
|
83 |
-
# Estimated cost & duration table
|
84 |
-
cost_duration_data = [
|
85 |
-
["Hospital Type", "Estimated Cost", "Recovery Time"],
|
86 |
-
["Government Hospital", f"₹{2000 if severity == 'Mild' else 8000 if severity == 'Moderate' else 20000} - ₹{5000 if severity == 'Mild' else 15000 if severity == 'Moderate' else 50000}", "4-12 weeks"],
|
87 |
-
["Private Hospital", f"₹{10000 if severity == 'Mild' else 30000 if severity == 'Moderate' else 100000}+", "6 weeks - Several months"]
|
88 |
-
]
|
89 |
-
|
90 |
# Save X-ray image for report
|
91 |
img = Image.open(xray).resize((300, 300))
|
92 |
-
img_path = os.path.join(REPORTS_DIR,
|
93 |
img.save(img_path)
|
94 |
|
95 |
# Generate PDF report
|
96 |
-
report_path = os.path.join(REPORTS_DIR,
|
97 |
c = canvas.Canvas(report_path, pagesize=letter)
|
98 |
-
|
99 |
-
# Report title
|
100 |
c.setFont("Helvetica-Bold", 16)
|
101 |
c.drawString(200, 770, "Bone Fracture Detection Report")
|
102 |
|
103 |
-
# Patient details table
|
104 |
patient_data = [
|
105 |
["Patient Name", name],
|
106 |
["Age", age],
|
@@ -115,7 +86,7 @@ def generate_report(name, age, gender, weight, height, allergies, cause, xray, e
|
|
115 |
|
116 |
# Format and align tables
|
117 |
def format_table(data):
|
118 |
-
table = Table(data, colWidths=[270, 270])
|
119 |
table.setStyle(TableStyle([
|
120 |
('BACKGROUND', (0, 0), (-1, 0), colors.darkblue),
|
121 |
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
|
@@ -127,50 +98,39 @@ def generate_report(name, age, gender, weight, height, allergies, cause, xray, e
|
|
127 |
]))
|
128 |
return table
|
129 |
|
130 |
-
# Draw patient details table
|
131 |
patient_table = format_table(patient_data)
|
132 |
patient_table.wrapOn(c, 480, 500)
|
133 |
patient_table.drawOn(c, 50, 620)
|
134 |
|
135 |
-
# Load and insert X-ray image
|
136 |
c.drawInlineImage(img_path, 50, 320, width=250, height=250)
|
137 |
c.setFont("Helvetica-Bold", 12)
|
138 |
c.drawString(120, 290, f"Fractured: {'Yes' if diagnosed_class == 'Fractured' else 'No'}")
|
139 |
|
140 |
-
# Draw treatment and cost tables
|
141 |
-
treatment_table = format_table(treatment_data)
|
142 |
-
treatment_table.wrapOn(c, 480, 200)
|
143 |
-
treatment_table.drawOn(c, 50, 200)
|
144 |
-
|
145 |
-
cost_table = format_table(cost_duration_data)
|
146 |
-
cost_table.wrapOn(c, 480, 150)
|
147 |
-
cost_table.drawOn(c, 50, 80)
|
148 |
-
|
149 |
c.save()
|
150 |
|
151 |
-
# Send
|
152 |
send_email_with_attachment(email, report_path, name)
|
153 |
|
154 |
-
return report_path
|
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():
|
162 |
name = gr.Textbox(label="Patient Name")
|
163 |
age = gr.Number(label="Age")
|
164 |
gender = gr.Radio(["Male", "Female", "Other"], label="Gender")
|
165 |
-
|
166 |
with gr.Row():
|
167 |
weight = gr.Number(label="Weight (kg)")
|
168 |
height = gr.Number(label="Height (cm)")
|
169 |
-
|
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")
|
174 |
|
175 |
with gr.Row():
|
176 |
xray = gr.Image(type="filepath", label="Upload X-ray Image")
|
|
|
1 |
import os
|
|
|
|
|
2 |
import gradio as gr
|
3 |
import tensorflow as tf
|
4 |
import numpy as np
|
|
|
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", "your-email@gmail.com")
|
27 |
+
SENDER_PASSWORD = os.getenv("SENDER_PASSWORD", "your-app-password")
|
28 |
|
29 |
# Function to send email with PDF attachment
|
30 |
def send_email_with_attachment(to_email, file_path, patient_name):
|
|
|
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)
|
|
|
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 |
|
|
|
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],
|
|
|
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),
|
|
|
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")
|