-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into DST-304-metadataFix
- Loading branch information
Showing
15 changed files
with
187 additions
and
41 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
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
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import logging | ||
import sys | ||
|
||
import src.adapters.db as db | ||
from src.app_config import app_config | ||
from src.util.file_util import get_files | ||
from src.util.ingest_utils import process_and_ingest_sys_args | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
# Print INFO messages since this is often run from the terminal | ||
# during local development | ||
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") | ||
|
||
|
||
def _ingest_policy_pdfs( | ||
db_session: db.Session, | ||
pdf_file_dir: str, | ||
doc_attribs: dict[str, str], | ||
) -> None: | ||
file_list = get_files(pdf_file_dir) | ||
embedding_model = app_config.sentence_transformer | ||
for file in file_list: | ||
if file.endswith(".pdf"): | ||
logger.info( | ||
f"Processing pdf file: {file} at {pdf_file_dir} using {embedding_model}, {db_session}, with {doc_attribs}" | ||
) | ||
|
||
|
||
def main() -> None: | ||
if len(sys.argv) < 5: | ||
logger.warning( | ||
"Expecting 4 arguments: DATASET_ID BENEFIT_PROGRAM BENEFIT_REGION FILEPATH\n but got: %s", | ||
sys.argv[1:], | ||
) | ||
return | ||
|
||
process_and_ingest_sys_args(sys, logger, _ingest_policy_pdfs) |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from sentence_transformers import SentenceTransformer, util | ||
|
||
|
||
def test_sentence_transformer(embedding_model: str) -> None: | ||
""" | ||
Exercises specified embedding model and calculates scores from the embedding vectors. | ||
The embedding models will be downloaded automatically to ~/.cache/huggingface/hub, if it does not already exist. | ||
Used the scores to confirm/compare against those of pgvector's max_inner_product. | ||
""" | ||
transformer = SentenceTransformer(embedding_model) | ||
# transformer.save(f"sentence_transformers/{embedding_model}") | ||
text = "Curiosity inspires creative, innovative communities worldwide." | ||
embedding = transformer.encode(text) | ||
print("=== ", embedding_model, len(embedding)) | ||
|
||
for query in [ | ||
text, | ||
"How does curiosity inspire communities?", | ||
"What's the best pet?", | ||
"What's the meaning of life?", | ||
]: | ||
query_embedding = transformer.encode(query) | ||
# Code adapted from https://huggingface.co/sentence-transformers/multi-qa-mpnet-base-cos-v1 | ||
score = util.dot_score(embedding, query_embedding) | ||
print("Score:", score.item(), "for:", query) | ||
|
||
|
||
# To run: python -m src.util.embedding_models | ||
if __name__ == "__main__": | ||
embedding_models = ["multi-qa-mpnet-base-cos-v1", "multi-qa-mpnet-base-dot-v1"] | ||
for model in embedding_models: | ||
print(model) | ||
test_sentence_transformer(model) |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import getopt | ||
from logging import Logger | ||
from types import ModuleType | ||
from typing import Callable | ||
|
||
from src.app_config import app_config | ||
|
||
|
||
def process_and_ingest_sys_args(sys: ModuleType, logger: Logger, ingestion_call: Callable) -> None: | ||
"""Method that reads sys args and passes them into ingestion call""" | ||
|
||
opts, args = getopt.getopt( | ||
sys.argv[1:], shortopts="", longopts=["DATASET_ID BENEFIT_PROGRAM BENEFIT_REGION FILEPATH)"] | ||
) | ||
|
||
dataset_id = args[0] | ||
benefit_program = args[1] | ||
benefit_region = args[2] | ||
pdf_file_dir = args[3] | ||
|
||
logger.info( | ||
f"Processing files {dataset_id} at {pdf_file_dir} for {benefit_program} in {benefit_region}" | ||
) | ||
|
||
doc_attribs = { | ||
"dataset": dataset_id, | ||
"program": benefit_program, | ||
"region": benefit_region, | ||
} | ||
|
||
with app_config.db_session() as db_session: | ||
ingestion_call(db_session, pdf_file_dir, doc_attribs) | ||
db_session.commit() | ||
|
||
logger.info("Finished processing") |
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import io | ||
import logging | ||
import tempfile | ||
|
||
import pytest | ||
from sqlalchemy import delete | ||
|
||
from src.db.models.document import Document | ||
from src.ingest_policy_pdfs import _ingest_policy_pdfs | ||
|
||
|
||
@pytest.fixture | ||
def policy_local_file(): | ||
with tempfile.TemporaryDirectory() as tmpdirname: | ||
tempfile.NamedTemporaryFile(prefix="policy", suffix=".pdf", dir=tmpdirname, delete=False) | ||
yield tmpdirname | ||
|
||
|
||
@pytest.fixture | ||
def policy_s3_file(mock_s3_bucket_resource): | ||
mock_s3_bucket_resource.put_object( | ||
Body=io.BytesIO(b"%PDF-1.4\n%Fake PDF content for testing\n"), Key="policy.pdf" | ||
) | ||
return "s3://test_bucket/policy.pdf" | ||
|
||
|
||
doc_attribs = { | ||
"dataset": "test_dataset", | ||
"program": "test_benefit_program", | ||
"region": "Michigan", | ||
} | ||
|
||
|
||
@pytest.mark.parametrize("file_location", ["local", "s3"]) | ||
def test__ingest_policy_pdfs( | ||
caplog, app_config, db_session, policy_s3_file, policy_local_file, file_location | ||
): | ||
db_session.execute(delete(Document)) | ||
|
||
with caplog.at_level(logging.INFO): | ||
if file_location == "local": | ||
_ingest_policy_pdfs(db_session, policy_local_file, doc_attribs) | ||
else: | ||
_ingest_policy_pdfs(db_session, policy_s3_file, doc_attribs) | ||
|
||
assert any(text.startswith("Processing pdf file:") for text in caplog.messages) |
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