Spaces:
Sleeping
Sleeping
push
Browse files- Dockerfile +53 -0
- README.md +18 -0
- app/main.py +33 -0
- requirements.txt +6 -0
Dockerfile
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use a lightweight Python base
|
| 2 |
+
FROM python:3.10-slim
|
| 3 |
+
|
| 4 |
+
# Prevent interactive prompts & speed up Python
|
| 5 |
+
ENV DEBIAN_FRONTEND=noninteractive \
|
| 6 |
+
PYTHONUNBUFFERED=1 \
|
| 7 |
+
PYTHONDONTWRITEBYTECODE=1 \
|
| 8 |
+
PIP_NO_CACHE_DIR=1 \
|
| 9 |
+
TOKENIZERS_PARALLELISM=false
|
| 10 |
+
|
| 11 |
+
# Set work directory
|
| 12 |
+
WORKDIR /code
|
| 13 |
+
|
| 14 |
+
# Install system dependencies
|
| 15 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 16 |
+
build-essential \
|
| 17 |
+
git \
|
| 18 |
+
curl \
|
| 19 |
+
libopenblas-dev \
|
| 20 |
+
libomp-dev \
|
| 21 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 22 |
+
|
| 23 |
+
# Copy requirements first (for Docker caching)
|
| 24 |
+
COPY requirements.txt .
|
| 25 |
+
|
| 26 |
+
# Install Python dependencies
|
| 27 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 28 |
+
|
| 29 |
+
# Hugging Face tools
|
| 30 |
+
RUN pip install --no-cache-dir huggingface-hub accelerate
|
| 31 |
+
|
| 32 |
+
# Set Hugging Face cache inside container (persistent, not /tmp)
|
| 33 |
+
ENV HF_HOME=/models/huggingface
|
| 34 |
+
ENV TRANSFORMERS_CACHE=/models/huggingface
|
| 35 |
+
ENV HUGGINGFACE_HUB_CACHE=/models/huggingface
|
| 36 |
+
ENV HF_HUB_CACHE=/models/huggingface
|
| 37 |
+
|
| 38 |
+
# Create cache dir
|
| 39 |
+
RUN mkdir -p /models/huggingface
|
| 40 |
+
|
| 41 |
+
# Pre-download model at build time (BLIP captioning model)
|
| 42 |
+
RUN python -c "from huggingface_hub import snapshot_download; snapshot_download(repo_id='Salesforce/blip-image-captioning-large')"
|
| 43 |
+
|
| 44 |
+
# Copy project files
|
| 45 |
+
COPY . .
|
| 46 |
+
|
| 47 |
+
# Expose FastAPI port (Hugging Face Spaces uses 7860)
|
| 48 |
+
EXPOSE 7860
|
| 49 |
+
|
| 50 |
+
# Run FastAPI app with uvicorn (single worker)
|
| 51 |
+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
| 52 |
+
|
| 53 |
+
|
README.md
CHANGED
|
@@ -8,3 +8,21 @@ pinned: false
|
|
| 8 |
---
|
| 9 |
|
| 10 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 11 |
+
|
| 12 |
+
## Local development
|
| 13 |
+
|
| 14 |
+
Install dependencies and run the API:
|
| 15 |
+
|
| 16 |
+
```bash
|
| 17 |
+
python -m venv .venv
|
| 18 |
+
source .venv/bin/activate
|
| 19 |
+
pip install -r requirements.txt
|
| 20 |
+
uvicorn app.main:app --host 0.0.0.0 --port 8000
|
| 21 |
+
```
|
| 22 |
+
|
| 23 |
+
Test the captioning endpoint:
|
| 24 |
+
|
| 25 |
+
```bash
|
| 26 |
+
curl -X POST "http://localhost:8000/caption" \
|
| 27 |
+
-F "image=@/path/to/your/image.jpg"
|
| 28 |
+
```
|
app/main.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
app = FastAPI(title="QuickCare Captioning Endpoint")
|
| 8 |
+
|
| 9 |
+
blip_model = BlipForConditionalGeneration.from_pretrained(
|
| 10 |
+
"Salesforce/blip-image-captioning-large"
|
| 11 |
+
)
|
| 12 |
+
blip_processor = BlipProcessor.from_pretrained(
|
| 13 |
+
"Salesforce/blip-image-captioning-large"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 17 |
+
blip_model.to(device)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@app.post("/caption")
|
| 21 |
+
async def generate_caption(image: UploadFile = File(...)):
|
| 22 |
+
image_bytes = await image.read()
|
| 23 |
+
img = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 24 |
+
|
| 25 |
+
inputs = blip_processor(images=img, return_tensors="pt").to(device)
|
| 26 |
+
|
| 27 |
+
with torch.no_grad():
|
| 28 |
+
caption_ids = blip_model.generate(**inputs, max_new_tokens=60)
|
| 29 |
+
caption = blip_processor.decode(caption_ids[0], skip_special_tokens=True)
|
| 30 |
+
|
| 31 |
+
return {"caption": caption}
|
| 32 |
+
|
| 33 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
+
transformers
|
| 4 |
+
torch
|
| 5 |
+
Pillow
|
| 6 |
+
|