litdb 2.1.2__tar.gz → 2.1.3__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.3
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,8 @@ 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
43
45
 
44
46
  #+title: litdb - a literature and document database
45
47
 
@@ -61,6 +63,7 @@ The idea is you add papers to your database, and then you can search it with nat
61
63
  5. https://youtube.com/live/j7rItPwWDaY litdb and Jupyter Lab
62
64
  6. https://youtube.com/live/SUtvtc7l6y0 litdb + GPT enhancements
63
65
  7. https://youtube.com/live/3FZ1ROnCC6Y litdb + LiteLLM and streamlit
66
+ 8. https://www.youtube.com/live/IKKTQSTXQmc litdb + Youtube and audio
64
67
 
65
68
  ** installation
66
69
 
@@ -18,6 +18,7 @@ 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
21
22
 
22
23
  ** installation
23
24
 
@@ -7,7 +7,7 @@ where = ["src"]
7
7
 
8
8
  [project]
9
9
  name = "litdb"
10
- version = "2.1.2"
10
+ version = "2.1.3"
11
11
  authors = [
12
12
  {name = "John Kitchin", email = "jkitchin@andrew.cmu.edu"},
13
13
  ]
@@ -61,7 +61,11 @@ 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"
65
69
  ]
66
70
 
67
71
  [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)
@@ -38,6 +38,7 @@ from .pdf import add_pdf
38
38
  from .bibtex import dump_bibtex
39
39
  from .youtube import get_youtube_doc
40
40
  from .audio import is_audio_url, get_audio_text, record
41
+ from .images import add_image, image_query
41
42
 
42
43
  with warnings.catch_warnings():
43
44
  warnings.filterwarnings("ignore", category=UserWarning, module="pydantic")
@@ -110,6 +111,7 @@ def add(
110
111
  These are one time additions.
111
112
 
112
113
  """
114
+ print(sources)
113
115
  for source in tqdm(sources):
114
116
  # a work
115
117
  if source.startswith("10.") or "doi.org" in source:
@@ -121,6 +123,13 @@ def add(
121
123
 
122
124
  add_work(source, references, citing, related)
123
125
 
126
+ elif (
127
+ source.endswith(".png")
128
+ or source.endswith(".jpg")
129
+ or source.endswith(".jpeg")
130
+ ):
131
+ add_image(source)
132
+
124
133
  # works from an author
125
134
  elif "orcid" in source or source.lower().startswith("https://openalex.org/a"):
126
135
  add_author(source)
@@ -550,7 +559,11 @@ def audio(playback=False):
550
559
  @click.option("-e", "--emacs", is_flag=True, default=False)
551
560
  @click.option("-i", "--iterative", is_flag=True, default=False)
552
561
  @click.option("-m", "--max-steps", default=None)
553
- @click.option("-f", "--fmt", default=" {{ i }}. ({{ score|round(3) }}) {{ text }}\n\n")
562
+ @click.option(
563
+ "-f",
564
+ "--fmt",
565
+ default=(" {{ i }}. ({{ score|round(3) }})" " {{ source }}\n{{ text[:200] }}\n\n"),
566
+ )
554
567
  @click.option("-x", "--cross-encode", is_flag=True, default=False)
555
568
  def vsearch(query, n, emacs, fmt, cross_encode, iterative, max_steps):
556
569
  """Do a vector search on QUERY.
@@ -697,6 +710,14 @@ def chat_command(model, debug):
697
710
  cli.add_command(chat_command, name="chat")
698
711
 
699
712
 
713
+ @cli.command()
714
+ @click.argument("query", nargs=-1)
715
+ @click.option("-c", "--clipboard", is_flag=True, default=False)
716
+ @click.option("-n", default=1, help="Number of hits to retrieve")
717
+ def image_search(query, clipboard, n):
718
+ image_query(" ".join(query), clipboard, n)
719
+
720
+
700
721
  @cli.command()
701
722
  @click.argument("source")
702
723
  @click.option("-f", "--fmt", default=None)
@@ -1421,7 +1442,7 @@ def suggest_reviewers(query, n):
1421
1442
  lki = d.get("last_known_institutions", [])
1422
1443
  if lki == []:
1423
1444
  affils = d.get("affiliations", [])
1424
- if len(affils) >= 0:
1445
+ if len(affils) >= 1:
1425
1446
  lki = affils[0]["institution"]["display_name"]
1426
1447
  else:
1427
1448
  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,69 @@
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
+ import numpy as np
12
+ import datetime
13
+ import os
14
+ import pyperclip
15
+
16
+
17
+ model = SentenceTransformer("clip-ViT-B-32")
18
+
19
+ db = get_db()
20
+
21
+
22
+ def add_image(path):
23
+ """Embed and add the image in path to db."""
24
+ emb = model.encode(Image.open(path))
25
+
26
+ q = """insert or ignore into
27
+ images(source, embedding, date_added)
28
+ values (?, ?, ?)"""
29
+ db.execute(
30
+ q,
31
+ (
32
+ os.path.abspath(path),
33
+ emb.astype(np.float32).tobytes(),
34
+ datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
35
+ ),
36
+ )
37
+ db.commit()
38
+
39
+
40
+ def image_query(query=None, clipboard=False, n=1):
41
+ """Query the image database.
42
+
43
+ If query is not None, it is either a text description or path to image.
44
+ If clipboard is True, use an image or text from the clipboard
45
+ """
46
+ if query and os.path.exists(query):
47
+ emb = model.encode(Image.open(query))
48
+ elif query and not os.path.exists(query):
49
+ emb = model.encode(query)
50
+ elif clipboard:
51
+ clip = ImageGrab.grabclipboard()
52
+ if isinstance(clip, Image.Image):
53
+ emb = model.encode(clip)
54
+ else:
55
+ clip = pyperclip.paste()
56
+ print(clip)
57
+ emb = model.encode(clip)
58
+
59
+ emb = emb.astype(np.float32).tobytes()
60
+
61
+ q = """select source from vector_top_k('image_idx', ?, ?)
62
+ join images on images.rowid = id"""
63
+ results = db.execute(q, (emb, n)).fetchall()
64
+
65
+ for (row,) in results:
66
+ print(row)
67
+ Image.open(row).show()
68
+
69
+ 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.3
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,8 @@ 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
43
45
 
44
46
  #+title: litdb - a literature and document database
45
47
 
@@ -61,6 +63,7 @@ The idea is you add papers to your database, and then you can search it with nat
61
63
  5. https://youtube.com/live/j7rItPwWDaY litdb and Jupyter Lab
62
64
  6. https://youtube.com/live/SUtvtc7l6y0 litdb + GPT enhancements
63
65
  7. https://youtube.com/live/3FZ1ROnCC6Y litdb + LiteLLM and streamlit
66
+ 8. https://www.youtube.com/live/IKKTQSTXQmc litdb + Youtube and audio
64
67
 
65
68
  ** installation
66
69
 
@@ -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,5 @@ IPython
31
31
  yt_dlp
32
32
  webvtt-py
33
33
  streamlit
34
+ pyperclip
35
+ PIL
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