litdb 2.1.2__tar.gz → 2.1.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: litdb
3
- Version: 2.1.2
3
+ Version: 2.1.4
4
4
  Summary: A literature database tool with GPT integration.
5
5
  Author-email: John Kitchin <jkitchin@andrew.cmu.edu>
6
6
  Maintainer-email: John Kitchin <jkitchin@andrew.cmu.edu>
@@ -40,6 +40,9 @@ Requires-Dist: IPython
40
40
  Requires-Dist: yt_dlp
41
41
  Requires-Dist: webvtt-py
42
42
  Requires-Dist: streamlit
43
+ Requires-Dist: pyperclip
44
+ Requires-Dist: PIL
45
+ Requires-Dist: pillow_heif
43
46
 
44
47
  #+title: litdb - a literature and document database
45
48
 
@@ -61,6 +64,8 @@ The idea is you add papers to your database, and then you can search it with nat
61
64
  5. https://youtube.com/live/j7rItPwWDaY litdb and Jupyter Lab
62
65
  6. https://youtube.com/live/SUtvtc7l6y0 litdb + GPT enhancements
63
66
  7. https://youtube.com/live/3FZ1ROnCC6Y litdb + LiteLLM and streamlit
67
+ 8. https://www.youtube.com/live/IKKTQSTXQmc litdb + Youtube and audio
68
+ 9. https://youtube.com/live/MEf9rPI0Z1M litdb + Image search with text and image queries using CLIP
64
69
 
65
70
  ** installation
66
71
 
@@ -18,6 +18,8 @@ The idea is you add papers to your database, and then you can search it with nat
18
18
  5. https://youtube.com/live/j7rItPwWDaY litdb and Jupyter Lab
19
19
  6. https://youtube.com/live/SUtvtc7l6y0 litdb + GPT enhancements
20
20
  7. https://youtube.com/live/3FZ1ROnCC6Y litdb + LiteLLM and streamlit
21
+ 8. https://www.youtube.com/live/IKKTQSTXQmc litdb + Youtube and audio
22
+ 9. https://youtube.com/live/MEf9rPI0Z1M litdb + Image search with text and image queries using CLIP
21
23
 
22
24
  ** installation
23
25
 
@@ -7,7 +7,7 @@ where = ["src"]
7
7
 
8
8
  [project]
9
9
  name = "litdb"
10
- version = "2.1.2"
10
+ version = "2.1.4"
11
11
  authors = [
12
12
  {name = "John Kitchin", email = "jkitchin@andrew.cmu.edu"},
13
13
  ]
@@ -61,7 +61,12 @@ dependencies = [
61
61
  "webvtt-py",
62
62
 
63
63
  # the streamlit app
64
- "streamlit"
64
+ "streamlit",
65
+
66
+ # image clip search
67
+ "pyperclip",
68
+ "PIL",
69
+ "pillow_heif"
65
70
  ]
66
71
 
67
72
  [project.scripts]
@@ -344,14 +344,17 @@ The following subcommands can be used:
344
344
  n = 3
345
345
 
346
346
  # Now we do RAG on litdb if needed.
347
+ # I don't use the references below, so we do not assign them.
347
348
  if rag:
348
- rag_content, references = get_rag_content(prompt, n)
349
+ rag_content, _ = get_rag_content(prompt, n)
349
350
  else:
350
- rag_content, references = (None, None)
351
+ rag_content, _ = (None, None)
351
352
 
352
353
  if rag and rag_content:
353
- prompt = f"""Use the following retrieved information to respond to
354
- the user prompt. Reference the citations if you use that information.
354
+ prompt = f"""You are a professional scientist. Use the following
355
+ retrieved information to respond to the user prompt. Reference the
356
+ citations if you use that information. Each reference is surrounded by
357
+ <citation> tags. If the information is not relevent, do not use it.
355
358
 
356
359
  <retrieved information>
357
360
  {rag_content}
@@ -373,5 +376,5 @@ The following subcommands can be used:
373
376
 
374
377
  # We don't always have data here, if you use your own rag data
375
378
 
376
- if references:
377
- richprint("References:\n\n" + references)
379
+ # if references:
380
+ # richprint("References:\n\n" + references)
@@ -31,6 +31,7 @@ import tabulate
31
31
  from tqdm import tqdm
32
32
  import webbrowser
33
33
 
34
+
34
35
  from .utils import get_config, init_litdb
35
36
  from .db import get_db, add_source, add_work, add_author, update_filter, add_bibtex
36
37
  from .openalex import get_data, get_text
@@ -38,6 +39,7 @@ from .pdf import add_pdf
38
39
  from .bibtex import dump_bibtex
39
40
  from .youtube import get_youtube_doc
40
41
  from .audio import is_audio_url, get_audio_text, record
42
+ from .images import add_image, image_query, image_extensions
41
43
 
42
44
  with warnings.catch_warnings():
43
45
  warnings.filterwarnings("ignore", category=UserWarning, module="pydantic")
@@ -110,6 +112,7 @@ def add(
110
112
  These are one time additions.
111
113
 
112
114
  """
115
+
113
116
  for source in tqdm(sources):
114
117
  # a work
115
118
  if source.startswith("10.") or "doi.org" in source:
@@ -121,6 +124,9 @@ def add(
121
124
 
122
125
  add_work(source, references, citing, related)
123
126
 
127
+ elif os.path.splitext(source)[1].lower() in image_extensions:
128
+ add_image(source)
129
+
124
130
  # works from an author
125
131
  elif "orcid" in source or source.lower().startswith("https://openalex.org/a"):
126
132
  add_author(source)
@@ -550,7 +556,11 @@ def audio(playback=False):
550
556
  @click.option("-e", "--emacs", is_flag=True, default=False)
551
557
  @click.option("-i", "--iterative", is_flag=True, default=False)
552
558
  @click.option("-m", "--max-steps", default=None)
553
- @click.option("-f", "--fmt", default=" {{ i }}. ({{ score|round(3) }}) {{ text }}\n\n")
559
+ @click.option(
560
+ "-f",
561
+ "--fmt",
562
+ default=(" {{ i }}. ({{ score|round(3) }})" " {{ source }}\n{{ text[:200] }}\n\n"),
563
+ )
554
564
  @click.option("-x", "--cross-encode", is_flag=True, default=False)
555
565
  def vsearch(query, n, emacs, fmt, cross_encode, iterative, max_steps):
556
566
  """Do a vector search on QUERY.
@@ -697,6 +707,14 @@ def chat_command(model, debug):
697
707
  cli.add_command(chat_command, name="chat")
698
708
 
699
709
 
710
+ @cli.command()
711
+ @click.argument("query", nargs=-1)
712
+ @click.option("-c", "--clipboard", is_flag=True, default=False)
713
+ @click.option("-n", default=1, help="Number of hits to retrieve")
714
+ def image_search(query, clipboard, n):
715
+ image_query(" ".join(query), clipboard, n)
716
+
717
+
700
718
  @cli.command()
701
719
  @click.argument("source")
702
720
  @click.option("-f", "--fmt", default=None)
@@ -1421,7 +1439,7 @@ def suggest_reviewers(query, n):
1421
1439
  lki = d.get("last_known_institutions", [])
1422
1440
  if lki == []:
1423
1441
  affils = d.get("affiliations", [])
1424
- if len(affils) >= 0:
1442
+ if len(affils) >= 1:
1425
1443
  lki = affils[0]["institution"]["display_name"]
1426
1444
  else:
1427
1445
  lki = "unknown"
@@ -51,7 +51,8 @@ def get_db():
51
51
  )
52
52
 
53
53
  db.execute(
54
- """create virtual table if not exists fulltext using fts5(source, text)"""
54
+ """create virtual table if not exists
55
+ fulltext using fts5(source, text)"""
55
56
  )
56
57
 
57
58
  db.execute(
@@ -94,6 +95,24 @@ def get_db():
94
95
  prompt_history(rowid integer primary key,
95
96
  prompt text)"""
96
97
  )
98
+
99
+ # For images
100
+ model = SentenceTransformer("clip-ViT-B-32")
101
+ _, dim = model.encode(["test"]).shape
102
+
103
+ db.execute(
104
+ f"""create table if not exists
105
+ images(rowid integer primary key,
106
+ source text unique,
107
+ embedding F32_BLOB({dim}),
108
+ date_added text)"""
109
+ )
110
+
111
+ db.execute(
112
+ """create index if not exists image_idx
113
+ ON images (libsql_vector_idx(embedding))"""
114
+ )
115
+
97
116
  return db
98
117
 
99
118
 
@@ -0,0 +1,73 @@
1
+ """Add-on to index and search images by text.
2
+
3
+ This uses the clip transformer to get embedding vectors of images. Then you can
4
+ search the images either by textual descriptions, or by using similar images.
5
+
6
+ """
7
+
8
+ from .db import get_db
9
+ from sentence_transformers import SentenceTransformer
10
+ from PIL import Image, ImageGrab
11
+ from pillow_heif import register_heif_opener
12
+ import numpy as np
13
+ import datetime
14
+ import os
15
+ import pyperclip
16
+
17
+ register_heif_opener()
18
+
19
+ image_extensions = Image.registered_extensions().keys()
20
+
21
+ model = SentenceTransformer("clip-ViT-B-32")
22
+ db = get_db()
23
+
24
+
25
+ def add_image(path):
26
+ """Embed and add the image in path to db."""
27
+
28
+ emb = model.encode(Image.open(path))
29
+
30
+ q = """insert or ignore into
31
+ images(source, embedding, date_added)
32
+ values (?, ?, ?)"""
33
+ db.execute(
34
+ q,
35
+ (
36
+ os.path.abspath(path),
37
+ emb.astype(np.float32).tobytes(),
38
+ datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
39
+ ),
40
+ )
41
+ db.commit()
42
+
43
+
44
+ def image_query(query=None, clipboard=False, n=1):
45
+ """Query the image database.
46
+
47
+ If query is not None, it is either a text description or path to image.
48
+ If clipboard is True, use an image or text from the clipboard
49
+ """
50
+ if query and os.path.exists(query):
51
+ emb = model.encode(Image.open(query))
52
+ elif query and not os.path.exists(query):
53
+ emb = model.encode(query)
54
+ elif clipboard:
55
+ clip = ImageGrab.grabclipboard()
56
+ if isinstance(clip, Image.Image):
57
+ emb = model.encode(clip)
58
+ else:
59
+ clip = pyperclip.paste()
60
+ print(clip)
61
+ emb = model.encode(clip)
62
+
63
+ emb = emb.astype(np.float32).tobytes()
64
+
65
+ q = """select source from vector_top_k('image_idx', ?, ?)
66
+ join images on images.rowid = id"""
67
+ results = db.execute(q, (emb, n)).fetchall()
68
+
69
+ for (row,) in results:
70
+ print(row)
71
+ Image.open(row).show()
72
+
73
+ return results
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: litdb
3
- Version: 2.1.2
3
+ Version: 2.1.4
4
4
  Summary: A literature database tool with GPT integration.
5
5
  Author-email: John Kitchin <jkitchin@andrew.cmu.edu>
6
6
  Maintainer-email: John Kitchin <jkitchin@andrew.cmu.edu>
@@ -40,6 +40,9 @@ Requires-Dist: IPython
40
40
  Requires-Dist: yt_dlp
41
41
  Requires-Dist: webvtt-py
42
42
  Requires-Dist: streamlit
43
+ Requires-Dist: pyperclip
44
+ Requires-Dist: PIL
45
+ Requires-Dist: pillow_heif
43
46
 
44
47
  #+title: litdb - a literature and document database
45
48
 
@@ -61,6 +64,8 @@ The idea is you add papers to your database, and then you can search it with nat
61
64
  5. https://youtube.com/live/j7rItPwWDaY litdb and Jupyter Lab
62
65
  6. https://youtube.com/live/SUtvtc7l6y0 litdb + GPT enhancements
63
66
  7. https://youtube.com/live/3FZ1ROnCC6Y litdb + LiteLLM and streamlit
67
+ 8. https://www.youtube.com/live/IKKTQSTXQmc litdb + Youtube and audio
68
+ 9. https://youtube.com/live/MEf9rPI0Z1M litdb + Image search with text and image queries using CLIP
64
69
 
65
70
  ** installation
66
71
 
@@ -9,6 +9,7 @@ src/litdb/cli.py
9
9
  src/litdb/coa.py
10
10
  src/litdb/db.py
11
11
  src/litdb/gpt.py
12
+ src/litdb/images.py
12
13
  src/litdb/lab.py
13
14
  src/litdb/openalex.py
14
15
  src/litdb/pdf.py
@@ -31,3 +31,6 @@ IPython
31
31
  yt_dlp
32
32
  webvtt-py
33
33
  streamlit
34
+ pyperclip
35
+ PIL
36
+ pillow_heif
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes