-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
366 additions
and
53 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
/* eslint-disable */ | ||
/** | ||
* This file was automatically generated by json-schema-to-typescript. | ||
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file, | ||
* and run json-schema-to-typescript to regenerate this file. | ||
*/ | ||
|
||
|
||
/** | ||
* Image Solr document in Impresso v2 | ||
*/ | ||
export interface Image { | ||
id?: string; | ||
meta_journal_s?: string; | ||
meta_year_i?: number; | ||
meta_day_i?: number; | ||
meta_ed_s?: string; | ||
meta_issue_id_s?: string; | ||
meta_date_dt?: string; | ||
linked_ci_s?: string; | ||
reading_order_i?: number; | ||
caption_txt?: string[]; | ||
item_type_s?: string; | ||
page_nb_is?: number[]; | ||
coords_is?: number[]; | ||
front_b?: boolean; | ||
iiif_url_s?: string; | ||
cc_b?: boolean; | ||
rights_data_domain_s?: string; | ||
rights_copyright_s?: string; | ||
rights_perm_use_explore_plain?: string[]; | ||
rights_perm_use_get_tr_plain?: string[]; | ||
rights_perm_use_get_img_plain?: string[]; | ||
rights_bm_explore_l?: number; | ||
rights_bm_get_tr_l?: number; | ||
rights_bm_get_img_l?: number; | ||
dinov2_emb_v1024?: number[]; | ||
openclip_emb_v768?: number[]; | ||
_version_?: number; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"title": "Image", | ||
"description": "An image from a content item", | ||
"type": "object", | ||
"additionalProperties": false, | ||
"properties": { | ||
"uid": { | ||
"type": "string", | ||
"description": "The unique identifier of the image" | ||
}, | ||
"issueUid": { | ||
"type": "string", | ||
"description": "The unique identifier of the issue that the image belongs to." | ||
}, | ||
"contentItemUid": { | ||
"type": "string", | ||
"description": "The unique identifier of the content item that the image belongs to." | ||
}, | ||
"previewUrl": { | ||
"type": "string", | ||
"format": "uri", | ||
"description": "The URL of the image preview" | ||
} | ||
}, | ||
"required": ["uid", "issueUid", "previewUrl"] | ||
} |
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,60 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"type": "object", | ||
"title": "Image", | ||
"description": "Image Solr document in Impresso v2", | ||
"properties": { | ||
"id": { "type": "string" }, | ||
"meta_journal_s": { "type": "string" }, | ||
"meta_year_i": { "type": "integer" }, | ||
"meta_day_i": { "type": "integer" }, | ||
"meta_ed_s": { "type": "string" }, | ||
"meta_issue_id_s": { "type": "string" }, | ||
"meta_date_dt": { "type": "string", "format": "date-time" }, | ||
"linked_ci_s": { "type": "string" }, | ||
"reading_order_i": { "type": "integer" }, | ||
"caption_txt": { | ||
"type": "array", | ||
"items": { "type": "string" } | ||
}, | ||
"item_type_s": { "type": "string" }, | ||
"page_nb_is": { | ||
"type": "array", | ||
"items": { "type": "integer" } | ||
}, | ||
"coords_is": { | ||
"type": "array", | ||
"items": { "type": "integer" } | ||
}, | ||
"front_b": { "type": "boolean" }, | ||
"iiif_url_s": { "type": "string", "format": "uri" }, | ||
"cc_b": { "type": "boolean" }, | ||
"rights_data_domain_s": { "type": "string" }, | ||
"rights_copyright_s": { "type": "string" }, | ||
"rights_perm_use_explore_plain": { | ||
"type": "array", | ||
"items": { "type": "string" } | ||
}, | ||
"rights_perm_use_get_tr_plain": { | ||
"type": "array", | ||
"items": { "type": "string" } | ||
}, | ||
"rights_perm_use_get_img_plain": { | ||
"type": "array", | ||
"items": { "type": "string" } | ||
}, | ||
"rights_bm_explore_l": { "type": "integer" }, | ||
"rights_bm_get_tr_l": { "type": "integer" }, | ||
"rights_bm_get_img_l": { "type": "integer" }, | ||
"dinov2_emb_v1024": { | ||
"type": "array", | ||
"items": { "type": "number" } | ||
}, | ||
"openclip_emb_v768": { | ||
"type": "array", | ||
"items": { "type": "number" } | ||
}, | ||
"_version_": { "type": "number" } | ||
}, | ||
"additionalProperties": false | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { NotFound } from '@feathersjs/errors' | ||
import { ClientService, Id, Params } from '@feathersjs/feathers' | ||
import { SimpleSolrClient } from '../../internalServices/simpleSolr' | ||
import { PublicFindResponse } from '../../models/common' | ||
import { Image } from '../../models/generated/schemas' | ||
import { Image as ImageDocument } from '../../models/generated/solr' | ||
import { SolrNamespaces } from '../../solr' | ||
|
||
const DefaultLimit = 10 | ||
const ImageSimilarityVectorField: keyof ImageDocument = 'dinov2_emb_v1024' | ||
|
||
export interface FindQuery { | ||
similar_to_image_id?: string | ||
limit?: number | ||
offset?: number | ||
} | ||
|
||
export class Images implements Pick<ClientService<Image, unknown, unknown, PublicFindResponse<Image>>, 'find' | 'get'> { | ||
constructor(private readonly solrClient: SimpleSolrClient) {} | ||
|
||
async find(params?: Params<FindQuery>): Promise<PublicFindResponse<Image>> { | ||
const limit = params?.query?.limit ?? DefaultLimit | ||
const offset = params?.query?.offset ?? 0 | ||
|
||
const queryParts: string[] = [] | ||
|
||
if (params?.query?.similar_to_image_id) { | ||
const referenceId = params.query.similar_to_image_id | ||
const referenceImage = await this.getImageDocument(referenceId, [ImageSimilarityVectorField]) | ||
const vector = referenceImage?.[ImageSimilarityVectorField] as number[] | ||
|
||
if (referenceImage == null || vector == null) | ||
return { | ||
data: [], | ||
pagination: { | ||
limit: limit ?? 0, | ||
offset: offset ?? 0, | ||
total: 0, | ||
}, | ||
} | ||
|
||
queryParts.push(`{!knn f=${ImageSimilarityVectorField} topK=${limit}}${JSON.stringify(vector)}`) | ||
} | ||
|
||
const query = queryParts.length > 0 ? queryParts.join(' AND ') : '*:*' | ||
|
||
const results = await this.solrClient.select<ImageDocument>(SolrNamespaces.Images, { | ||
body: { | ||
query, | ||
limit, | ||
offset, | ||
}, | ||
}) | ||
|
||
return { | ||
data: results?.response?.docs?.map(toImage) ?? [], | ||
pagination: { | ||
limit: 0, | ||
offset: 0, | ||
total: 0, | ||
}, | ||
} | ||
} | ||
|
||
async get(id: Id, params?: Params): Promise<Image> { | ||
const imageDoc = await this.getImageDocument(String(id)) | ||
if (imageDoc == null) throw new NotFound(`Image with id ${id} not found`) | ||
return toImage(imageDoc) | ||
} | ||
|
||
async getImageDocument(id: string, fields?: (keyof ImageDocument)[]): Promise<ImageDocument | undefined> { | ||
const result = await this.solrClient.selectOne<ImageDocument>(SolrNamespaces.Images, { | ||
body: { query: `id:${id}`, limit: 1, fields: fields != null ? fields?.join(',') : undefined }, | ||
}) | ||
return result | ||
} | ||
} | ||
|
||
const toImage = (doc: ImageDocument): Image => { | ||
return { | ||
uid: doc.id!, | ||
...(doc.linked_ci_s != null ? { contentItemUid: doc.linked_ci_s } : {}), | ||
issueUid: doc.meta_issue_id_s!, | ||
previewUrl: doc.iiif_url_s!, | ||
} | ||
} |
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,10 @@ | ||
import { HookMap } from '@feathersjs/feathers' | ||
import { authenticateAround as authenticate } from '../../hooks/authenticate' | ||
import { rateLimit } from '../../hooks/rateLimiter' | ||
import { AppServices, ImpressoApplication } from '../../types' | ||
|
||
export default { | ||
around: { | ||
all: [authenticate({ allowUnauthenticated: true }), rateLimit()], | ||
}, | ||
} satisfies HookMap<ImpressoApplication, AppServices> |
Oops, something went wrong.