Spaces:
Build error
Build error
upload image-search-blip-clip.zip
Browse files
image-search-blip-clip/Dockerfile
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
RUN apt-get update && apt-get install -y git libgl1-mesa-glx
|
| 6 |
+
|
| 7 |
+
COPY requirements.txt .
|
| 8 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 9 |
+
|
| 10 |
+
COPY . .
|
| 11 |
+
|
| 12 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
image-search-blip-clip/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# BLIP + CLIP Image Search API
|
| 2 |
+
|
| 3 |
+
A deployable image caption + search backend using BLIP for image captioning and CLIP for image-text embedding.
|
| 4 |
+
|
| 5 |
+
## Usage
|
| 6 |
+
|
| 7 |
+
1. Upload an image to `/upload`
|
| 8 |
+
2. Search with text using `/search?query=...`
|
| 9 |
+
|
| 10 |
+
## Endpoints
|
| 11 |
+
|
| 12 |
+
- `POST /upload`: Upload an image, return caption
|
| 13 |
+
- `POST /search`: Query images using text, return best matches
|
| 14 |
+
|
| 15 |
+
Deploy this to Hugging Face Spaces with Docker + GPU.
|
image-search-blip-clip/app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from models import generate_caption, get_img_emb, get_text_emb
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
db = []
|
| 9 |
+
|
| 10 |
+
class SearchResponse(BaseModel):
|
| 11 |
+
caption: str
|
| 12 |
+
score: float
|
| 13 |
+
|
| 14 |
+
@app.post("/upload")
|
| 15 |
+
async def upload(image: UploadFile = File(...)):
|
| 16 |
+
img = Image.open(image.file).convert("RGB")
|
| 17 |
+
cap = generate_caption(img)
|
| 18 |
+
vec = get_img_emb(img)
|
| 19 |
+
db.append({"caption": cap, "vector": vec})
|
| 20 |
+
return {"caption": cap}
|
| 21 |
+
|
| 22 |
+
@app.post("/search", response_model=list[SearchResponse])
|
| 23 |
+
async def search(query: str):
|
| 24 |
+
qv = get_text_emb(query)
|
| 25 |
+
results = []
|
| 26 |
+
for entry in db:
|
| 27 |
+
cosine = torch.cosine_similarity(qv, entry["vector"], dim=-1).item()
|
| 28 |
+
results.append({"caption": entry["caption"], "score": cosine})
|
| 29 |
+
results.sort(key=lambda x: -x["score"])
|
| 30 |
+
return results[:5]
|
image-search-blip-clip/models.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 2 |
+
import torch
|
| 3 |
+
import open_clip
|
| 4 |
+
|
| 5 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 6 |
+
|
| 7 |
+
blip_proc = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 8 |
+
blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to(device)
|
| 9 |
+
|
| 10 |
+
clip_model, _, preprocess = open_clip.create_model_and_transforms('ViT-B-32', pretrained='laion2b_s34b_b79k')
|
| 11 |
+
clip_tokenizer = open_clip.get_tokenizer('ViT-B-32')
|
| 12 |
+
clip_model = clip_model.to(device)
|
| 13 |
+
|
| 14 |
+
def generate_caption(img):
|
| 15 |
+
inputs = blip_proc(img, return_tensors="pt").to(device)
|
| 16 |
+
out = blip_model.generate(**inputs)
|
| 17 |
+
return blip_proc.decode(out[0], skip_special_tokens=True)
|
| 18 |
+
|
| 19 |
+
def get_img_emb(img):
|
| 20 |
+
im = preprocess(img).unsqueeze(0).to(device)
|
| 21 |
+
with torch.no_grad():
|
| 22 |
+
return clip_model.encode_image(im).cpu()
|
| 23 |
+
|
| 24 |
+
def get_text_emb(text):
|
| 25 |
+
tokens = clip_tokenizer([text]).to(device)
|
| 26 |
+
with torch.no_grad():
|
| 27 |
+
return clip_model.encode_text(tokens).cpu()
|
image-search-blip-clip/requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
torch
|
| 4 |
+
transformers
|
| 5 |
+
openclip-torch
|
| 6 |
+
Pillow
|