|
from fastapi import FastAPI, HTTPException, status, Security |
|
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer |
|
from decouple import config |
|
|
|
from src.encoder import FashionCLIPEncoder |
|
from src.models import TextRequest, ImageRequest, Response |
|
|
|
|
|
security = HTTPBearer() |
|
encoder = FashionCLIPEncoder() |
|
|
|
|
|
API_TOKEN = config("API_TOKEN") |
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
@app.get("/") |
|
async def root(): |
|
return { |
|
"status": "ok", |
|
"message": "FashionCLIP API is running", |
|
"endpoints": { |
|
"encode_texts": "POST /encode_texts - Get embeddings for text inputs", |
|
"encode_images": "POST /encode_images - Get embeddings for image inputs", |
|
}, |
|
} |
|
|
|
|
|
@app.post("/encode_texts") |
|
async def encode_texts( |
|
request: TextRequest, credentials: HTTPAuthorizationCredentials = Security(security) |
|
) -> Response: |
|
if credentials.credentials != API_TOKEN: |
|
raise HTTPException( |
|
status_code=status.HTTP_401_UNAUTHORIZED, |
|
detail="Invalid authentication token", |
|
) |
|
|
|
embeddings = encoder.encode_text(request.texts) |
|
response = Response(embeddings=embeddings) |
|
|
|
return response |
|
|
|
|
|
@app.post("/encode_images") |
|
async def encode_images( |
|
request: ImageRequest, |
|
credentials: HTTPAuthorizationCredentials = Security(security), |
|
) -> Response: |
|
if credentials.credentials != API_TOKEN: |
|
raise HTTPException( |
|
status_code=status.HTTP_401_UNAUTHORIZED, |
|
detail="Invalid authentication token", |
|
) |
|
|
|
images = request.download() |
|
embeddings = encoder.encode_images(images) |
|
response = Response(embeddings=embeddings) |
|
|
|
return response |
|
|