Skip to content

Commit 99db378

Browse files
committed
Update semantic image search example README
1 parent b1537e2 commit 99db378

File tree

1 file changed

+83
-20
lines changed

1 file changed

+83
-20
lines changed
+83-20
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,97 @@
1-
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
1+
2+
# Semantic Image Search
3+
4+
This example shows you how to use Transformers.js to create a semantic image search engine. Check out the demo [here](https://huggingface.co/spaces/Xenova/semantic-image-search).
5+
6+
![Semantic Image Search Demo](https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/semantic-image-search-min.png)
27

38
## Getting Started
49

5-
First, run the development server:
10+
### Dataset
11+
This application uses images from [The Unsplash Dataset](https://github.com/unsplash/datasets), which you can download [here](https://unsplash.com/data/lite/latest). All you need for this demo is the `photos.tsv000` TSV file, which contains the metadata for all the images.
612

7-
```bash
8-
npm run dev
9-
# or
10-
yarn dev
11-
# or
12-
pnpm dev
13-
```
1413

15-
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
14+
### Connecting to Supabase
15+
16+
After creating a new [Supabase](https://supabase.com/) project, you'll need to:
17+
18+
1. Create an `images` table and import the data from `photos.tsv000`.
1619

17-
You can start editing the page by modifying `app/page.js`. The page auto-updates as you edit the file.
20+
2. Add a column for `image_embeddings`:
1821

19-
This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.
22+
```sql
23+
-- Add a new vector column with a dimension of 512
24+
alter table images add column image_embedding vector(512);
25+
```
2026

21-
## Learn More
27+
3. Add your `SUPABASE_URL`, `SUPABASE_ANON_KEY`, and `SUPABASE_SECRET_KEY` keys to a `.env.local` file (see `.env.local.example` for template).
2228

23-
To learn more about Next.js, take a look at the following resources:
29+
4. Update the image embeddings in your database by running the following command:
30+
```bash
31+
SUPABASE_URL=your-project-url \
32+
SUPABASE_SECRET_KEY=your-secret-key \
33+
node scripts/update-database.mjs
34+
```
2435

25-
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
26-
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
36+
*Note:* This will take a while. Also, since queries are capped at 1000 returned rows, you'll need to run this command multiple times to insert all 25000 rows.
2737
28-
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
38+
5. Create a new `match_images` [database function](https://supabase.com/docs/guides/database/functions):
2939
30-
## Deploy on Vercel
40+
```sql
41+
-- https://supabase.com/blog/openai-embeddings-postgres-vector
42+
create or replace function match_images (
43+
query_embedding vector(512),
44+
match_threshold float,
45+
match_count int
46+
)
47+
returns table (
48+
photo_id text,
49+
photo_url text,
50+
photo_image_url text,
51+
photo_width int,
52+
photo_height int,
53+
photo_aspect_ratio float,
54+
photo_description text,
55+
ai_description text,
56+
blur_hash text,
57+
similarity float
58+
)
59+
language sql stable
60+
as $$
61+
select
62+
photo_id,
63+
photo_url,
64+
photo_image_url,
65+
photo_width,
66+
photo_height,
67+
photo_aspect_ratio,
68+
photo_description,
69+
ai_description,
70+
blur_hash,
71+
1 - (image_embedding <=> query_embedding) as similarity
72+
from images
73+
where 1 - (image_embedding <=> query_embedding) > match_threshold
74+
order by similarity desc
75+
limit match_count;
76+
$$;
77+
```
3178
32-
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
79+
5. Add a [database policy](https://supabase.com/docs/guides/auth/row-level-security#policies) to allow users to view the database:
3380
34-
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
81+
```sql
82+
create policy "policy_name"
83+
on public.images
84+
for select using (
85+
true
86+
);
87+
```
88+
89+
### Development
90+
91+
You can now run the development server with:
92+
93+
```bash
94+
npm run dev
95+
```
96+
97+
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

0 commit comments

Comments
 (0)