File size: 1,669 Bytes
88e0bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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