Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,14 +30,14 @@ app.add_middleware(
|
|
| 30 |
# Dataset with FAISS index + radiology_metadata.csv
|
| 31 |
EMBED_REPO_ID = "saad003/Red01"
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
|
|
|
| 35 |
BASE_IMAGE_URL = (
|
| 36 |
f"https://huggingface.co/datasets/{IMAGE_REPO_ID}/resolve/main"
|
| 37 |
)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 41 |
|
| 42 |
# ---------- Download index + metadata ----------
|
| 43 |
print("Downloading FAISS index & metadata from Hugging Face...")
|
|
@@ -62,7 +62,6 @@ index = faiss.read_index(INDEX_PATH)
|
|
| 62 |
print("Loading metadata CSV...")
|
| 63 |
metadata = pd.read_csv(META_PATH)
|
| 64 |
|
| 65 |
-
# We only need these columns
|
| 66 |
required_cols = {"vec_index", "ID", "caption", "concepts_manual"}
|
| 67 |
missing = required_cols - set(metadata.columns)
|
| 68 |
if missing:
|
|
@@ -95,112 +94,157 @@ print("Backend ready ✅")
|
|
| 95 |
|
| 96 |
|
| 97 |
# ---------- Helpers ----------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
def id_to_image_url(image_id: str) -> str:
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
return f"{BASE_IMAGE_URL}/{filename}"
|
| 111 |
|
| 112 |
|
| 113 |
def search_similar_by_image(image: Image.Image, k: int = 5) -> pd.DataFrame:
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
|
| 125 |
-
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
|
| 135 |
|
| 136 |
def generate_query_caption(image: Image.Image) -> str:
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
|
| 144 |
|
| 145 |
def infer_modality_from_caption(caption: str) -> str:
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
return "Unknown"
|
| 149 |
-
|
| 150 |
-
text = caption.lower()
|
| 151 |
-
|
| 152 |
-
if any(w in text for w in ["ct scan", "ct of", "computed tomography"]):
|
| 153 |
-
return "CT"
|
| 154 |
-
if any(w in text for w in ["mri", "magnetic resonance"]):
|
| 155 |
-
return "MRI"
|
| 156 |
-
if any(w in text for w in ["x-ray", "x ray", "radiograph", "chest xray", "chest x-ray"]):
|
| 157 |
-
return "X-ray"
|
| 158 |
-
if any(w in text for w in ["ultrasound", "sonography", "sonogram"]):
|
| 159 |
-
return "Ultrasound"
|
| 160 |
-
if any(w in text for w in ["pet-ct", "pet ct", "pet scan", "positron emission tomography"]):
|
| 161 |
-
return "PET/CT"
|
| 162 |
-
|
| 163 |
return "Unknown"
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
# ---------- Routes ----------
|
| 167 |
@app.get("/")
|
| 168 |
def root():
|
| 169 |
-
|
| 170 |
|
| 171 |
|
| 172 |
@app.post("/search_by_image")
|
| 173 |
async def search_by_image(file: UploadFile = File(...), k: int = 5):
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
|
|
|
| 30 |
# Dataset with FAISS index + radiology_metadata.csv
|
| 31 |
EMBED_REPO_ID = "saad003/Red01"
|
| 32 |
|
| 33 |
+
# NEW dataset with images organized into subfolders
|
| 34 |
+
# test, valid, train01, train02, ..., train07
|
| 35 |
+
IMAGE_REPO_ID = "saad003/images04"
|
| 36 |
BASE_IMAGE_URL = (
|
| 37 |
f"https://huggingface.co/datasets/{IMAGE_REPO_ID}/resolve/main"
|
| 38 |
)
|
| 39 |
|
| 40 |
+
HF_TOKEN = os.environ.get("HF_TOKEN") # set in HF Space secrets if needed
|
|
|
|
| 41 |
|
| 42 |
# ---------- Download index + metadata ----------
|
| 43 |
print("Downloading FAISS index & metadata from Hugging Face...")
|
|
|
|
| 62 |
print("Loading metadata CSV...")
|
| 63 |
metadata = pd.read_csv(META_PATH)
|
| 64 |
|
|
|
|
| 65 |
required_cols = {"vec_index", "ID", "caption", "concepts_manual"}
|
| 66 |
missing = required_cols - set(metadata.columns)
|
| 67 |
if missing:
|
|
|
|
| 94 |
|
| 95 |
|
| 96 |
# ---------- Helpers ----------
|
| 97 |
+
def train_folder_from_id(image_id: str) -> str:
|
| 98 |
+
"""
|
| 99 |
+
For IDs like 'ROCOv2_2023_train_000001', decide which trainXX folder.
|
| 100 |
+
Uses numeric ranges based on the last 6 digits.
|
| 101 |
+
"""
|
| 102 |
+
try:
|
| 103 |
+
num_str = image_id.split("_")[-1] # "000001"
|
| 104 |
+
num = int(num_str)
|
| 105 |
+
except Exception:
|
| 106 |
+
return "train01" # safe default
|
| 107 |
+
|
| 108 |
+
if num <= 9000:
|
| 109 |
+
return "train01"
|
| 110 |
+
elif num <= 18000:
|
| 111 |
+
return "train02"
|
| 112 |
+
elif num <= 27000:
|
| 113 |
+
return "train03"
|
| 114 |
+
elif num <= 36000:
|
| 115 |
+
return "train04"
|
| 116 |
+
elif num <= 45000:
|
| 117 |
+
return "train05"
|
| 118 |
+
elif num <= 54000:
|
| 119 |
+
return "train06"
|
| 120 |
+
else:
|
| 121 |
+
return "train07"
|
| 122 |
+
|
| 123 |
+
|
| 124 |
def id_to_image_url(image_id: str) -> str:
|
| 125 |
+
"""
|
| 126 |
+
Build raw image URL based on ID and folder structure.
|
| 127 |
+
|
| 128 |
+
Examples:
|
| 129 |
+
ROCOv2_2023_test_000001 -> test/ROCOv2_2023_test_000001.jpg
|
| 130 |
+
ROCOv2_2023_valid_000005 -> valid/ROCOv2_2023_valid_000005.jpg
|
| 131 |
+
ROCOv2_2023_train_000001 -> train01/ROCOv2_2023_train_000001.jpg
|
| 132 |
+
ROCOv2_2023_train_009001 -> train02/ROCOv2_2023_train_009001.jpg
|
| 133 |
+
"""
|
| 134 |
+
if not isinstance(image_id, str):
|
| 135 |
+
return None
|
| 136 |
+
|
| 137 |
+
image_id = image_id.strip()
|
| 138 |
+
|
| 139 |
+
if "test_" in image_id:
|
| 140 |
+
folder = "test"
|
| 141 |
+
elif "valid_" in image_id:
|
| 142 |
+
folder = "valid"
|
| 143 |
+
elif "train_" in image_id:
|
| 144 |
+
folder = train_folder_from_id(image_id)
|
| 145 |
+
else:
|
| 146 |
+
# Fallback: put directly at root (in case of weird ID)
|
| 147 |
+
folder = ""
|
| 148 |
+
|
| 149 |
+
filename = f"{image_id}.jpg"
|
| 150 |
+
|
| 151 |
+
if folder:
|
| 152 |
+
return f"{BASE_IMAGE_URL}/{folder}/{filename}"
|
| 153 |
+
else:
|
| 154 |
return f"{BASE_IMAGE_URL}/{filename}"
|
| 155 |
|
| 156 |
|
| 157 |
def search_similar_by_image(image: Image.Image, k: int = 5) -> pd.DataFrame:
|
| 158 |
+
"""
|
| 159 |
+
Encode query image with CLIP, search FAISS, and return top-k rows
|
| 160 |
+
with vec_index, ID, caption, concepts_manual, score, image_url.
|
| 161 |
+
"""
|
| 162 |
+
inputs = clip_processor(images=image, return_tensors="pt").to(device)
|
| 163 |
+
with torch.no_grad():
|
| 164 |
+
feats = clip_model.get_image_features(**inputs)
|
| 165 |
|
| 166 |
+
feats = feats / feats.norm(p=2, dim=-1, keepdim=True)
|
| 167 |
+
feats = feats.cpu().numpy().astype("float32")
|
| 168 |
|
| 169 |
+
D, I = index.search(feats, k)
|
| 170 |
|
| 171 |
+
rows = metadata.iloc[I[0]].copy()
|
| 172 |
+
rows["score"] = D[0]
|
| 173 |
+
rows["image_url"] = rows["ID"].apply(id_to_image_url)
|
| 174 |
|
| 175 |
+
return rows[
|
| 176 |
+
["vec_index", "ID", "caption", "concepts_manual", "score", "image_url"]
|
| 177 |
+
]
|
| 178 |
|
| 179 |
|
| 180 |
def generate_query_caption(image: Image.Image) -> str:
|
| 181 |
+
"""Generate a medical caption for the query image using BLIP."""
|
| 182 |
+
inputs = caption_processor(images=image, return_tensors="pt").to(device)
|
| 183 |
+
with torch.no_grad():
|
| 184 |
+
out = caption_model.generate(**inputs, max_new_tokens=64)
|
| 185 |
+
caption = caption_processor.batch_decode(out, skip_special_tokens=True)[0]
|
| 186 |
+
return caption.strip()
|
| 187 |
|
| 188 |
|
| 189 |
def infer_modality_from_caption(caption: str) -> str:
|
| 190 |
+
"""Heuristic to infer modality from caption text."""
|
| 191 |
+
if not caption:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
return "Unknown"
|
| 193 |
|
| 194 |
+
text = caption.lower()
|
| 195 |
+
|
| 196 |
+
if any(w in text for w in ["ct scan", "ct of", "computed tomography"]):
|
| 197 |
+
return "CT"
|
| 198 |
+
if any(w in text for w in ["mri", "magnetic resonance"]):
|
| 199 |
+
return "MRI"
|
| 200 |
+
if any(w in text for w in ["x-ray", "x ray", "radiograph", "chest xray", "chest x-ray"]):
|
| 201 |
+
return "X-ray"
|
| 202 |
+
if any(w in text for w in ["ultrasound", "sonography", "sonogram"]):
|
| 203 |
+
return "Ultrasound"
|
| 204 |
+
if any(w in text for w in ["pet-ct", "pet ct", "pet scan", "positron emission tomography"]):
|
| 205 |
+
return "PET/CT"
|
| 206 |
+
|
| 207 |
+
return "Unknown"
|
| 208 |
+
|
| 209 |
|
| 210 |
# ---------- Routes ----------
|
| 211 |
@app.get("/")
|
| 212 |
def root():
|
| 213 |
+
return {"status": "ok", "message": "Radiology retrieval + captioning API"}
|
| 214 |
|
| 215 |
|
| 216 |
@app.post("/search_by_image")
|
| 217 |
async def search_by_image(file: UploadFile = File(...), k: int = 5):
|
| 218 |
+
"""
|
| 219 |
+
Upload a radiology image.
|
| 220 |
+
|
| 221 |
+
Returns:
|
| 222 |
+
- query_caption: BLIP caption for query image
|
| 223 |
+
- modality: inferred imaging modality
|
| 224 |
+
- results: list of similar images with
|
| 225 |
+
vec_index, ID, concepts_manual, score, image_url
|
| 226 |
+
"""
|
| 227 |
+
content = await file.read()
|
| 228 |
+
image = Image.open(io.BytesIO(content)).convert("RGB")
|
| 229 |
+
|
| 230 |
+
# 1) Retrieval
|
| 231 |
+
results_df = search_similar_by_image(image, k=k)
|
| 232 |
+
results = results_df.to_dict(orient="records")
|
| 233 |
+
|
| 234 |
+
# 2) Caption for query image
|
| 235 |
+
try:
|
| 236 |
+
query_caption = generate_query_caption(image)
|
| 237 |
+
except Exception as e:
|
| 238 |
+
print("Error generating caption:", e)
|
| 239 |
+
query_caption = None
|
| 240 |
+
|
| 241 |
+
# 3) Modality
|
| 242 |
+
modality = infer_modality_from_caption(query_caption or "")
|
| 243 |
+
|
| 244 |
+
return JSONResponse(
|
| 245 |
+
{
|
| 246 |
+
"query_caption": query_caption,
|
| 247 |
+
"modality": modality,
|
| 248 |
+
"results": results,
|
| 249 |
+
}
|
| 250 |
+
)
|