-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
80f4fbe
commit 6001589
Showing
5 changed files
with
93 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
""" | ||
To install the required dependencies: | ||
pip install datachain[examples] | ||
""" | ||
|
||
from collections.abc import Iterator | ||
|
||
from unstructured.cleaners.core import ( | ||
clean, | ||
group_broken_paragraphs, | ||
replace_unicode_quotes, | ||
) | ||
from unstructured.embed.huggingface import ( | ||
HuggingFaceEmbeddingConfig, | ||
HuggingFaceEmbeddingEncoder, | ||
) | ||
from unstructured.partition.pdf import partition_pdf | ||
|
||
from datachain import C, DataChain, DataModel, File | ||
|
||
source = "gs://datachain-demo/neurips/1987/" | ||
|
||
|
||
# Define the output as a DataModel class | ||
class Chunk(DataModel): | ||
key: str | ||
text: str | ||
embeddings: list[float] | ||
|
||
|
||
# Define embedding encoder | ||
|
||
embedding_encoder = HuggingFaceEmbeddingEncoder(config=HuggingFaceEmbeddingConfig()) | ||
|
||
|
||
# Use signatures to define UDF input/output | ||
# these can be pydantic model or regular Python types | ||
def process_pdf(file: File) -> Iterator[Chunk]: | ||
# Ingest the file | ||
with file.open() as f: | ||
chunks = partition_pdf(file=f, chunking_strategy="by_title", strategy="fast") | ||
|
||
# Clean the chunks and add new columns | ||
for chunk in chunks: | ||
chunk.apply( | ||
lambda text: clean( | ||
text, bullets=True, extra_whitespace=True, trailing_punctuation=True | ||
) | ||
) | ||
chunk.apply(replace_unicode_quotes) | ||
chunk.apply(group_broken_paragraphs) | ||
|
||
# create embeddings | ||
chunks_embedded = embedding_encoder.embed_documents(chunks) | ||
|
||
# Add new rows to DataChain | ||
for chunk in chunks_embedded: | ||
yield Chunk( | ||
key=file.path, | ||
text=chunk.text, | ||
embeddings=chunk.embeddings, | ||
) | ||
|
||
|
||
dc = ( | ||
DataChain.from_storage(source) | ||
.settings(parallel=-1) | ||
.filter(C.file.path.glob("*.pdf")) | ||
.gen(document=process_pdf) | ||
) | ||
|
||
dc.save("embedded-documents") | ||
|
||
DataChain.from_dataset("embedded-documents").show() |
10 changes: 7 additions & 3 deletions
10
examples/llm_and_nlp/unstructured-text.py → ...s/llm_and_nlp/unstructured-summary-map.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
# pip install Pillow | ||
|
||
import base64 | ||
import os | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters