ftx7go commited on
Commit
8079fc6
·
verified ·
1 Parent(s): cdda6e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -32
app.py CHANGED
@@ -4,6 +4,8 @@ import numpy as np
4
  from tensorflow.keras.preprocessing import image
5
  from PIL import Image
6
  import os
 
 
7
 
8
  # Load the trained model
9
  model = tf.keras.models.load_model("my_keras_model.h5")
@@ -21,45 +23,60 @@ def predict_image(img):
21
  # Assuming binary classification (fractured or normal)
22
  class_names = ['Fractured', 'Normal']
23
  predicted_class = class_names[int(prediction[0] > 0.5)] # Threshold at 0.5
 
24
 
25
- return f"Prediction: {predicted_class} (Confidence: {prediction[0][0]:.2f})"
26
 
27
- # Get image paths dynamically
28
- sample_images_dir = "samples"
29
- sample_images = [os.path.join(sample_images_dir, f) for f in os.listdir(sample_images_dir) if f.endswith(('.jpg', '.png'))]
 
30
 
31
- # File download paths
32
- report_path = "https://huggingface.co/spaces/ftx7go/bone-fracture-detection-model/resolve/main/files/report.pdf"
33
- notebook_path = "https://huggingface.co/spaces/ftx7go/bone-fracture-detection-model/resolve/main/files/bfd.ipynb"
 
 
34
 
35
- # Function to provide file download links
36
- def get_download_links():
37
- return f"""
38
- **Download Project Files:**
39
- - [Download Report (PDF)]({report_path})
40
- - [Download Notebook (IPYNB)]({notebook_path})
41
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  # Define Gradio Interface
44
  interface = gr.Interface(
45
- fn=predict_image,
46
- inputs=gr.Image(type="pil"),
47
- outputs=gr.Textbox(),
48
- examples=sample_images, # Preloaded images for testing
49
- title="Bone Fracture Detection",
50
- description="Upload an X-ray image or select a sample image to check for fractures.",
51
- article=f"""
52
- ## Instructions
53
- - Upload an X-ray image to check for fractures.
54
- - Select a sample image for quick testing.
55
- - The model will predict if the bone is fractured or normal.
56
-
57
- ## Capabilities
58
- - Detects bone fractures from X-ray images.
59
- - Uses a trained deep learning model for classification.
60
-
61
- {get_download_links()}
62
- """
63
  )
64
 
65
  # Launch the Gradio app
 
4
  from tensorflow.keras.preprocessing import image
5
  from PIL import Image
6
  import os
7
+ from fpdf import FPDF
8
+ import datetime
9
 
10
  # Load the trained model
11
  model = tf.keras.models.load_model("my_keras_model.h5")
 
23
  # Assuming binary classification (fractured or normal)
24
  class_names = ['Fractured', 'Normal']
25
  predicted_class = class_names[int(prediction[0] > 0.5)] # Threshold at 0.5
26
+ confidence = prediction[0][0]
27
 
28
+ return predicted_class, confidence
29
 
30
+ # Function to generate a PDF report
31
+ def generate_report(name, age, weight, height, img):
32
+ # Predict result
33
+ predicted_class, confidence = predict_image(img)
34
 
35
+ # Create PDF
36
+ pdf = FPDF()
37
+ pdf.set_auto_page_break(auto=True, margin=15)
38
+ pdf.add_page()
39
+ pdf.set_font("Arial", size=12)
40
 
41
+ # Add title
42
+ pdf.set_font("Arial", style='B', size=16)
43
+ pdf.cell(200, 10, "Bone Fracture Detection Report", ln=True, align='C')
44
+ pdf.ln(10)
45
+
46
+ # Add patient details
47
+ pdf.set_font("Arial", size=12)
48
+ pdf.cell(200, 10, f"Patient Name: {name}", ln=True)
49
+ pdf.cell(200, 10, f"Age: {age}", ln=True)
50
+ pdf.cell(200, 10, f"Weight: {weight} kg", ln=True)
51
+ pdf.cell(200, 10, f"Height: {height} cm", ln=True)
52
+ pdf.cell(200, 10, f"Diagnosis Date: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", ln=True)
53
+ pdf.ln(10)
54
+
55
+ # Add prediction result
56
+ pdf.set_font("Arial", style='B', size=14)
57
+ pdf.cell(200, 10, f"Diagnosis: {predicted_class}", ln=True)
58
+ pdf.set_font("Arial", size=12)
59
+ pdf.cell(200, 10, f"Confidence: {confidence:.2f}", ln=True)
60
+
61
+ # Save PDF
62
+ pdf_filename = "patient_report.pdf"
63
+ pdf.output(pdf_filename)
64
+
65
+ return pdf_filename
66
 
67
  # Define Gradio Interface
68
  interface = gr.Interface(
69
+ fn=generate_report,
70
+ inputs=[
71
+ gr.Textbox(label="Patient Name"),
72
+ gr.Number(label="Age"),
73
+ gr.Number(label="Weight (kg)"),
74
+ gr.Number(label="Height (cm)"),
75
+ gr.Image(type="pil", label="X-ray Image")
76
+ ],
77
+ outputs=gr.File(label="Download Report"),
78
+ title="Bone Fracture Detection & Diagnosis",
79
+ description="Fill in the patient details and upload an X-ray image. The system will analyze the image and generate a PDF report with the diagnosis."
 
 
 
 
 
 
 
80
  )
81
 
82
  # Launch the Gradio app