Spaces:
Runtime error
Runtime error
Matt Grannell commited on
Commit ·
94e3c71
1
Parent(s): 162676a
Add MedImageInsight Gradio app
Browse files- app.py +40 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import base64
|
| 3 |
+
import io
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from medimageinsight import MedImageInsight
|
| 6 |
+
|
| 7 |
+
classifier = MedImageInsight(
|
| 8 |
+
model_dir="2024.09.27",
|
| 9 |
+
vision_model_name="medimageinsigt-v1.0.0.pt",
|
| 10 |
+
language_model_name="language_model.pth"
|
| 11 |
+
)
|
| 12 |
+
classifier.load_model()
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def analyze(image: Image.Image, labels_str: str):
|
| 16 |
+
labels = [l.strip() for l in labels_str.split(",") if l.strip()]
|
| 17 |
+
if not labels:
|
| 18 |
+
return {}
|
| 19 |
+
buf = io.BytesIO()
|
| 20 |
+
image.save(buf, format="PNG")
|
| 21 |
+
img_b64 = base64.encodebytes(buf.getvalue()).decode("utf-8")
|
| 22 |
+
results = classifier.predict([img_b64], labels)
|
| 23 |
+
return results[0]
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
demo = gr.Interface(
|
| 27 |
+
fn=analyze,
|
| 28 |
+
inputs=[
|
| 29 |
+
gr.Image(type="pil", label="Medical Image"),
|
| 30 |
+
gr.Textbox(
|
| 31 |
+
value="normal, Pneumonia, pleural effusion",
|
| 32 |
+
label="Labels (comma-separated)"
|
| 33 |
+
),
|
| 34 |
+
],
|
| 35 |
+
outputs=gr.Label(num_top_classes=5, label="Classification Scores"),
|
| 36 |
+
title="MedImageInsight — Zero-Shot Medical Image Classification",
|
| 37 |
+
description="Upload a medical image (X-ray, CT, MRI, etc.) and provide candidate labels for zero-shot classification.",
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
git+https://huggingface.co/lion-ai/MedImageInsights
|
| 2 |
+
huggingface_hub
|
| 3 |
+
Pillow
|
| 4 |
+
torch
|