Delete app.py
Browse files
app.py
DELETED
@@ -1,170 +0,0 @@
|
|
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 |
-
import torch
|
10 |
-
from torchvision import transforms
|
11 |
-
from PIL import Image
|
12 |
-
|
13 |
-
# Set environment variable to disable GPU if needed
|
14 |
-
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
15 |
-
|
16 |
-
# Load the trained fracture detection model
|
17 |
-
model = torch.load("my_keras_model.h5")
|
18 |
-
model.eval()
|
19 |
-
|
20 |
-
# Function to predict fracture
|
21 |
-
def predict_fracture(xray):
|
22 |
-
transform = transforms.Compose([
|
23 |
-
transforms.Resize((224, 224)),
|
24 |
-
transforms.ToTensor(),
|
25 |
-
])
|
26 |
-
image = transform(xray).unsqueeze(0)
|
27 |
-
with torch.no_grad():
|
28 |
-
output = model(image)
|
29 |
-
predicted_class = "Fractured" if torch.argmax(output) == 1 else "Not Fractured"
|
30 |
-
confidence = torch.nn.functional.softmax(output, dim=1).max().item() * 100
|
31 |
-
return predicted_class, confidence
|
32 |
-
|
33 |
-
# Function to generate PDF report
|
34 |
-
def generate_report(name, age, gender, weight, height, allergies, injury_cause, address, parent_name, email, xray):
|
35 |
-
# Ensure input limits
|
36 |
-
name = name[:50] if name else "N/A"
|
37 |
-
age = str(age) if age else "N/A"
|
38 |
-
gender = gender if gender else "N/A"
|
39 |
-
weight = str(weight) + " kg" if weight else "N/A"
|
40 |
-
height = str(height) + " cm" if height else "N/A"
|
41 |
-
allergies = allergies[:100] if allergies else "None"
|
42 |
-
injury_cause = injury_cause[:500] if injury_cause else "Not specified"
|
43 |
-
address = address[:150] if address else "N/A"
|
44 |
-
parent_name = parent_name[:50] if parent_name else "N/A"
|
45 |
-
|
46 |
-
# Fake hospital details
|
47 |
-
hospital_name = "CityCare Orthopedic Hospital"
|
48 |
-
hospital_address = "123 Medical Lane, Health City, Country"
|
49 |
-
|
50 |
-
# Predict fracture
|
51 |
-
prediction, confidence = predict_fracture(xray)
|
52 |
-
|
53 |
-
# Create PDF
|
54 |
-
pdf = FPDF()
|
55 |
-
pdf.set_auto_page_break(auto=True, margin=15)
|
56 |
-
pdf.add_page()
|
57 |
-
|
58 |
-
# Title
|
59 |
-
pdf.set_font("Arial", style="B", size=14)
|
60 |
-
pdf.cell(200, 10, hospital_name, ln=True, align="C")
|
61 |
-
pdf.set_font("Arial", size=10)
|
62 |
-
pdf.cell(200, 5, hospital_address, ln=True, align="C")
|
63 |
-
pdf.ln(10)
|
64 |
-
|
65 |
-
# Patient Information
|
66 |
-
pdf.set_font("Arial", style="B", size=12)
|
67 |
-
pdf.cell(200, 10, "Patient Report", ln=True, align="C")
|
68 |
-
pdf.ln(5)
|
69 |
-
|
70 |
-
pdf.set_font("Arial", size=10)
|
71 |
-
pdf.cell(200, 5, f"Patient Name: {name}", ln=True)
|
72 |
-
pdf.cell(200, 5, f"Age: {age} | Gender: {gender}", ln=True)
|
73 |
-
pdf.cell(200, 5, f"Weight: {weight} | Height: {height}", ln=True)
|
74 |
-
pdf.cell(200, 5, f"Allergies: {allergies}", ln=True)
|
75 |
-
pdf.cell(200, 5, f"Cause of Injury: {injury_cause}", ln=True)
|
76 |
-
pdf.cell(200, 5, f"Address: {address}", ln=True)
|
77 |
-
pdf.cell(200, 5, f"Parent/Guardian: {parent_name}", ln=True)
|
78 |
-
pdf.ln(10)
|
79 |
-
|
80 |
-
# X-ray image
|
81 |
-
if xray:
|
82 |
-
pdf.set_font("Arial", style="B", size=12)
|
83 |
-
pdf.cell(200, 10, "X-ray Image", ln=True, align="C")
|
84 |
-
pdf.ln(5)
|
85 |
-
xray_path = "temp_xray.png"
|
86 |
-
xray.save(xray_path)
|
87 |
-
pdf.image(xray_path, x=40, w=130)
|
88 |
-
os.remove(xray_path)
|
89 |
-
pdf.ln(5)
|
90 |
-
|
91 |
-
# Prediction result
|
92 |
-
pdf.set_font("Arial", style="B", size=10)
|
93 |
-
pdf.cell(200, 5, f"Prediction: {prediction} (Confidence: {confidence:.2f}%)", ln=True, align="C")
|
94 |
-
|
95 |
-
pdf.ln(10)
|
96 |
-
|
97 |
-
# Diagnosis and Recommendation
|
98 |
-
pdf.set_font("Arial", style="B", size=12)
|
99 |
-
pdf.cell(200, 10, "Diagnosis & Recommendations", ln=True)
|
100 |
-
pdf.set_font("Arial", size=10)
|
101 |
-
pdf.multi_cell(0, 5, "Based on the provided X-ray and details, the following suggestions are recommended:")
|
102 |
-
|
103 |
-
pdf.set_font("Arial", style="I", size=10)
|
104 |
-
pdf.cell(200, 5, "- Immediate medical consultation is advised.", ln=True)
|
105 |
-
pdf.cell(200, 5, "- Pain management with prescribed medications.", ln=True)
|
106 |
-
pdf.cell(200, 5, "- Possible surgical intervention if required.", ln=True)
|
107 |
-
pdf.cell(200, 5, "- Rest and immobilization of the affected area.", ln=True)
|
108 |
-
pdf.cell(200, 5, "- Follow-up X-ray and rehabilitation therapy.", ln=True)
|
109 |
-
|
110 |
-
pdf.ln(5)
|
111 |
-
pdf.set_font("Arial", style="B", size=10)
|
112 |
-
pdf.cell(200, 5, "Estimated Treatment Costs:", ln=True)
|
113 |
-
|
114 |
-
pdf.set_font("Arial", size=10)
|
115 |
-
pdf.cell(200, 5, "Government Hospital: $500 - $1,200", ln=True)
|
116 |
-
pdf.cell(200, 5, "Private Hospital: $2,000 - $5,000", ln=True)
|
117 |
-
|
118 |
-
# Save PDF
|
119 |
-
pdf_path = "patient_report.pdf"
|
120 |
-
pdf.output(pdf_path)
|
121 |
-
|
122 |
-
# Send email
|
123 |
-
send_email(email, name, hospital_name, pdf_path)
|
124 |
-
|
125 |
-
return pdf_path
|
126 |
-
|
127 |
-
# Function to send email with PDF report
|
128 |
-
def send_email(email, patient_name, hospital_name, pdf_path):
|
129 |
-
sender_email = "[email protected]"
|
130 |
-
sender_password = "your_app_password" # Use App Password
|
131 |
-
|
132 |
-
subject = f"Patient Report - {patient_name}"
|
133 |
-
|
134 |
-
message = MIMEMultipart()
|
135 |
-
message["From"] = sender_email
|
136 |
-
message["To"] = email
|
137 |
-
message["Subject"] = subject
|
138 |
-
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}"
|
139 |
-
message.attach(MIMEText(body, "plain"))
|
140 |
-
|
141 |
-
with open(pdf_path, "rb") as attachment:
|
142 |
-
part = MIMEBase("application", "octet-stream")
|
143 |
-
part.set_payload(attachment.read())
|
144 |
-
encoders.encode_base64(part)
|
145 |
-
part.add_header("Content-Disposition", f"attachment; filename={pdf_path}")
|
146 |
-
message.attach(part)
|
147 |
-
|
148 |
-
try:
|
149 |
-
server = smtplib.SMTP("smtp.gmail.com", 587)
|
150 |
-
server.starttls()
|
151 |
-
server.login(sender_email, sender_password)
|
152 |
-
server.sendmail(sender_email, email, message.as_string())
|
153 |
-
server.quit()
|
154 |
-
print("Email sent successfully!")
|
155 |
-
except Exception as e:
|
156 |
-
print(f"Error sending email: {e}")
|
157 |
-
|
158 |
-
# Gradio Interface
|
159 |
-
with gr.Blocks() as app:
|
160 |
-
gr.Markdown("# Bone Fracture Detection & Diagnosis")
|
161 |
-
gr.Markdown("Upload an X-ray, enter patient details, and get a report with treatment suggestions.")
|
162 |
-
|
163 |
-
xray = gr.Image(label="Upload X-ray", type="pil", value="samples/sample_xray.jpg")
|
164 |
-
|
165 |
-
submit = gr.Button("Generate Report")
|
166 |
-
output = gr.File()
|
167 |
-
|
168 |
-
submit.click(generate_report, [name, age, gender, weight, height, allergies, injury_cause, address, parent_name, email, xray], output)
|
169 |
-
|
170 |
-
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|