pneumothorax / app.py
delphiclinic's picture
Update app.py
741c658 verified
##importing the libraries
from PIL import Image
import gradio as gr
from ultralytics import YOLO
import os
## Where to store flagged data
HF_TOKEN = os.getenv('HF_TOKEN')
hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "delphiclinic/flaggedImages")
# Load your trained model
model = YOLO('best.pt')
#Function for making predictions
def predict (image):
##To display an error message if you submit without an image
if image is None:
raise gr.Error("Please upload a chest X-ray image to proceed")
results = model(image, conf=0.1)
## list of all the probabilities
conf = []
for result in results:
im_array = result.plot()
img = Image.fromarray(im_array) # RGB PIL image
img.show()
### Getting the confidence
box = result.boxes
conf.append(box.conf.numpy())
## sort the confidence from lowest to highest
if len(conf[0])>1:
conf_sort = sorted(conf[0]) ## ascending order
impression = f"Pneumothorax is detected in the image with confidence ranging from {round(conf_sort[0]*100,1)}% to {round(conf_sort[-1]*100,1)}%"
elif len(conf[0])==1:
impression = f"Pneumothorax is detected in the image with confidence of {round(conf[0][0]*100,1)}%"
else:
impression = "No pneumothorax is detected in the image"
# print(impression)
return img, impression
platform = gr.Interface( fn = predict,
title ="PTCADx: Computer-Aided Detection of Pneumothorax in Chest X-ray Images",
inputs = "image",
outputs = [
gr.Image(label="Processed Image"),
gr.Text(label="Impression")
],
description="""
Introducing a revolutionary computer-aided detection tool designed to enhance the efficiency of clinicians in detecting pneumothorax in chest X-ray images.
""",
allow_flagging="auto",
# flagging_options=["Wrong detection", "Ambiguous", "Other"],
flagging_callback=hf_writer,
article = """
It is crucial to emphasize that while this tool serves as a valuable research aid,
it is not intended to replace clinical guidelines,
nor should it substitute for the wealth of clinical knowledge
and experience possessed by healthcare professionals.
The algorithm is meant to complement and support the diagnostic process,
providing an additional layer of analysis for consideration in conjunction with the clinician's expertise.
Users are encouraged to interpret the algorithm's output in conjunction with their clinical judgment,
and the tool should be viewed as a supplementary resource rather than a standalone diagnostic solution.
"""
)
platform.launch(inline=True,share=True,favicon_path = "thumbnail.jpg",debug=True)