finetuned model + more instructions
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ from datasets import load_dataset
|
|
| 3 |
import torch
|
| 4 |
from torchvision import transforms, models
|
| 5 |
from PIL import Image
|
|
|
|
| 6 |
import numpy as np
|
| 7 |
import random
|
| 8 |
|
|
@@ -14,12 +15,11 @@ dataset = load_dataset("gymprathap/Breast-Cancer-Ultrasound-Images-Dataset", spl
|
|
| 14 |
# Map numeric labels to readable classes
|
| 15 |
label_names = dataset.features["label"].names # ['benign', 'malignant', 'normal']
|
| 16 |
|
| 17 |
-
# ---- 2. Define model (
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
model
|
| 21 |
-
|
| 22 |
-
model.eval()
|
| 23 |
|
| 24 |
# Transform for inference
|
| 25 |
transform = transforms.Compose([
|
|
@@ -32,38 +32,37 @@ transform = transforms.Compose([
|
|
| 32 |
|
| 33 |
# ---- 3. Utility: get image + run prediction ----
|
| 34 |
def predict_from_sample(sample_idx):
|
| 35 |
-
"""Return image, prediction info, and true label."""
|
| 36 |
row = dataset[int(sample_idx)]
|
| 37 |
image = row["image"]
|
| 38 |
true_label_idx = row["label"]
|
| 39 |
true_label = label_names[true_label_idx]
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
| 56 |
caption = (
|
| 57 |
-
f"**Predicted:** {pred_label}
|
| 58 |
f"**True Label:** {true_label} \n"
|
| 59 |
-
f"{
|
| 60 |
)
|
| 61 |
|
| 62 |
-
return image, caption
|
| 63 |
-
|
| 64 |
|
| 65 |
# ---- 4. Build Gradio UI ----
|
| 66 |
-
N_SAMPLES =
|
| 67 |
total = len(dataset)
|
| 68 |
random_indices = random.sample(range(total), N_SAMPLES)
|
| 69 |
|
|
@@ -76,7 +75,16 @@ with gr.Blocks(title="Women's Longevity Hack") as demo:
|
|
| 76 |
with gr.Tabs():
|
| 77 |
with gr.Tab("Getting Started"):
|
| 78 |
gr.Markdown(
|
| 79 |
-
"## Getting Started
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
)
|
| 81 |
|
| 82 |
with gr.Tab("More Datasets"):
|
|
@@ -84,6 +92,9 @@ with gr.Blocks(title="Women's Longevity Hack") as demo:
|
|
| 84 |
"""
|
| 85 |
## π Dataset Inspiration
|
| 86 |
|
|
|
|
|
|
|
|
|
|
| 87 |
| Dataset | Modalities / Type | Description & Use Cases |
|
| 88 |
|---|---|---|
|
| 89 |
| [gymprathap/Breast-Cancer-Ultrasound-Images-Dataset](https://huggingface.co/datasets/gymprathap/Breast-Cancer-Ultrasound-Images-Dataset) | Images (ultrasound) + labels | Ultrasound images labeled as benign / malignant / normal. Useful for image classification, explainability (e.g., Grad-CAM), or multimodal fusion if metadata available. |
|
|
@@ -93,6 +104,8 @@ with gr.Blocks(title="Women's Longevity Hack") as demo:
|
|
| 93 |
| [BoneMet/BoneMet](https://huggingface.co/datasets/BoneMet/BoneMet) | Biomedical / genomic / imaging | Dataset focused on bone-metastasis research; can support multimodal modeling combining clinical, imaging, and molecular data. |
|
| 94 |
| [AIBIC/MLOmics](https://huggingface.co/datasets/AIBIC/MLOmics) | Multi-omics / biomedical | Multi-omics resource (genomic, transcriptomic, proteomic) for biomedical discovery and precision-health modeling. |
|
| 95 |
|
|
|
|
|
|
|
| 96 |
"""
|
| 97 |
)
|
| 98 |
|
|
@@ -118,7 +131,6 @@ with gr.Blocks(title="Women's Longevity Hack") as demo:
|
|
| 118 |
gr.Markdown(
|
| 119 |
"Dataset: [gymprathap/Breast-Cancer-Ultrasound-Images-Dataset]"
|
| 120 |
"(https://huggingface.co/datasets/gymprathap/Breast-Cancer-Ultrasound-Images-Dataset)\n"
|
| 121 |
-
"Note: Model weights here are for demonstration only."
|
| 122 |
)
|
| 123 |
|
| 124 |
# ---- 5. Launch app ----
|
|
|
|
| 3 |
import torch
|
| 4 |
from torchvision import transforms, models
|
| 5 |
from PIL import Image
|
| 6 |
+
from transformers import pipeline
|
| 7 |
import numpy as np
|
| 8 |
import random
|
| 9 |
|
|
|
|
| 15 |
# Map numeric labels to readable classes
|
| 16 |
label_names = dataset.features["label"].names # ['benign', 'malignant', 'normal']
|
| 17 |
|
| 18 |
+
# ---- 2. Define model (pretrained or real CNN) ----
|
| 19 |
+
clf = pipeline(
|
| 20 |
+
"image-classification",
|
| 21 |
+
model="hugging-science/sample-breast-cancer-classification"
|
| 22 |
+
)
|
|
|
|
| 23 |
|
| 24 |
# Transform for inference
|
| 25 |
transform = transforms.Compose([
|
|
|
|
| 32 |
|
| 33 |
# ---- 3. Utility: get image + run prediction ----
|
| 34 |
def predict_from_sample(sample_idx):
|
|
|
|
| 35 |
row = dataset[int(sample_idx)]
|
| 36 |
image = row["image"]
|
| 37 |
true_label_idx = row["label"]
|
| 38 |
true_label = label_names[true_label_idx]
|
| 39 |
|
| 40 |
+
# Run Hugging Face pipeline
|
| 41 |
+
preds = clf(image.convert("RGB"))
|
| 42 |
+
pred_label = preds[0]["label"]
|
| 43 |
+
conf = preds[0]["score"]
|
| 44 |
+
|
| 45 |
+
label_map = {
|
| 46 |
+
"LABEL_0": "benign",
|
| 47 |
+
"LABEL_1": "malignant",
|
| 48 |
+
"LABEL_2": "normal"
|
| 49 |
+
}
|
| 50 |
+
pred_label_raw = preds[0]["label"]
|
| 51 |
+
pred_label = label_map.get(pred_label_raw, pred_label_raw) # fallback to raw if missing
|
| 52 |
+
conf = preds[0]["score"]
|
| 53 |
+
|
| 54 |
+
# Format output
|
| 55 |
+
correct = (pred_label.lower() == true_label.lower())
|
| 56 |
caption = (
|
| 57 |
+
f"**Predicted:** {pred_label} (confidence: {conf:.2f}) \n"
|
| 58 |
f"**True Label:** {true_label} \n"
|
| 59 |
+
f"{'β
Correct' if correct else 'β Incorrect'}"
|
| 60 |
)
|
| 61 |
|
| 62 |
+
return image.convert("RGB"), caption
|
|
|
|
| 63 |
|
| 64 |
# ---- 4. Build Gradio UI ----
|
| 65 |
+
N_SAMPLES = 30 # number of random samples to show
|
| 66 |
total = len(dataset)
|
| 67 |
random_indices = random.sample(range(total), N_SAMPLES)
|
| 68 |
|
|
|
|
| 75 |
with gr.Tabs():
|
| 76 |
with gr.Tab("Getting Started"):
|
| 77 |
gr.Markdown(
|
| 78 |
+
"""## Getting Started
|
| 79 |
+
|
| 80 |
+
Welcome to the Women's Longevity Hack demo! This app showcases a simple image classification model trained to identify breast cancer from ultrasound images. It also provides resources for exploring related datasets.
|
| 81 |
+
|
| 82 |
+
To build something like this yourself, check out the files in this repo:
|
| 83 |
+
- `train.py`: Code to fine-tune a Hugging Face vision model (e.g., ViT) on breast ultrasound images.
|
| 84 |
+
- `app.py`: Code for this Gradio app, which loads the model and dataset, and provides an interactive demo.
|
| 85 |
+
|
| 86 |
+
Don't be afraid to modify and experiment with the code! Or, ask ChatGPT to help you customize it for your own use case. If a generative model gives you something that doesn't work (especially if a flag is not recognized), I recommend telling the model you think it's hallucinating and checking its suggestions against the most recent documentation.
|
| 87 |
+
"""
|
| 88 |
)
|
| 89 |
|
| 90 |
with gr.Tab("More Datasets"):
|
|
|
|
| 92 |
"""
|
| 93 |
## π Dataset Inspiration
|
| 94 |
|
| 95 |
+
|
| 96 |
+
If you want to explore other potentially interesting datasets, please check these out:
|
| 97 |
+
|
| 98 |
| Dataset | Modalities / Type | Description & Use Cases |
|
| 99 |
|---|---|---|
|
| 100 |
| [gymprathap/Breast-Cancer-Ultrasound-Images-Dataset](https://huggingface.co/datasets/gymprathap/Breast-Cancer-Ultrasound-Images-Dataset) | Images (ultrasound) + labels | Ultrasound images labeled as benign / malignant / normal. Useful for image classification, explainability (e.g., Grad-CAM), or multimodal fusion if metadata available. |
|
|
|
|
| 104 |
| [BoneMet/BoneMet](https://huggingface.co/datasets/BoneMet/BoneMet) | Biomedical / genomic / imaging | Dataset focused on bone-metastasis research; can support multimodal modeling combining clinical, imaging, and molecular data. |
|
| 105 |
| [AIBIC/MLOmics](https://huggingface.co/datasets/AIBIC/MLOmics) | Multi-omics / biomedical | Multi-omics resource (genomic, transcriptomic, proteomic) for biomedical discovery and precision-health modeling. |
|
| 106 |
|
| 107 |
+
|
| 108 |
+
If none of these is quite what you're looking for, you can also explore the [Datasets Semantic Search](https://huggingface.co/spaces/librarian-bots/huggingface-semantic-search) to find something more your speed.
|
| 109 |
"""
|
| 110 |
)
|
| 111 |
|
|
|
|
| 131 |
gr.Markdown(
|
| 132 |
"Dataset: [gymprathap/Breast-Cancer-Ultrasound-Images-Dataset]"
|
| 133 |
"(https://huggingface.co/datasets/gymprathap/Breast-Cancer-Ultrasound-Images-Dataset)\n"
|
|
|
|
| 134 |
)
|
| 135 |
|
| 136 |
# ---- 5. Launch app ----
|