ftx7go's picture
Update app.py
f2d6494 verified
raw
history blame
5.89 kB
import os
import gradio as gr
from fpdf import FPDF
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
# Set environment variable to disable GPU if needed
os.environ["CUDA_VISIBLE_DEVICES"] = ""
# Function to generate PDF report
def generate_report(name, age, gender, weight, height, allergies, injury_cause, address, parent_name, email, xray):
# Ensure input limits
name = name[:50] if name else "N/A"
age = str(age) if age else "N/A"
gender = gender if gender else "N/A"
weight = str(weight) + " kg" if weight else "N/A"
height = str(height) + " cm" if height else "N/A"
allergies = allergies[:100] if allergies else "None"
injury_cause = injury_cause[:500] if injury_cause else "Not specified"
address = address[:150] if address else "N/A"
parent_name = parent_name[:50] if parent_name else "N/A"
# Fake hospital details
hospital_name = "CityCare Orthopedic Hospital"
hospital_address = "123 Medical Lane, Health City, Country"
# Create PDF
pdf = FPDF()
pdf.set_auto_page_break(auto=True, margin=15)
pdf.add_page()
# Title
pdf.set_font("Arial", style="B", size=14)
pdf.cell(200, 10, hospital_name, ln=True, align="C")
pdf.set_font("Arial", size=10)
pdf.cell(200, 5, hospital_address, ln=True, align="C")
pdf.ln(10)
# Patient Information
pdf.set_font("Arial", style="B", size=12)
pdf.cell(200, 10, "Patient Report", ln=True, align="C")
pdf.ln(5)
pdf.set_font("Arial", size=10)
pdf.cell(200, 5, f"Patient Name: {name}", ln=True)
pdf.cell(200, 5, f"Age: {age} | Gender: {gender}", ln=True)
pdf.cell(200, 5, f"Weight: {weight} | Height: {height}", ln=True)
pdf.cell(200, 5, f"Allergies: {allergies}", ln=True)
pdf.cell(200, 5, f"Cause of Injury: {injury_cause}", ln=True)
pdf.cell(200, 5, f"Address: {address}", ln=True)
pdf.cell(200, 5, f"Parent/Guardian: {parent_name}", ln=True)
pdf.ln(10)
# X-ray image
if xray:
pdf.set_font("Arial", style="B", size=12)
pdf.cell(200, 10, "X-ray Image", ln=True, align="C")
pdf.ln(5)
xray_path = "temp_xray.png"
xray.save(xray_path)
pdf.image(xray_path, x=40, w=130)
os.remove(xray_path)
pdf.ln(10)
# Diagnosis and Recommendation
pdf.set_font("Arial", style="B", size=12)
pdf.cell(200, 10, "Diagnosis & Recommendations", ln=True)
pdf.set_font("Arial", size=10)
pdf.multi_cell(0, 5, "Based on the provided X-ray and details, the following suggestions are recommended:")
pdf.set_font("Arial", style="I", size=10)
pdf.cell(200, 5, "- Immediate medical consultation is advised.", ln=True)
pdf.cell(200, 5, "- Pain management with prescribed medications.", ln=True)
pdf.cell(200, 5, "- Possible surgical intervention if required.", ln=True)
pdf.cell(200, 5, "- Rest and immobilization of the affected area.", ln=True)
pdf.cell(200, 5, "- Follow-up X-ray and rehabilitation therapy.", ln=True)
pdf.ln(5)
pdf.set_font("Arial", style="B", size=10)
pdf.cell(200, 5, "Estimated Treatment Costs:", ln=True)
pdf.set_font("Arial", size=10)
pdf.cell(200, 5, "Government Hospital: $500 - $1,200", ln=True)
pdf.cell(200, 5, "Private Hospital: $2,000 - $5,000", ln=True)
# Save PDF
pdf_path = "patient_report.pdf"
pdf.output(pdf_path)
# Send email
send_email(email, name, hospital_name, pdf_path)
return pdf_path
# Function to send email with PDF report
def send_email(email, patient_name, hospital_name, pdf_path):
sender_email = "[email protected]"
sender_password = "your_password"
subject = f"Patient Report - {patient_name}"
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = email
message["Subject"] = subject
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}"
message.attach(MIMEText(body, "plain"))
with open(pdf_path, "rb") as attachment:
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header("Content-Disposition", f"attachment; filename={pdf_path}")
message.attach(part)
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(sender_email, sender_password)
server.sendmail(sender_email, email, message.as_string())
server.quit()
print("Email sent successfully!")
except Exception as e:
print(f"Error sending email: {e}")
# Gradio Interface
with gr.Blocks() as app:
gr.Markdown("# Bone Fracture Detection & Diagnosis")
gr.Markdown("Upload an X-ray, enter patient details, and get a report with treatment suggestions.")
name = gr.Textbox(label="Patient Name", max_length=50)
age = gr.Number(label="Age")
gender = gr.Dropdown(label="Gender", choices=["Male", "Female", "Other"])
weight = gr.Number(label="Weight (kg)")
height = gr.Number(label="Height (cm)")
allergies = gr.Textbox(label="Allergies", max_length=100)
injury_cause = gr.Textbox(label="Cause of Injury", max_length=500)
address = gr.Textbox(label="Address", max_length=150)
parent_name = gr.Textbox(label="Parent/Guardian Name", max_length=50)
email = gr.Textbox(label="Patient Email", type="email")
xray = gr.Image(label="Upload X-ray", type="pil")
submit = gr.Button("Generate Report")
output = gr.File()
submit.click(generate_report, [name, age, gender, weight, height, allergies, injury_cause, address, parent_name, email, xray], output)
app.launch()