Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
#import openai
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
#openai.api_key = os.getenv("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")
|
| 7 |
+
|
| 8 |
+
# Load the pre-trained image classification pipeline
|
| 9 |
+
pipe = pipeline("image-classification", model="ALM-AHME/convnextv2-large-1k-224-finetuned-BreastCancer-Classification-BreakHis-AH-60-20-20")
|
| 10 |
+
|
| 11 |
+
# Define the decision logic based on confidence scores
|
| 12 |
+
def classify_mammogram(img):
|
| 13 |
+
results = pipe(img)
|
| 14 |
+
predicted_label = results[0]['label']
|
| 15 |
+
confidence = results[0]['score']
|
| 16 |
+
|
| 17 |
+
if predicted_label == "malignant":
|
| 18 |
+
if confidence >= 0.8:
|
| 19 |
+
classification = "Non-suspicious"
|
| 20 |
+
elif 0.6 <= confidence < 0.8:
|
| 21 |
+
classification = "High risk"
|
| 22 |
+
else:
|
| 23 |
+
classification = "Indeterminate"
|
| 24 |
+
else: # Benign prediction
|
| 25 |
+
if confidence < 0.4:
|
| 26 |
+
classification = "Suspicious"
|
| 27 |
+
elif 0.4 <= confidence < 0.8:
|
| 28 |
+
classification = "Non-suspicious"
|
| 29 |
+
else:
|
| 30 |
+
classification = "No risk"
|
| 31 |
+
|
| 32 |
+
return f"Predicted Class: {predicted_label}\nClassification: {classification}\nConfidence: {confidence:.2f}"
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
# user_message = {
|
| 36 |
+
# "role": "user",
|
| 37 |
+
# "content": f""" You are a breast cancer specialist and you have been given a recent breast cancer result of a patient and here are the results:
|
| 38 |
+
# Predicted Class: {predicted_label}\nClassification: {classification}\nConfidence: {confidence:.2f}
|
| 39 |
+
|
| 40 |
+
# You have to give a clear explanations about their screening results, potential next steps, and relevant information about breast health based on the scanned mammogram and provide reference to things like "Retrieve Similar Cases," "Explain Diagnostic," "Treatment Recommendations," "Prognosis Analysis," and "Clinical Trial Matching."
|
| 41 |
+
|
| 42 |
+
# Write a extensive report for this.
|
| 43 |
+
# Good Luck!
|
| 44 |
+
|
| 45 |
+
# """
|
| 46 |
+
# }
|
| 47 |
+
# completion = openai.ChatCompletion.create(
|
| 48 |
+
# model="gpt-3.5-turbo",
|
| 49 |
+
# messages=[user_message]
|
| 50 |
+
# )
|
| 51 |
+
# result= completion.choices[0].message.content.strip()
|
| 52 |
+
|
| 53 |
+
# return result
|
| 54 |
+
|
| 55 |
+
# Launch the GUI with the modified output
|
| 56 |
+
iface = gr.Interface(
|
| 57 |
+
fn=classify_mammogram,
|
| 58 |
+
inputs=gr.inputs.Image(type="pil", label="Input Mammogram"),
|
| 59 |
+
outputs=gr.outputs.Textbox(label="Output"),
|
| 60 |
+
examples=[["1-243.jpg"],["1-285.jpg"],["1-198.jpg"]],
|
| 61 |
+
title="Enhanced Protocol for Breast Cancer Detection: Streamlining Decisions from Mammograms.",
|
| 62 |
+
description=(
|
| 63 |
+
"<div style='display: flex; justify-content: space-between;'>"
|
| 64 |
+
"<div style='width: 48%;'>"
|
| 65 |
+
"<p style='font-size: 18px; font-weight: bold;'>Malignant</p>"
|
| 66 |
+
"<p style='font-size: 16px;'>Non-suspicious: The AI is confident that no suspicious signs are present.<br>"
|
| 67 |
+
"High risk: The AI is confident that the results are highly suspicious.<br>"
|
| 68 |
+
"Indeterminate: The AI is uncertain and not confident in making a definitive classification.</p>"
|
| 69 |
+
"</div>"
|
| 70 |
+
"<div style='width: 48%;'>"
|
| 71 |
+
"<p style='font-size: 18px; font-weight: bold;'>Benign</p>"
|
| 72 |
+
"<p style='font-size: 16px;'>Suspicious: AI is not that confident. Further Supervision is needed.<br>"
|
| 73 |
+
"Non-Suspicious: AI is confident that there is nothing to worry about.<br>"
|
| 74 |
+
"No Risk: AI is confident. No direct supervision is needed.</p>"
|
| 75 |
+
"</div>"
|
| 76 |
+
"</div>"
|
| 77 |
+
)
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
iface.launch(inbrowser=True)
|