Spaces:
Running
Running
Upload 3 files
Browse files- Dockerfile +16 -0
- app.py +66 -0
- requirements.txt +6 -0
Dockerfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY . /app
|
| 6 |
+
|
| 7 |
+
RUN pip install --no-cache-dir -r requirements.txt uvicorn
|
| 8 |
+
|
| 9 |
+
ENV HF_HOME=/home/user/cache
|
| 10 |
+
ENV TORCH_HOME=/home/user/cache
|
| 11 |
+
RUN mkdir -p /home/user/cache && chmod -R 777 /home/user/cache
|
| 12 |
+
COPY . .
|
| 13 |
+
|
| 14 |
+
EXPOSE 7860
|
| 15 |
+
|
| 16 |
+
CMD ["uvicorn","app:app","--host","0.0.0.0","--port","7860"]
|
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
import torch
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
import logging
|
| 5 |
+
import os
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from transformers import CLIPProcessor, CLIPModel
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
load_dotenv()
|
| 11 |
+
logging.basicConfig(level=logging.INFO)
|
| 12 |
+
logger = logging.getLogger(__name__)
|
| 13 |
+
|
| 14 |
+
app = FastAPI(title='CLIP API',
|
| 15 |
+
description='Returns CLIP embedding for text and image')
|
| 16 |
+
|
| 17 |
+
HF_TOKEN = os.getenv('hf_token')
|
| 18 |
+
|
| 19 |
+
logger.info("Loading CLIP processor and model")
|
| 20 |
+
try:
|
| 21 |
+
processor = CLIPProcessor.from_pretrained(
|
| 22 |
+
"openai/clip-vit-large-patch14", use_auth_token=HF_TOKEN
|
| 23 |
+
)
|
| 24 |
+
clip_model = CLIPModel.from_pretrained(
|
| 25 |
+
"openai/clip-vit-large-patch14", use_auth_token=HF_TOKEN)
|
| 26 |
+
clip_model.eval()
|
| 27 |
+
logger.info("CLIP model loaded successfully")
|
| 28 |
+
except Exception as e:
|
| 29 |
+
logger.error(f"Failed to load CLIP model : {e}")
|
| 30 |
+
raise
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def get_text_embedding(text: str):
|
| 34 |
+
try:
|
| 35 |
+
inputs = processor(text=[text], return_tensors="pt",
|
| 36 |
+
padding=True, truncation=True)
|
| 37 |
+
with torch.no_grad():
|
| 38 |
+
text_embeddings = clip_model.get_text_features(**inputs)
|
| 39 |
+
logger.info("Text embedding generated")
|
| 40 |
+
return text_embeddings.squeeze(0).tolist()
|
| 41 |
+
except Exception as e:
|
| 42 |
+
logger.error(f"Error while generating embedding : {e}")
|
| 43 |
+
raise
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
@app.get("/")
|
| 47 |
+
async def root():
|
| 48 |
+
logger.info("Root endpoint accessed")
|
| 49 |
+
return {"message": "Welcome to the CLIP embedding API."}
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
@app.get("/embedding")
|
| 53 |
+
async def get_embedding_text(text: str):
|
| 54 |
+
logger.info(f"Embedding endpoint called with text")
|
| 55 |
+
embedding = get_text_embedding(text)
|
| 56 |
+
return {"embedding": embedding, "dimension": len(embedding)}
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
@app.post("/clip/process")
|
| 60 |
+
async def process_image(file: UploadFile = File(...)):
|
| 61 |
+
logger.info("Processing image")
|
| 62 |
+
image = Image.open(file.file).convert("RGB")
|
| 63 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 64 |
+
with torch.no_grad():
|
| 65 |
+
embeddings = clip_model.get_image_features(**inputs)
|
| 66 |
+
return {"embedding": embeddings.tolist()}
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers==4.49.0
|
| 2 |
+
fastapi==0.115.11
|
| 3 |
+
pydantic==2.10.6
|
| 4 |
+
torch==2.6.0
|
| 5 |
+
pillow==11.1.0
|
| 6 |
+
python-dotenv==1.0.1
|