Update app.py
Browse files
app.py
CHANGED
@@ -1,185 +1,150 @@
|
|
1 |
import os
|
2 |
-
import smtplib
|
3 |
import gradio as gr
|
4 |
-
|
5 |
-
import
|
6 |
from email.mime.multipart import MIMEMultipart
|
7 |
from email.mime.text import MIMEText
|
8 |
from email.mime.base import MIMEBase
|
9 |
from email import encoders
|
10 |
-
from tensorflow.keras.preprocessing import image
|
11 |
-
from PIL import Image
|
12 |
-
from reportlab.lib.pagesizes import letter
|
13 |
-
from reportlab.pdfgen import canvas
|
14 |
-
from reportlab.lib.utils import simpleSplit
|
15 |
|
16 |
-
#
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
#
|
20 |
-
|
|
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
-
sender_email = "[email protected]"
|
25 |
-
sender_password = "your_email_password"
|
26 |
-
|
27 |
-
try:
|
28 |
-
msg = MIMEMultipart()
|
29 |
-
msg["From"] = sender_email
|
30 |
-
msg["To"] = patient_email
|
31 |
-
msg["Subject"] = "Your Bone Fracture Report"
|
32 |
|
33 |
-
|
34 |
-
msg.attach(MIMEText(body, "plain"))
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
|
|
43 |
server = smtplib.SMTP("smtp.gmail.com", 587)
|
44 |
server.starttls()
|
45 |
server.login(sender_email, sender_password)
|
46 |
-
server.sendmail(sender_email,
|
47 |
server.quit()
|
48 |
-
|
49 |
-
return "Report sent successfully!"
|
50 |
except Exception as e:
|
51 |
-
|
52 |
-
|
53 |
-
# Generate PDF report
|
54 |
-
def generate_report(name, age, gender, weight, height, allergies, cause, xray, email):
|
55 |
-
image_size = (224, 224)
|
56 |
-
|
57 |
-
def predict_fracture(xray_path):
|
58 |
-
img = Image.open(xray_path).resize(image_size)
|
59 |
-
img_array = image.img_to_array(img) / 255.0
|
60 |
-
img_array = np.expand_dims(img_array, axis=0)
|
61 |
-
prediction = model.predict(img_array)[0][0]
|
62 |
-
return prediction
|
63 |
-
|
64 |
-
prediction = predict_fracture(xray)
|
65 |
-
diagnosed_class = "Normal" if prediction > 0.5 else "Fractured"
|
66 |
-
severity = "Mild" if prediction < 0.3 else "Moderate" if prediction < 0.7 else "Severe"
|
67 |
-
|
68 |
-
# Save X-ray image
|
69 |
-
img = Image.open(xray).resize((300, 300))
|
70 |
-
img_path = f"{name}_xray.png"
|
71 |
-
img.save(img_path)
|
72 |
-
|
73 |
-
# PDF Report
|
74 |
-
report_path = f"{name}_fracture_report.pdf"
|
75 |
-
c = canvas.Canvas(report_path, pagesize=letter)
|
76 |
-
|
77 |
-
# Header
|
78 |
-
c.setFont("Helvetica-Bold", 16)
|
79 |
-
c.drawCentredString(300, 770, "XYZ Hospital, New Delhi")
|
80 |
-
c.setFont("Helvetica", 12)
|
81 |
-
c.drawCentredString(300, 750, "123 Health Street, New Delhi, India")
|
82 |
-
c.line(50, 740, 550, 740)
|
83 |
-
|
84 |
-
# Patient Details
|
85 |
-
c.setFont("Helvetica-Bold", 14)
|
86 |
-
c.drawString(50, 710, "Patient Information:")
|
87 |
-
c.setFont("Helvetica", 12)
|
88 |
-
details = [
|
89 |
-
f"Name: {name}",
|
90 |
-
f"Age: {age}",
|
91 |
-
f"Gender: {gender}",
|
92 |
-
f"Weight: {weight} kg",
|
93 |
-
f"Height: {height} cm",
|
94 |
-
f"Allergies: {allergies if allergies else 'None'}",
|
95 |
-
f"Cause of Injury: {cause if cause else 'Not Provided'}"
|
96 |
-
]
|
97 |
-
y = 690
|
98 |
-
for detail in details:
|
99 |
-
c.drawString(50, y, detail)
|
100 |
-
y -= 20
|
101 |
-
|
102 |
-
# Diagnosis
|
103 |
-
c.setFont("Helvetica-Bold", 14)
|
104 |
-
c.drawString(50, y, "Diagnosis:")
|
105 |
-
c.setFont("Helvetica", 12)
|
106 |
-
y -= 20
|
107 |
-
c.drawString(50, y, f"Fractured: {'Yes' if diagnosed_class == 'Fractured' else 'No'}")
|
108 |
-
y -= 20
|
109 |
-
c.drawString(50, y, f"Injury Severity: {severity}")
|
110 |
-
|
111 |
-
# X-ray Image
|
112 |
-
c.drawInlineImage(img_path, 150, y - 260, width=300, height=300)
|
113 |
-
y -= 280
|
114 |
-
|
115 |
-
# Treatment & Recommendations
|
116 |
-
c.setFont("Helvetica-Bold", 14)
|
117 |
-
c.drawString(50, y, "Recommended Treatment:")
|
118 |
-
c.setFont("Helvetica", 12)
|
119 |
-
y -= 20
|
120 |
-
recommendations = {
|
121 |
-
"Mild": "Rest, pain relievers, and follow-up X-ray.",
|
122 |
-
"Moderate": "Plaster cast, minor surgery if needed.",
|
123 |
-
"Severe": "Major surgery, metal implants, and physiotherapy."
|
124 |
-
}
|
125 |
-
treatment_text = recommendations[severity]
|
126 |
-
for line in simpleSplit(treatment_text, "Helvetica", 12, 480):
|
127 |
-
c.drawString(50, y, line)
|
128 |
-
y -= 20
|
129 |
-
|
130 |
-
# Estimated Cost
|
131 |
-
c.setFont("Helvetica-Bold", 14)
|
132 |
-
c.drawString(50, y, "Estimated Treatment Cost:")
|
133 |
-
c.setFont("Helvetica", 12)
|
134 |
-
y -= 20
|
135 |
-
cost_gov = f"Government Hospital: ₹{2000 if severity == 'Mild' else 8000 if severity == 'Moderate' else 20000} - ₹{5000 if severity == 'Mild' else 15000 if severity == 'Moderate' else 50000}"
|
136 |
-
cost_priv = f"Private Hospital: ₹{10000 if severity == 'Mild' else 30000 if severity == 'Moderate' else 100000}+"
|
137 |
-
for line in simpleSplit(cost_gov, "Helvetica", 12, 480):
|
138 |
-
c.drawString(50, y, line)
|
139 |
-
y -= 20
|
140 |
-
for line in simpleSplit(cost_priv, "Helvetica", 12, 480):
|
141 |
-
c.drawString(50, y, line)
|
142 |
-
y -= 20
|
143 |
-
|
144 |
-
c.save()
|
145 |
-
|
146 |
-
# Send email with report
|
147 |
-
email_status = send_email(email, name, report_path)
|
148 |
-
|
149 |
-
return report_path, email_status
|
150 |
|
151 |
# Gradio Interface
|
152 |
with gr.Blocks() as app:
|
153 |
-
gr.Markdown("# Bone Fracture Detection
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
submit_button = gr.Button("Generate Report")
|
175 |
-
output_file = gr.File(label="Download Report")
|
176 |
-
email_status = gr.Textbox(label="Email Status", interactive=False)
|
177 |
-
|
178 |
-
submit_button.click(
|
179 |
-
generate_report,
|
180 |
-
inputs=[name, age, gender, weight, height, allergies, cause, xray, email],
|
181 |
-
outputs=[output_file, email_status]
|
182 |
-
)
|
183 |
-
|
184 |
-
if __name__ == "__main__":
|
185 |
-
app.launch()
|
|
|
1 |
import os
|
|
|
2 |
import gradio as gr
|
3 |
+
from fpdf import FPDF
|
4 |
+
import smtplib
|
5 |
from email.mime.multipart import MIMEMultipart
|
6 |
from email.mime.text import MIMEText
|
7 |
from email.mime.base import MIMEBase
|
8 |
from email import encoders
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
# Set environment variable to disable GPU if needed
|
11 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
12 |
+
|
13 |
+
# Function to generate PDF report
|
14 |
+
def generate_report(name, age, gender, weight, height, allergies, injury_cause, address, parent_name, email, xray):
|
15 |
+
# Ensure input limits
|
16 |
+
name = name[:50] if name else "N/A"
|
17 |
+
age = str(age) if age else "N/A"
|
18 |
+
gender = gender if gender else "N/A"
|
19 |
+
weight = str(weight) + " kg" if weight else "N/A"
|
20 |
+
height = str(height) + " cm" if height else "N/A"
|
21 |
+
allergies = allergies[:100] if allergies else "None"
|
22 |
+
injury_cause = injury_cause[:500] if injury_cause else "Not specified"
|
23 |
+
address = address[:150] if address else "N/A"
|
24 |
+
parent_name = parent_name[:50] if parent_name else "N/A"
|
25 |
+
|
26 |
+
# Fake hospital details
|
27 |
+
hospital_name = "CityCare Orthopedic Hospital"
|
28 |
+
hospital_address = "123 Medical Lane, Health City, Country"
|
29 |
+
|
30 |
+
# Create PDF
|
31 |
+
pdf = FPDF()
|
32 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
33 |
+
pdf.add_page()
|
34 |
+
|
35 |
+
# Title
|
36 |
+
pdf.set_font("Arial", style="B", size=14)
|
37 |
+
pdf.cell(200, 10, hospital_name, ln=True, align="C")
|
38 |
+
pdf.set_font("Arial", size=10)
|
39 |
+
pdf.cell(200, 5, hospital_address, ln=True, align="C")
|
40 |
+
pdf.ln(10)
|
41 |
+
|
42 |
+
# Patient Information
|
43 |
+
pdf.set_font("Arial", style="B", size=12)
|
44 |
+
pdf.cell(200, 10, "Patient Report", ln=True, align="C")
|
45 |
+
pdf.ln(5)
|
46 |
+
|
47 |
+
pdf.set_font("Arial", size=10)
|
48 |
+
pdf.cell(200, 5, f"Patient Name: {name}", ln=True)
|
49 |
+
pdf.cell(200, 5, f"Age: {age} | Gender: {gender}", ln=True)
|
50 |
+
pdf.cell(200, 5, f"Weight: {weight} | Height: {height}", ln=True)
|
51 |
+
pdf.cell(200, 5, f"Allergies: {allergies}", ln=True)
|
52 |
+
pdf.cell(200, 5, f"Cause of Injury: {injury_cause}", ln=True)
|
53 |
+
pdf.cell(200, 5, f"Address: {address}", ln=True)
|
54 |
+
pdf.cell(200, 5, f"Parent/Guardian: {parent_name}", ln=True)
|
55 |
+
pdf.ln(10)
|
56 |
+
|
57 |
+
# X-ray image
|
58 |
+
if xray:
|
59 |
+
pdf.set_font("Arial", style="B", size=12)
|
60 |
+
pdf.cell(200, 10, "X-ray Image", ln=True, align="C")
|
61 |
+
pdf.ln(5)
|
62 |
+
xray_path = "temp_xray.png"
|
63 |
+
xray.save(xray_path)
|
64 |
+
pdf.image(xray_path, x=40, w=130)
|
65 |
+
os.remove(xray_path)
|
66 |
+
pdf.ln(10)
|
67 |
+
|
68 |
+
# Diagnosis and Recommendation
|
69 |
+
pdf.set_font("Arial", style="B", size=12)
|
70 |
+
pdf.cell(200, 10, "Diagnosis & Recommendations", ln=True)
|
71 |
+
pdf.set_font("Arial", size=10)
|
72 |
+
pdf.multi_cell(0, 5, "Based on the provided X-ray and details, the following suggestions are recommended:")
|
73 |
+
|
74 |
+
pdf.set_font("Arial", style="I", size=10)
|
75 |
+
pdf.cell(200, 5, "- Immediate medical consultation is advised.", ln=True)
|
76 |
+
pdf.cell(200, 5, "- Pain management with prescribed medications.", ln=True)
|
77 |
+
pdf.cell(200, 5, "- Possible surgical intervention if required.", ln=True)
|
78 |
+
pdf.cell(200, 5, "- Rest and immobilization of the affected area.", ln=True)
|
79 |
+
pdf.cell(200, 5, "- Follow-up X-ray and rehabilitation therapy.", ln=True)
|
80 |
+
|
81 |
+
pdf.ln(5)
|
82 |
+
pdf.set_font("Arial", style="B", size=10)
|
83 |
+
pdf.cell(200, 5, "Estimated Treatment Costs:", ln=True)
|
84 |
+
|
85 |
+
pdf.set_font("Arial", size=10)
|
86 |
+
pdf.cell(200, 5, "Government Hospital: $500 - $1,200", ln=True)
|
87 |
+
pdf.cell(200, 5, "Private Hospital: $2,000 - $5,000", ln=True)
|
88 |
|
89 |
+
# Save PDF
|
90 |
+
pdf_path = "patient_report.pdf"
|
91 |
+
pdf.output(pdf_path)
|
92 |
|
93 |
+
# Send email
|
94 |
+
send_email(email, name, hospital_name, pdf_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
+
return pdf_path
|
|
|
97 |
|
98 |
+
# Function to send email with PDF report
|
99 |
+
def send_email(email, patient_name, hospital_name, pdf_path):
|
100 |
+
sender_email = "your_email@gmail.com"
|
101 |
+
sender_password = "your_password"
|
102 |
+
subject = f"Patient Report - {patient_name}"
|
103 |
+
|
104 |
+
message = MIMEMultipart()
|
105 |
+
message["From"] = sender_email
|
106 |
+
message["To"] = email
|
107 |
+
message["Subject"] = subject
|
108 |
+
body = f"Dear {patient_name},\n\nYour medical report from {hospital_name} is attached. Please review the details and consult a doctor if needed.\n\nBest regards,\n{hospital_name}"
|
109 |
+
message.attach(MIMEText(body, "plain"))
|
110 |
+
|
111 |
+
with open(pdf_path, "rb") as attachment:
|
112 |
+
part = MIMEBase("application", "octet-stream")
|
113 |
+
part.set_payload(attachment.read())
|
114 |
+
encoders.encode_base64(part)
|
115 |
+
part.add_header("Content-Disposition", f"attachment; filename={pdf_path}")
|
116 |
+
message.attach(part)
|
117 |
|
118 |
+
try:
|
119 |
server = smtplib.SMTP("smtp.gmail.com", 587)
|
120 |
server.starttls()
|
121 |
server.login(sender_email, sender_password)
|
122 |
+
server.sendmail(sender_email, email, message.as_string())
|
123 |
server.quit()
|
124 |
+
print("Email sent successfully!")
|
|
|
125 |
except Exception as e:
|
126 |
+
print(f"Error sending email: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
|
128 |
# Gradio Interface
|
129 |
with gr.Blocks() as app:
|
130 |
+
gr.Markdown("# Bone Fracture Detection & Diagnosis")
|
131 |
+
gr.Markdown("Upload an X-ray, enter patient details, and get a report with treatment suggestions.")
|
132 |
+
|
133 |
+
name = gr.Textbox(label="Patient Name", max_length=50)
|
134 |
+
age = gr.Number(label="Age")
|
135 |
+
gender = gr.Dropdown(label="Gender", choices=["Male", "Female", "Other"])
|
136 |
+
weight = gr.Number(label="Weight (kg)")
|
137 |
+
height = gr.Number(label="Height (cm)")
|
138 |
+
allergies = gr.Textbox(label="Allergies", max_length=100)
|
139 |
+
injury_cause = gr.Textbox(label="Cause of Injury", max_length=500)
|
140 |
+
address = gr.Textbox(label="Address", max_length=150)
|
141 |
+
parent_name = gr.Textbox(label="Parent/Guardian Name", max_length=50)
|
142 |
+
email = gr.Textbox(label="Patient Email", type="email")
|
143 |
+
xray = gr.Image(label="Upload X-ray", type="pil")
|
144 |
+
|
145 |
+
submit = gr.Button("Generate Report")
|
146 |
+
output = gr.File()
|
147 |
+
|
148 |
+
submit.click(generate_report, [name, age, gender, weight, height, allergies, injury_cause, address, parent_name, email, xray], output)
|
149 |
+
|
150 |
+
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|