-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample-psycopg.py
59 lines (46 loc) · 1.71 KB
/
example-psycopg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import psycopg
from sentence_transformers import SentenceTransformer
from PIL import Image
from matplotlib import pyplot as plt
from matplotlib import image as mpimg
from pgvector.psycopg import register_vector
import os, sys
conn = psycopg.connect(dbname="postgres", autocommit=True)
model = SentenceTransformer('clip-ViT-B-32')
def seed():
print("seeding the database")
conn.execute('CREATE EXTENSION IF NOT EXISTS vector')
register_vector(conn)
conn.execute('DROP TABLE IF EXISTS items')
conn.execute('CREATE TABLE items (id bigserial PRIMARY KEY, path varchar(64), embedding vector(512))')
cur = conn.cursor()
cur.execute('create extension if not exists vector with schema public')
images = os.listdir("./images")
for f in images:
file = f'./images/{f}'
img_emb = model.encode(Image.open(file))
cur.execute('INSERT INTO items (embedding, path) VALUES (%s,%s)', (img_emb.tolist(), file))
def search():
# query_string = "a white bike in front of a red brick wall"
query_string = input("Enter image query:")
text_emb = model.encode(query_string)
cur = conn.cursor()
cur.execute("""
SELECT id, path, embedding <-> %s AS distance
FROM items ORDER BY embedding::vector(512) <-> %s
""",
(str(text_emb.tolist()),str(text_emb.tolist())))
rows = cur.fetchall()
print(rows)
show(rows[0][1], rows[0][2])
show(rows[1][1], rows[0][2])
def show(path, distance):
plt.title(f'{path} {distance}')
image = mpimg.imread(path)
plt.imshow(image)
plt.show()
if __name__ == '__main__':
args = sys.argv
if len(args) > 1 and args[1] == '-s':
seed()
search()