Add background task for image cleanup and implement request count-based garbage collection
Browse files
app.py
CHANGED
|
@@ -1,28 +1,40 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
|
|
|
| 2 |
|
| 3 |
from src.encoder import FashionCLIPEncoder
|
| 4 |
from src.models import TextRequest, ImageRequest, Response
|
|
|
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
encoder = FashionCLIPEncoder(normalize=True)
|
| 8 |
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
@app.get("/")
|
| 12 |
async def root():
|
| 13 |
return {
|
| 14 |
"status": "ok",
|
| 15 |
-
"message": "FashionCLIP API is running",
|
| 16 |
-
"endpoints": {
|
| 17 |
-
"encode_texts": "POST /encode_texts - Get embeddings for text inputs",
|
| 18 |
-
"encode_images": "POST /encode_images - Get embeddings for image inputs",
|
| 19 |
-
},
|
| 20 |
}
|
| 21 |
|
| 22 |
|
| 23 |
@app.post("/encode_texts")
|
| 24 |
async def encode_texts(
|
| 25 |
request: TextRequest,
|
|
|
|
| 26 |
) -> Response:
|
| 27 |
embeddings = encoder.encode_text(request.texts)
|
| 28 |
response = Response(embeddings=embeddings)
|
|
@@ -33,9 +45,12 @@ async def encode_texts(
|
|
| 33 |
@app.post("/encode_images")
|
| 34 |
async def encode_images(
|
| 35 |
request: ImageRequest,
|
|
|
|
|
|
|
| 36 |
) -> Response:
|
| 37 |
images = request.download()
|
| 38 |
embeddings = encoder.encode_images(images)
|
| 39 |
-
response = Response(embeddings=embeddings)
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Depends, BackgroundTasks
|
| 2 |
+
import gc
|
| 3 |
|
| 4 |
from src.encoder import FashionCLIPEncoder
|
| 5 |
from src.models import TextRequest, ImageRequest, Response
|
| 6 |
+
from src.auth import verify_token
|
| 7 |
+
from src.utils import delete_images
|
| 8 |
|
| 9 |
|
| 10 |
encoder = FashionCLIPEncoder(normalize=True)
|
| 11 |
app = FastAPI()
|
| 12 |
+
app.state.req_count = 0
|
| 13 |
+
COLLECT_GC_EVERY = 20
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def cleanup_after_request(images):
|
| 17 |
+
if images is not None:
|
| 18 |
+
success = delete_images(images)
|
| 19 |
+
if not success:
|
| 20 |
+
print("Failed to delete images")
|
| 21 |
+
|
| 22 |
+
app.state.req_count += 1
|
| 23 |
+
if app.state.req_count % COLLECT_GC_EVERY == 0:
|
| 24 |
+
gc.collect()
|
| 25 |
|
| 26 |
|
| 27 |
@app.get("/")
|
| 28 |
async def root():
|
| 29 |
return {
|
| 30 |
"status": "ok",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
}
|
| 32 |
|
| 33 |
|
| 34 |
@app.post("/encode_texts")
|
| 35 |
async def encode_texts(
|
| 36 |
request: TextRequest,
|
| 37 |
+
token: str = Depends(verify_token),
|
| 38 |
) -> Response:
|
| 39 |
embeddings = encoder.encode_text(request.texts)
|
| 40 |
response = Response(embeddings=embeddings)
|
|
|
|
| 45 |
@app.post("/encode_images")
|
| 46 |
async def encode_images(
|
| 47 |
request: ImageRequest,
|
| 48 |
+
background_tasks: BackgroundTasks,
|
| 49 |
+
token: str = Depends(verify_token),
|
| 50 |
) -> Response:
|
| 51 |
images = request.download()
|
| 52 |
embeddings = encoder.encode_images(images)
|
|
|
|
| 53 |
|
| 54 |
+
background_tasks.add_task(cleanup_after_request, images)
|
| 55 |
+
|
| 56 |
+
return Response(embeddings=embeddings)
|