Download app.py from precove/fclip: direct link, hf CLI and curl.
- Browser
- Download file 1.31 kB
-
https://huggingface.co/spaces/precove/fclip/resolve/main/app.py
- Command line
-
hf download hf://spaces/precove/fclip/app.py
-
curl -L -o app.py https://huggingface.co/spaces/precove/fclip/resolve/main/app.py
1.31 kB
| 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() | |
| async def root(): | |
| return { | |
| "status": "ok", | |
| } | |
| async def encode_texts( | |
| request: TextRequest, | |
| token: str = Depends(verify_token), | |
| ) -> Response: | |
| embeddings = encoder.encode_text(request.texts) | |
| response = Response(embeddings=embeddings) | |
| return response | |
| 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) | |