Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import json
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
|
| 6 |
+
# Load labels from external JSON
|
| 7 |
+
with open("labels.json", "r", encoding="utf-8") as f:
|
| 8 |
+
candidate_labels = json.load(f)
|
| 9 |
+
|
| 10 |
+
# Initialize the MedSigLIP pipeline
|
| 11 |
+
pipe = pipeline("zero-shot-image-classification", model="google/medsiglip-448")
|
| 12 |
+
|
| 13 |
+
# Define prediction function
|
| 14 |
+
def classify_image(image):
|
| 15 |
+
result = pipe(image, candidate_labels=candidate_labels)
|
| 16 |
+
# Convert result list to {label: score}
|
| 17 |
+
formatted = {item["label"]: round(item["score"] * 100, 2) for item in result}
|
| 18 |
+
# Sort descending by confidence
|
| 19 |
+
sorted_result = dict(sorted(formatted.items(), key=lambda x: x[1], reverse=True))
|
| 20 |
+
return sorted_result
|
| 21 |
+
|
| 22 |
+
# Build Gradio interface
|
| 23 |
+
demo = gr.Interface(
|
| 24 |
+
fn=classify_image,
|
| 25 |
+
inputs=gr.Image(type="filepath", label="Upload Chest X-ray or Medical Image"),
|
| 26 |
+
outputs=gr.Label(num_top_classes=5, label="Top Predictions"),
|
| 27 |
+
title="🩻 MedSigLIP Zero-Shot Medical Image Classifier",
|
| 28 |
+
description=(
|
| 29 |
+
"This demo uses Google's MedSigLIP (Sigmoid Loss for Language Image Pre-training) model "
|
| 30 |
+
"for zero-shot medical image classification. Upload an image and the model will estimate "
|
| 31 |
+
"its similarity with known medical conditions (loaded dynamically from labels.json)."
|
| 32 |
+
),
|
| 33 |
+
allow_flagging="never",
|
| 34 |
+
examples=[
|
| 35 |
+
["https://storage.googleapis.com/dx-scin-public-data/dataset/images/3445096909671059178.png"],
|
| 36 |
+
["https://storage.googleapis.com/dx-scin-public-data/dataset/images/-5669089898008966381.png"]
|
| 37 |
+
],
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
demo.launch()
|