generated from mozilla-ai/Blueprint-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Add preprocessing module. #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -162,3 +162,5 @@ cython_debug/ | |
|
||
.idea/ | ||
.vscode/ | ||
|
||
example_outputs |
This file contains hidden or 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 hidden or 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 hidden or 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,7 +1,31 @@ | ||
from io import BytesIO | ||
from pathlib import Path | ||
|
||
import streamlit as st | ||
from docling_core.types.io import DocumentStream | ||
from structured_qa.preprocessing import document_to_sections_dir | ||
|
||
st.title("Structured Q&A") | ||
|
||
st.header("Uploading Data") | ||
|
||
uploaded_file = st.file_uploader( | ||
"Choose a file", type=["pdf", "html", "txt", "docx", "md"] | ||
) | ||
|
||
from blueprint.hello import hello | ||
if uploaded_file is not None: | ||
st.divider() | ||
st.header("Loading and converting to sections") | ||
st.markdown("[Docs for this Step]()") | ||
st.divider() | ||
|
||
st.title("Blueprint Demo") | ||
with st.spinner("Converting to sections..."): | ||
document_to_sections_dir( | ||
DocumentStream( | ||
name=uploaded_file.name, stream=BytesIO(uploaded_file.read()) | ||
), | ||
"output", | ||
) | ||
|
||
st.write(hello()) | ||
sections = [f.stem for f in Path("output").iterdir()] | ||
st.json(sections) |
This file contains hidden or 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,3 +1,3 @@ | ||
# API Reference | ||
|
||
"::: blueprint.hello" | ||
::: structured_qa.preprocessing |
This file contains hidden or 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,26 @@ | ||
# Command Line Interface | ||
|
||
Once you have [installed the blueprint](./getting-started.md), you can use it from the CLI. | ||
|
||
You can either provide the path to a configuration file: | ||
|
||
```bash | ||
structured-qa --from_config "example_data/config.yaml" | ||
``` | ||
|
||
Or provide values to the arguments directly: | ||
|
||
|
||
```bash | ||
structured-aq \ | ||
--input_file "example_data/EU_AI_ACT_CHAPTER_V.pdf" \ | ||
--output_folder "example_outputs/EU_AI_ACT_CHAPTER_V" | ||
``` | ||
|
||
--- | ||
|
||
::: structured_qa.cli.structured_qa | ||
|
||
--- | ||
|
||
::: structured_qa.config.Config |
This file contains hidden or 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 hidden or 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
Binary file not shown.
This file contains hidden or 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,2 @@ | ||
input_file: example_data/EU_AI_ACT_CHAPTER_V.pdf | ||
output_dir: example_outputs/EU_AI_ACT_CHAPTER_V |
This file contains hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains hidden or 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,47 @@ | ||
from pathlib import Path | ||
|
||
from fire import Fire | ||
from loguru import logger | ||
|
||
import yaml | ||
|
||
from structured_qa.config import Config | ||
from structured_qa.preprocessing import document_to_sections_dir | ||
|
||
|
||
@logger.catch(reraise=True) | ||
def structured_qa( | ||
input_file: str | None = None, | ||
output_dir: str | None = None, | ||
from_config: str | None = None, | ||
): | ||
""" | ||
|
||
Args: | ||
input_file: Path to the input document. | ||
output_dir: Path to the output directory. | ||
Structure of the output directory: | ||
|
||
``` | ||
output_dir/ | ||
section_1.txt | ||
section_2.txt | ||
... | ||
``` | ||
from_config: The path to the config file. | ||
|
||
If provided, all other arguments will be ignored. | ||
""" | ||
if from_config: | ||
config = Config.model_validate(yaml.safe_load(Path(from_config).read_text())) | ||
else: | ||
Path(output_dir).mkdir(exist_ok=True, parents=True) | ||
config = Config(input_file=input_file, output_dir=output_dir) | ||
|
||
logger.info("Loading and converting to sections") | ||
document_to_sections_dir(config.input_file, config.output_dir) | ||
logger.success("Done") | ||
|
||
|
||
def main(): | ||
Fire(structured_qa) |
This file contains hidden or 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,6 @@ | ||
from pydantic import BaseModel, DirectoryPath, FilePath | ||
|
||
|
||
class Config(BaseModel): | ||
input_file: FilePath | ||
output_dir: DirectoryPath |
This file contains hidden or 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,56 @@ | ||
from pathlib import Path | ||
|
||
import pymupdf4llm | ||
from langchain_text_splitters import MarkdownHeaderTextSplitter | ||
|
||
from loguru import logger | ||
|
||
|
||
@logger.catch(reraise=True) | ||
def document_to_sections_dir(input_file: str, output_dir: str) -> list[str]: | ||
""" | ||
Convert a document to a directory of sections. | ||
|
||
Uses [pymupdf4llm](https://ds4sd.github.io/docling/) to convert input_file to markdown. | ||
Then uses [langchain_text_splitters] to split the markdown into sections based on the headers. | ||
|
||
Args: | ||
input_file: Path to the input document. | ||
output_dir: Path to the output directory. | ||
Structure of the output directory: | ||
|
||
``` | ||
output_dir/ | ||
section_1.txt | ||
section_2.txt | ||
... | ||
``` | ||
|
||
Returns: | ||
List of section names. | ||
""" | ||
|
||
logger.info(f"Converting {input_file}") | ||
md_text = pymupdf4llm.to_markdown("example_data/1706.03762v7.pdf") | ||
logger.success("Converted") | ||
|
||
logger.info("Extracting sections") | ||
splitter = MarkdownHeaderTextSplitter( | ||
headers_to_split_on=[("#", "Header 1"), ("##", "Header 2"), ("###", "Header 3")] | ||
) | ||
sections = splitter.split_text(md_text) | ||
logger.success(f"Found {len(sections)} sections") | ||
|
||
logger.info(f"Writing sections to {output_dir}") | ||
output_dir = Path(output_dir) | ||
output_dir.mkdir(exist_ok=True, parents=True) | ||
section_names = [] | ||
for section in sections: | ||
section_name = list(section.metadata.values())[-1] | ||
section_names.append(section_name) | ||
(output_dir / f"{section_name.replace('/', '_')}.txt").write_text( | ||
section.page_content | ||
) | ||
logger.success("Done") | ||
|
||
return section_names |
This file contains hidden or 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,8 @@ | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def example_data(): | ||
return Path(__file__).parent.parent / "example_data" |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,9 @@ | ||
from structured_qa.preprocessing import document_to_sections_dir | ||
|
||
|
||
def test_document_to_sections_dir(tmp_path, example_data): | ||
output_dir = tmp_path / "output" | ||
document_to_sections_dir(example_data / "1706.03762v7.pdf", output_dir) | ||
sections = list(output_dir.iterdir()) | ||
assert all(section.is_file() and section.suffix == ".txt" for section in sections) | ||
assert len(sections) == 10 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.