-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from hv0905/images-api
Add images API
- Loading branch information
Showing
8 changed files
with
122 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from typing import Annotated | ||
from uuid import UUID | ||
|
||
from fastapi import APIRouter, Depends, Path, HTTPException, Query | ||
|
||
from app.Models.api_response.images_api_response import QueryByIdApiResponse, ImageStatus, QueryImagesApiResponse | ||
from app.Models.query_params import FilterParams | ||
from app.Services.authentication import force_access_token_verify | ||
from app.Services.provider import ServiceProvider | ||
from app.Services.vector_db_context import PointNotFoundError | ||
from app.config import config | ||
|
||
images_router = APIRouter(dependencies=([Depends(force_access_token_verify)] if config.access_protected else None), | ||
tags=["Images"]) | ||
|
||
services: ServiceProvider | None = None # The service provider will be injected in the webapp initialize | ||
|
||
|
||
@images_router.get("/id/{image_id}", description="Query the image info with the given image ID. \n" | ||
"This can also be used to check the status" | ||
" of an image in the index queue.") | ||
async def query_image_by_id(image_id: Annotated[UUID, Path(description="The id of the image you want to query.")]): | ||
try: | ||
return QueryByIdApiResponse(img=await services.db_context.retrieve_by_id(str(image_id)), | ||
img_status=ImageStatus.MAPPED, | ||
message="Success query the image with the given ID.") | ||
except PointNotFoundError as ex: | ||
if services.upload_service and image_id in services.upload_service.uploading_ids: | ||
return QueryByIdApiResponse(img_status=ImageStatus.IN_QUEUE, message="The image is in the indexing queue.") | ||
raise HTTPException(404, "Cannot find the image with the given ID.") from ex | ||
|
||
|
||
@images_router.get("/", description="Query images in order of ID.") | ||
async def scroll_images(filter_param: Annotated[FilterParams, Depends()], | ||
prev_offset_id: Annotated[UUID, Query(description="The previous offset image ID.")] = None, | ||
count: Annotated[int, Query(ge=1, le=100, description="The number of images to query.")] = 15): | ||
# validate the offset ID | ||
if prev_offset_id is not None and len(await services.db_context.validate_ids([str(prev_offset_id)])) == 0: | ||
raise HTTPException(404, "The previous offset ID is invalid.") | ||
images, offset = await services.db_context.scroll_points(str(prev_offset_id), count, filter_param=filter_param) | ||
return QueryImagesApiResponse(images=images, next_page_offset=offset, message="Success query images.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from enum import Enum | ||
|
||
from pydantic import Field | ||
|
||
from app.Models.api_response.base import NekoProtocol | ||
from app.Models.img_data import ImageData | ||
|
||
|
||
class ImageStatus(str, Enum): | ||
MAPPED = "mapped" | ||
IN_QUEUE = "in_queue" | ||
|
||
|
||
class QueryByIdApiResponse(NekoProtocol): | ||
img_status: ImageStatus = Field(description="The status of the image.\n" | ||
"Warning: If NekoImageGallery is deployed in a cluster, " | ||
"the `in_queue` might not be accurate since the index queue " | ||
"is independent of each service instance.") | ||
img: ImageData | None = Field(description="The mapped image data. Only available when `img_status = mapped`.") | ||
|
||
|
||
class QueryImagesApiResponse(NekoProtocol): | ||
images: list[ImageData] = Field(description="The list of images.") | ||
next_page_offset: str | None = Field(description="The offset ID for the next page query. " | ||
"If there are no more images, this field will be null.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters