Update app.py
Browse files
app.py
CHANGED
@@ -6,10 +6,30 @@ 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
|
@@ -26,7 +46,10 @@ def generate_report(name, age, gender, weight, height, allergies, injury_cause,
|
|
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)
|
@@ -63,7 +86,13 @@ def generate_report(name, age, gender, weight, height, allergies, injury_cause,
|
|
63 |
xray.save(xray_path)
|
64 |
pdf.image(xray_path, x=40, w=130)
|
65 |
os.remove(xray_path)
|
66 |
-
pdf.ln(
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
# Diagnosis and Recommendation
|
69 |
pdf.set_font("Arial", style="B", size=12)
|
@@ -98,7 +127,8 @@ def generate_report(name, age, gender, weight, height, allergies, injury_cause,
|
|
98 |
# Function to send email with PDF report
|
99 |
def send_email(email, patient_name, hospital_name, pdf_path):
|
100 |
sender_email = "[email protected]"
|
101 |
-
sender_password = "
|
|
|
102 |
subject = f"Patient Report - {patient_name}"
|
103 |
|
104 |
message = MIMEMultipart()
|
@@ -129,18 +159,8 @@ def send_email(email, patient_name, hospital_name, pdf_path):
|
|
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 |
-
|
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()
|
|
|
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("model.pth")
|
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
|
|
|
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)
|
|
|
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)
|
|
|
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()
|
|
|
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()
|