π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
add clip search UI
Abhinav866ce000f532
diff --git a/apps/photos/src/components/PhotoList.tsx b/apps/photos/src/components/PhotoList.tsxindex 793be7193b..7323ab2432 100644--- a/apps/photos/src/components/PhotoList.tsx+++ b/apps/photos/src/components/PhotoList.tsx@@ -304,7 +304,9 @@ export function PhotoList({));}- if (deduplicateContext.isOnDeduplicatePage) {+ if (galleryContext.isClipSearchResult) {+ noGrouping(timeStampList);+ } else if (deduplicateContext.isOnDeduplicatePage) {skipMerge = true;groupByFileSize(timeStampList);} else {@@ -348,6 +350,7 @@ export function PhotoList({deduplicateContext.clubSameTimeFilesOnly,galleryContext.photoListHeader,publicCollectionGalleryContext.photoListHeader,+ galleryContext.isClipSearchResult,]);useEffect(() => {@@ -523,6 +526,23 @@ export function PhotoList({});};+ const noGrouping = (timeStampList: TimeStampListItem[]) => {+ let listItemIndex = columns;+ displayFiles.forEach((item, index) => {+ if (listItemIndex < columns) {+ timeStampList[timeStampList.length - 1].items.push(item);+ listItemIndex++;+ } else {+ listItemIndex = 1;+ timeStampList.push({+ itemType: ITEM_TYPE.FILE,+ items: [item],+ itemStartIndex: index,+ });+ }+ });+ };+const isSameDay = (first, second) => {return (first.getFullYear() === second.getFullYear() &&diff --git a/apps/photos/src/components/Search/SearchBar/searchInput/index.tsx b/apps/photos/src/components/Search/SearchBar/searchInput/index.tsxindex a5ca17b697..ca25436b58 100644--- a/apps/photos/src/components/Search/SearchBar/searchInput/index.tsx+++ b/apps/photos/src/components/Search/SearchBar/searchInput/index.tsx@@ -12,7 +12,13 @@ import {getAutoCompleteSuggestions,getDefaultOptions,} from 'services/searchService';-import { DateValue, Search, SearchOption, SuggestionType } from 'types/search';+import {+ ClipSearchScores,+ DateValue,+ Search,+ SearchOption,+ SuggestionType,+} from 'types/search';import { ValueContainerWithIcon } from './valueContainerWithIcon';import { SelectStyles } from '../../../../styles/search';import AsyncSelect from 'react-select/async';@@ -122,6 +128,8 @@ export default function SearchInput(props: Iprops) {case SuggestionType.FILE_TYPE:search = { fileType: selectedOption.value as FILE_TYPE };break;+ case SuggestionType.CLIP:+ search = { clip: selectedOption.value as ClipSearchScores };}props.updateSearch(search, {optionName: selectedOption.label,diff --git a/apps/photos/src/pages/gallery/index.tsx b/apps/photos/src/pages/gallery/index.tsxindex 2b5a9d5213..830a1fb761 100644--- a/apps/photos/src/pages/gallery/index.tsx+++ b/apps/photos/src/pages/gallery/index.tsx@@ -148,6 +148,7 @@ const defaultGalleryContext: GalleryContextType = {userIDToEmailMap: null,emailList: null,openHiddenSection: () => null,+ isClipSearchResult: null,};export const GalleryContext = createContext<GalleryContextType>(@@ -283,6 +284,9 @@ export default function Gallery() {});};+ const [isClipSearchResult, setIsClipSearchResult] =+ useState<boolean>(false);+useEffect(() => {appContext.showNavBar(true);const key = getKey(SESSION_KEYS.ENCRYPTION_KEY);@@ -471,9 +475,8 @@ export default function Gallery() {...files.filter((file) => deletedFileIds?.has(file.id)),]);}- const sortAsc = activeCollection?.pubMagicMetadata?.data?.asc ?? false;- return getUniqueFiles(+ const filteredFiles = getUniqueFiles((isInHiddenSection ? hiddenFiles : files).filter((item) => {if (deletedFileIds?.has(item.id)) {return false;@@ -532,6 +535,9 @@ export default function Gallery() {) {return false;}+ if (typeof search?.clip && search.clip.has(item.id)) {+ return false;+ }return true;}@@ -575,9 +581,19 @@ export default function Gallery() {} else {return false;}- }),- sortAsc+ }));+ if (search?.clip) {+ return filteredFiles.sort((a, b) => {+ return search.clip.get(b.id) - search.clip.get(a.id);+ });+ }+ const sortAsc = activeCollection?.pubMagicMetadata?.data?.asc ?? false;+ if (sortAsc) {+ return sortFiles(filteredFiles, true);+ } else {+ return filteredFiles;+ }}, [files,trashedFiles,@@ -899,6 +915,7 @@ export default function Gallery() {} else {setSearch(newSearch);}+ setIsClipSearchResult(!!newSearch.clip);if (!newSearch?.collection) {setIsInSearchMode(!!newSearch);setSetSearchResultSummary(summary);@@ -948,6 +965,7 @@ export default function Gallery() {user,emailList,openHiddenSection,+ isClipSearchResult,}}><FullScreenDropZonegetDragAndDropRootProps={getDragAndDropRootProps}>diff --git a/apps/photos/src/services/searchService.ts b/apps/photos/src/services/searchService.tsindex 48c09d4369..eb97616a0b 100644--- a/apps/photos/src/services/searchService.ts+++ b/apps/photos/src/services/searchService.ts@@ -13,6 +13,7 @@ import {SearchOption,Suggestion,SuggestionType,+ ClipSearchScores,} from 'types/search';import ObjectService from './machineLearning/objectService';import {@@ -26,6 +27,11 @@ import { getLatestEntities } from './entityService';import { LocationTag, LocationTagData, EntityType } from 'types/entity';import { addLogLine } from 'utils/logging';import { FILE_TYPE } from 'constants/file';+import {+ ClipService,+ computeClipMatchScore,+ getAllClipImageEmbeddings,+} from './clipService';const DIGITS = new Set(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']);@@ -54,6 +60,7 @@ export const getAutoCompleteSuggestions =getFileCaptionSuggestion(searchPhrase, files),...(await getLocationTagSuggestions(searchPhrase)),...(await getThingSuggestion(searchPhrase)),+ await getClipSuggestion(searchPhrase),];return convertSuggestionsToOptions(suggestions, files);@@ -271,6 +278,15 @@ async function getThingSuggestion(searchPhrase: string): Promise<Suggestion[]> {);}+async function getClipSuggestion(searchPhrase: string): Promise<Suggestion> {+ const clipResults = await searchClip(searchPhrase);+ return {+ type: SuggestionType.CLIP,+ value: clipResults,+ label: searchPhrase,+ };+}+function searchCollection(searchPhrase: string,collections: Collection[]@@ -343,6 +359,26 @@ async function searchThing(searchPhrase: string) {);}+async function searchClip(searchPhrase: string): Promise<ClipSearchScores> {+ const imageEmbeddings = await getAllClipImageEmbeddings();+ const textEmbedding = await ClipService.getTextEmbedding(searchPhrase);+ const clipSearchResult = new Map<number, number>(+ await Promise.all(+ imageEmbeddings.map(+ async (imageEmbedding): Promise<[number, number]> => [+ imageEmbedding.fileID,+ await computeClipMatchScore(+ imageEmbedding.embedding,+ textEmbedding+ ),+ ]+ )+ )+ );++ return clipSearchResult;+}+function isSearchedFile(file: EnteFile, search: Search) {if (search?.collection) {return search.collection === file.collectionID;@@ -379,6 +415,9 @@ function isSearchedFile(file: EnteFile, search: Search) {if (typeof search?.fileType !== 'undefined') {return search.fileType === file.metadata.fileType;}+ if (typeof search?.clip !== 'undefined') {+ return search.clip.has(file.id);+ }return false;}@@ -410,5 +449,7 @@ function convertSuggestionToSearchQuery(option: Suggestion): Search {return { thing: option.value as Thing };case SuggestionType.FILE_TYPE:return { fileType: option.value as FILE_TYPE };+ case SuggestionType.CLIP:+ return { clip: option.value as ClipSearchScores };}}diff --git a/apps/photos/src/types/gallery/index.ts b/apps/photos/src/types/gallery/index.tsindex 721ea34567..6cd5416fcc 100644--- a/apps/photos/src/types/gallery/index.ts+++ b/apps/photos/src/types/gallery/index.ts@@ -45,6 +45,7 @@ export type GalleryContextType = {userIDToEmailMap: Map<number, string>;emailList: string[];openHiddenSection: (callback?: () => void) => void;+ isClipSearchResult: boolean;};export enum CollectionSelectorIntent {diff --git a/apps/photos/src/types/search/index.ts b/apps/photos/src/types/search/index.tsindex ea38e9737c..1e41d53f18 100644--- a/apps/photos/src/types/search/index.ts+++ b/apps/photos/src/types/search/index.ts@@ -15,6 +15,7 @@ export enum SuggestionType {TEXT = 'TEXT',FILE_CAPTION = 'FILE_CAPTION',FILE_TYPE = 'FILE_TYPE',+ CLIP = 'CLIP',}export interface DateValue {@@ -34,7 +35,8 @@ export interface Suggestion {| Thing| WordGroup| LocationTagData- | FILE_TYPE;+ | FILE_TYPE+ | ClipSearchScores;hide?: boolean;}@@ -47,6 +49,7 @@ export type Search = {thing?: Thing;text?: WordGroup;fileType?: FILE_TYPE;+ clip?: ClipSearchScores;};export type SearchResultSummary = {@@ -63,3 +66,5 @@ export type UpdateSearch = (search: Search,summary: SearchResultSummary) => void;++export type ClipSearchScores = Map<number, number>;diff --git a/apps/photos/src/utils/file/index.ts b/apps/photos/src/utils/file/index.tsindex f5f324d01b..21a5084692 100644--- a/apps/photos/src/utils/file/index.ts+++ b/apps/photos/src/utils/file/index.ts@@ -617,7 +617,7 @@ export async function getFileFromURL(fileURL: string) {return fileFile;}-export function getUniqueFiles(files: EnteFile[], sortAsc = false) {+export function getUniqueFiles(files: EnteFile[]) {const idSet = new Set<number>();const uniqueFiles = files.filter((file) => {if (!idSet.has(file.id)) {@@ -628,11 +628,6 @@ export function getUniqueFiles(files: EnteFile[], sortAsc = false) {}});- if (sortAsc === true) {- return uniqueFiles.sort(- (a, b) => a.metadata.creationTime - b.metadata.creationTime- );- }return uniqueFiles;}