| from fastapi import FastAPI, Depends, BackgroundTasks |
| import gc |
|
|
| from src.encoder import FashionCLIPEncoder |
| from src.models import TextRequest, ImageRequest, Response |
| from src.auth import verify_token |
| from src.utils import delete_images |
|
|
|
|
| encoder = FashionCLIPEncoder(normalize=True) |
| app = FastAPI() |
| app.state.req_count = 0 |
| COLLECT_GC_EVERY = 20 |
|
|
|
|
| def cleanup_after_request(images): |
| if images is not None: |
| success = delete_images(images) |
| if not success: |
| print("Failed to delete images") |
|
|
| app.state.req_count += 1 |
| if app.state.req_count % COLLECT_GC_EVERY == 0: |
| gc.collect() |
|
|
|
|
| @app.get("/") |
| async def root(): |
| return { |
| "status": "ok", |
| } |
|
|
|
|
| @app.post("/encode_texts") |
| async def encode_texts( |
| request: TextRequest, |
| token: str = Depends(verify_token), |
| ) -> Response: |
| embeddings = encoder.encode_text(request.texts) |
| response = Response(embeddings=embeddings) |
|
|
| return response |
|
|
|
|
| @app.post("/encode_images") |
| async def encode_images( |
| request: ImageRequest, |
| background_tasks: BackgroundTasks, |
| token: str = Depends(verify_token), |
| ) -> Response: |
| images = request.download() |
| embeddings = encoder.encode_images(images) |
|
|
| background_tasks.add_task(cleanup_after_request, images) |
|
|
| return Response(embeddings=embeddings) |
|
|