ajinkya-jadhav-auraverse commited on
Commit ·
af3df0b
1
Parent(s): cf7eba3
application file and requirements
Browse files- app.py +46 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from open_clip import create_model_from_pretrained, get_tokenizer
|
| 5 |
+
|
| 6 |
+
MODEL_NAME = "microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_224"
|
| 7 |
+
|
| 8 |
+
print("Loading BiomedCLIP...")
|
| 9 |
+
model, preprocess = create_model_from_pretrained(f"hf-hub:{MODEL_NAME}")
|
| 10 |
+
tokenizer = get_tokenizer(f"hf-hub:{MODEL_NAME}")
|
| 11 |
+
model.eval()
|
| 12 |
+
print("Model Loaded")
|
| 13 |
+
|
| 14 |
+
labels = [
|
| 15 |
+
"eczema",
|
| 16 |
+
"psoriasis",
|
| 17 |
+
"fungal infection",
|
| 18 |
+
"acne",
|
| 19 |
+
"dermatitis",
|
| 20 |
+
"healthy skin"
|
| 21 |
+
]
|
| 22 |
+
|
| 23 |
+
def predict(image):
|
| 24 |
+
image = preprocess(image).unsqueeze(0)
|
| 25 |
+
|
| 26 |
+
with torch.no_grad():
|
| 27 |
+
image_features = model.encode_image(image)
|
| 28 |
+
text_tokens = tokenizer(labels)
|
| 29 |
+
text_features = model.encode_text(text_tokens)
|
| 30 |
+
|
| 31 |
+
image_features /= image_features.norm(dim=-1, keepdim=True)
|
| 32 |
+
text_features /= text_features.norm(dim=-1, keepdim=True)
|
| 33 |
+
|
| 34 |
+
similarity = (100.0 * image_features @ text_features.T).softmax(dim=-1)
|
| 35 |
+
probs = similarity.squeeze().tolist()
|
| 36 |
+
|
| 37 |
+
results = {labels[i]: probs[i] for i in range(len(labels))}
|
| 38 |
+
return results
|
| 39 |
+
|
| 40 |
+
gr.Interface(
|
| 41 |
+
fn=predict,
|
| 42 |
+
inputs=gr.Image(type="pil"),
|
| 43 |
+
outputs=gr.Label(num_top_classes=3),
|
| 44 |
+
title="BiomedCLIP Dermatology Demo",
|
| 45 |
+
description="Upload a skin image. Zero-shot prediction using BiomedCLIP. Research use only — not medical advice."
|
| 46 |
+
).launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchvision
|
| 3 |
+
torchaudio
|
| 4 |
+
open_clip_torch
|
| 5 |
+
Pillow
|
| 6 |
+
gradio
|