Skip to content

Commit

Permalink
Add loading model and database message
Browse files Browse the repository at this point in the history
  • Loading branch information
xenova committed Sep 22, 2023
1 parent 50013bd commit 03b6ed8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
11 changes: 10 additions & 1 deletion examples/semantic-image-search-client/src/app/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ImageGrid } from './components/ImageGrid';
export default function Home() {

// Application state
const [ready, setReady] = useState(null);
const [images, setImages] = useState(null);
const [currentImage, setCurrentImage] = useState(null);

Expand All @@ -24,9 +25,12 @@ export default function Home() {
}
const onMessageReceived = (e) => {
switch (e.data.status) {
case 'initiate':
setReady(false);
break;
case 'ready':
setReady(true);
break;

case 'complete':
setImages(e.data.output);
break;
Expand All @@ -50,6 +54,11 @@ export default function Home() {
<main className="mx-auto max-w-[1960px] p-4 relative">
<Modal currentImage={currentImage} setCurrentImage={setCurrentImage} />
<SearchBar search={search} />
{ready === false && (
<div className="z-10 fixed top-0 left-0 w-full h-full bg-black bg-opacity-50 flex items-center justify-center">
<div className="text-white text-2xl font-bold">Loading model and database...</div>
</div>
)}
<ImageGrid images={images} setCurrentImage={setCurrentImage} />
</main>
)
Expand Down
11 changes: 7 additions & 4 deletions examples/semantic-image-search-client/src/app/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ class ApplicationSingleton {
static metadata = null;
static embeddings = null;

static async getInstance() {
static async getInstance(progress_callback = null) {
// Load tokenizer and text model
if (this.tokenizer === null) {
this.tokenizer = AutoTokenizer.from_pretrained(this.model_id);
this.tokenizer = AutoTokenizer.from_pretrained(this.model_id, { progress_callback });
}
if (this.text_model === null) {
this.text_model = CLIPTextModelWithProjection.from_pretrained(this.model_id);
this.text_model = CLIPTextModelWithProjection.from_pretrained(this.model_id, { progress_callback });
}
if (this.metadata === null) {
this.metadata = getCachedJSON(this.BASE_URL + 'image-embeddings.json');
Expand Down Expand Up @@ -74,7 +74,10 @@ function cosineSimilarity(query_embeds, database_embeds) {
self.addEventListener('message', async (event) => {
// Get the tokenizer, model, metadata, and embeddings. When called for the first time,
// this will load the files and cache them for future use.
const [tokenizer, text_model, metadata, embeddings] = await ApplicationSingleton.getInstance();
const [tokenizer, text_model, metadata, embeddings] = await ApplicationSingleton.getInstance(self.postMessage);

// Send the output back to the main thread
self.postMessage({ status: 'ready' });

// Run tokenization
const text_inputs = tokenizer(event.data.text, { padding: true, truncation: true });
Expand Down

0 comments on commit 03b6ed8

Please sign in to comment.