import os import smtplib import ssl from email.message import EmailMessage os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # Force TensorFlow to use CPU import gradio as gr import tensorflow as tf import numpy as np from tensorflow.keras.preprocessing import image from PIL import Image from reportlab.lib.pagesizes import letter from reportlab.pdfgen import canvas from reportlab.lib import colors from reportlab.platypus import Table, TableStyle # Load the trained model model = tf.keras.models.load_model("my_keras_model.h5") # Store generated report file path report_paths = {} # Function to send email def send_email(patient_email, patient_name): if patient_name not in report_paths: return "Error: Generate the report first before sending." report_path = report_paths[patient_name] sender_email = "your_email@gmail.com" sender_password = "your_email_password" subject = f"Bone Fracture Report for {patient_name}" body = f""" Dear {patient_name}, Your bone fracture diagnosis report is attached. If you have any concerns, consult your doctor. Regards, Hospital Team """ msg = EmailMessage() msg["From"] = sender_email msg["To"] = patient_email msg["Subject"] = subject msg.set_content(body) # Attach PDF file with open(report_path, "rb") as file: msg.add_attachment(file.read(), maintype="application", subtype="pdf", filename=os.path.basename(report_path)) # Secure email sending context = ssl.create_default_context() with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server: server.login(sender_email, sender_password) server.send_message(msg) return f"Report sent successfully to {patient_email}!" # Function to generate report def generate_report(name, age, gender, weight, height, allergies, cause, xray): if not name: return "Error: Please enter a patient name." image_size = (224, 224) def predict_fracture(xray_path): img = Image.open(xray_path).resize(image_size) img_array = image.img_to_array(img) / 255.0 img_array = np.expand_dims(img_array, axis=0) prediction = model.predict(img_array)[0][0] return prediction # Predict fracture prediction = predict_fracture(xray) diagnosed_class = "normal" if prediction > 0.5 else "Fractured" # Injury severity classification severity = "Mild" if prediction < 0.3 else "Moderate" if prediction < 0.7 else "Severe" # Save X-ray image for report img = Image.open(xray).resize((300, 300)) img_path = f"{name}_xray.png" img.save(img_path) # Generate PDF report report_path = f"{name}_fracture_report.pdf" c = canvas.Canvas(report_path, pagesize=letter) c.setFont("Helvetica-Bold", 16) c.drawString(200, 770, "Bone Fracture Detection Report") c.drawString(120, 290, f"Fractured: {'Yes' if diagnosed_class == 'Fractured' else 'No'}") c.drawInlineImage(img_path, 50, 320, width=250, height=250) c.save() # Store the file path report_paths[name] = report_path return report_path # Return file path # Define Gradio Interface with gr.Blocks() as app: gr.Markdown("## Bone Fracture Detection System") with gr.Row(): name = gr.Textbox(label="Patient Name") age = gr.Number(label="Age") gender = gr.Radio(["Male", "Female", "Other"], label="Gender") with gr.Row(): weight = gr.Number(label="Weight (kg)") height = gr.Number(label="Height (cm)") with gr.Row(): email = gr.Textbox(label="Patient Email", type="email") with gr.Row(): xray = gr.Image(type="filepath", label="Upload X-ray Image") submit_button = gr.Button("Generate Report") send_email_button = gr.Button("Send Report via Email") output_file = gr.File(label="Download Report") # When clicking "Generate Report", save the file path and allow downloading submit_button.click( generate_report, inputs=[name, age, gender, weight, height, allergies, cause, xray], outputs=[output_file] ) # When clicking "Send Report via Email", send the stored report file send_email_button.click( send_email, inputs=[email, name], outputs=[gr.Textbox(label="Status")] ) # Launch the Gradio app if __name__ == "__main__": app.launch()