Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,48 @@
|
|
| 1 |
from transformers import AutoModelForImageClassification, AutoImageProcessor
|
| 2 |
import torch
|
| 3 |
-
import torch.nn.functional as F
|
| 4 |
from PIL import Image
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
-
# 1️⃣ Load fine-tuned vit-chest-xray model
|
| 8 |
model_name = "codewithdark/vit-chest-xray"
|
| 9 |
model = AutoModelForImageClassification.from_pretrained(model_name)
|
| 10 |
processor = AutoImageProcessor.from_pretrained(model_name)
|
| 11 |
model.eval()
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
# We only care about Pneumonia, Consolidation, Edema
|
| 17 |
-
target_labels = ['Pneumonia', 'Consolidation', 'Cardiomegaly', 'No Finding', 'Edema']
|
| 18 |
-
target_idxs = [label_list.index(lbl) for lbl in target_labels]
|
| 19 |
|
| 20 |
def predict(image):
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
iface = gr.Interface(
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
)
|
| 39 |
-
|
|
|
|
|
|
| 1 |
from transformers import AutoModelForImageClassification, AutoImageProcessor
|
| 2 |
import torch
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import gradio as gr
|
| 5 |
|
|
|
|
| 6 |
model_name = "codewithdark/vit-chest-xray"
|
| 7 |
model = AutoModelForImageClassification.from_pretrained(model_name)
|
| 8 |
processor = AutoImageProcessor.from_pretrained(model_name)
|
| 9 |
model.eval()
|
| 10 |
|
| 11 |
+
labels = ['Cardiomegaly', 'Edema', 'Consolidation', 'No Finding', 'Pneumonia']
|
| 12 |
+
target_labels = ['Pneumonia', 'Consolidation', 'Edema']
|
| 13 |
+
target_idxs = [labels.index(lbl) for lbl in target_labels]
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def predict(image):
|
| 16 |
+
image = image.convert("RGB").resize((224, 224))
|
| 17 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 18 |
+
with torch.no_grad():
|
| 19 |
+
logits = model(**inputs).logits
|
| 20 |
+
probs = torch.sigmoid(logits).squeeze()
|
| 21 |
+
|
| 22 |
+
```
|
| 23 |
+
detected = []
|
| 24 |
+
results = []
|
| 25 |
+
for idx, lbl in zip(target_idxs, target_labels):
|
| 26 |
+
prob = probs[idx].item()
|
| 27 |
+
status = "YES" if prob > 0.5 else "NO"
|
| 28 |
+
results.append(f"{lbl}: {status} ({prob:.2f})")
|
| 29 |
+
if status == "YES":
|
| 30 |
+
detected.append(lbl)
|
| 31 |
+
|
| 32 |
+
if detected:
|
| 33 |
+
summary = f"⚠️ Patient shows signs of: {', '.join(detected)}."
|
| 34 |
+
else:
|
| 35 |
+
summary = "✅ Patient appears healthy — no major lung issues detected."
|
| 36 |
+
|
| 37 |
+
return "\n".join(results + ["\n" + summary])
|
| 38 |
+
```
|
| 39 |
|
| 40 |
iface = gr.Interface(
|
| 41 |
+
fn=predict,
|
| 42 |
+
inputs=gr.Image(type="pil"),
|
| 43 |
+
outputs="text",
|
| 44 |
+
title="Chest X-ray Disease Detector",
|
| 45 |
+
description="Upload a chest X-ray to detect Pneumonia, Consolidation, and Edema. Gives clear patient health summary."
|
| 46 |
)
|
| 47 |
+
|
| 48 |
+
iface.launch()
|