Spaces:
Sleeping
Sleeping
Download src/main.py from Oriskany/clip_api: direct link, hf CLI and curl.
- Browser
- Download file 3.03 kB
-
https://huggingface.co/spaces/Oriskany/clip_api/resolve/main/src/main.py
- Command line
-
hf download hf://spaces/Oriskany/clip_api/src/main.py
-
curl -L -o main.py https://huggingface.co/spaces/Oriskany/clip_api/resolve/main/src/main.py
3.03 kB
| import io | |
| import uvicorn | |
| from fastapi import FastAPI, UploadFile, status, Form | |
| from contextlib import asynccontextmanager | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.responses import JSONResponse, RedirectResponse | |
| from pydantic import BaseModel | |
| from config import settings | |
| from PIL import Image | |
| from faiss_db import FAISS_DB | |
| from vlm_model import VisionLanguageModel | |
| import torch | |
| import gc | |
| import base64 | |
| vectordb = vectordb = FAISS_DB( | |
| faiss_index_file_path=settings.FAISS_INDEX_FILE_PATH, | |
| keyframes_groups_json_path=settings.KEYFRAMES_GROUPS_JSON_PATH, | |
| ) | |
| vlm_model = VisionLanguageModel( | |
| model_name=settings.MODEL_NAME, | |
| pretrained=settings.MODEL_CHECKPOINT, | |
| device=( | |
| "cuda" if settings.DEVICE == "cuda" and torch.cuda.is_available() else "cpu" | |
| ), | |
| ) | |
| async def lifespan(app: FastAPI): | |
| global vectordb | |
| global vlm_model | |
| yield | |
| del vectordb | |
| del vlm_model | |
| torch.cuda.empty_cache() | |
| gc.collect() | |
| torch.cuda.ipc_collect() | |
| app = FastAPI(title="CLIP Retrieval API", lifespan=lifespan) | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_headers=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| ) | |
| async def root() -> None: | |
| return RedirectResponse("/docs") | |
| async def perform_healthcheck() -> None: | |
| return JSONResponse(content={"message": "success"}) | |
| class Item(BaseModel): | |
| query: str | UploadFile | |
| queryType: str | |
| topk: int | |
| async def retrieval( | |
| query: str | UploadFile = Form(...), | |
| queryType: str = Form("text"), # text || image | |
| topk: int = Form(...), | |
| ) -> JSONResponse: | |
| try: | |
| if queryType == "text": | |
| input_query = query | |
| elif queryType == "image": | |
| image_bytes = await query.read() | |
| input_query = Image.open(io.BytesIO(image_bytes)).convert("RGB") | |
| else: | |
| return JSONResponse( | |
| status_code=status.HTTP_400_BAD_REQUEST, | |
| content={ | |
| "message": "Invalid query type. Provide either a text query or an image." | |
| }, | |
| ) | |
| query_vector = vlm_model.get_embedding(input_query) | |
| except Exception as e: | |
| return JSONResponse( | |
| status_code=status.HTTP_400_BAD_REQUEST, | |
| content={"message": f"Invalid query: {str(e)}"}, | |
| ) | |
| try: | |
| search_results = vectordb.kNN_search(query_vector, topk) | |
| except Exception as e: | |
| return JSONResponse( | |
| status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | |
| content={"message": f"Search error: {str(e)}"}, | |
| ) | |
| return JSONResponse( | |
| status_code=status.HTTP_200_OK, | |
| content={"message": "success", "details": search_results}, | |
| ) | |
| # Start API | |
| if __name__ == "__main__": | |
| uvicorn.run("main:app", host=settings.HOST, port=settings.PORT, reload=True) |