-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from scripturecentralqa/knowhys_loader
added knowhy loader notebook
- Loading branch information
Showing
11 changed files
with
1,423 additions
and
11 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 |
---|---|---|
|
@@ -16,4 +16,5 @@ __pycache__/ | |
/notebooks/wandb/ | ||
.idea/ | ||
.venv/ | ||
.vscode/ | ||
*.log |
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,6 @@ | ||
{ | ||
"[python]": { | ||
"editor.defaultFormatter": "ms-python.black-formatter" | ||
}, | ||
"python.formatting.provider": "none" | ||
} |
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,74 @@ | ||
"""Load conference talks.""" | ||
|
||
import json | ||
import os | ||
from typing import Any | ||
from typing import Iterator | ||
from typing import cast | ||
|
||
from bs4 import BeautifulSoup # type: ignore | ||
from langchain.document_loaders.base import BaseLoader | ||
from langchain.schema.document import Document | ||
from markdownify import MarkdownConverter # type: ignore | ||
from tqdm import tqdm | ||
|
||
from models.load_utils import clean | ||
|
||
|
||
# Create shorthand method for custom conversion | ||
def _to_markdown(html: str, **options: Any) -> str: | ||
"""Convert html to markdown.""" | ||
return cast(str, MarkdownConverter(**options).convert(html)) | ||
|
||
|
||
def load_knowhy(url: str, html: str, bs_parser: str = "html.parser") -> Document: | ||
"""Load a conference talk from a url and html.""" | ||
soup = BeautifulSoup(html, bs_parser) | ||
title = soup.find("h1", class_="page-title").text | ||
author = soup.find("div", class_="field-nam-author").text.replace("Post contributed by", "") | ||
date = soup.find("div", class_="field-name-publish-date").text | ||
citation = soup.find(id="block-views-knowhy-citation-block") | ||
body = soup.find("div", class_="group-left") | ||
content = clean(_to_markdown(str(body), base_url=url)) if body else "" | ||
|
||
metadata = { | ||
"url": url, | ||
"title": clean(title) if title else "", | ||
"author": clean(author) if author else "", | ||
"date": clean(date) if date else "", | ||
"citation": clean(_to_markdown(str(citation), base_url=url)) if citation else "", | ||
} | ||
return Document(page_content=content, metadata=metadata) | ||
|
||
|
||
class KnowhyLoader(BaseLoader): | ||
"""Loader for General Conference Talks.""" | ||
|
||
def lazy_load(self) -> Iterator[Document]: | ||
"""A lazy loader for Documents.""" | ||
raise NotImplementedError(f"{self.__class__.__name__} does not implement lazy_load()") | ||
|
||
def __init__(self, path: str = "", bs_parser: str = "html.parser"): | ||
"""Initialize loader.""" | ||
super().__init__() | ||
self.path = path | ||
self.bs_parser = bs_parser | ||
|
||
def load(self, verbose: bool = False) -> list[Document]: | ||
"""Load documents from path.""" | ||
docs = [] | ||
for filename in tqdm(os.listdir(self.path), disable=not verbose): | ||
path = os.path.join(self.path, filename) | ||
with open(path, encoding="utf8") as f: | ||
data = json.load(f) | ||
print(data) | ||
doc = load_knowhy(data["url"], data["html"], bs_parser=self.bs_parser) | ||
if not doc.metadata["title"] or not doc.page_content: | ||
if verbose: | ||
print("Missing title or content - skipping", filename) | ||
continue | ||
if not doc.metadata["author"]: | ||
if verbose: | ||
print("Missing author", filename) | ||
docs.append(doc) | ||
return docs |
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,98 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "936d02dd", | ||
"metadata": {}, | ||
"source": [ | ||
"# Load Talks\n", | ||
"\n", | ||
"Convert talk content from raw HTML to markdown format and extract key information. Write talks in JSONL format." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "7fe5bf12", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%load_ext autoreload\n", | ||
"%autoreload 2" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "4bca89a2", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from datetime import datetime\n", | ||
"\n", | ||
"import os\n", | ||
"\n", | ||
"from models.load_know import KnowhyLoader\n", | ||
"from models.load_utils import save_docs_to_jsonl" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "e753397e", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# config\n", | ||
"input_dir = '../data/load/raw/knowhys/'\n", | ||
"output_dir = '../data/load/output/knowhys/'\n", | ||
"\n", | ||
"today = datetime.today().strftime('%Y-%m-%d')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "2f5ebfa4", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"loader = KnowhyLoader(input_dir)\n", | ||
"docs = loader.load(verbose=True)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "7949104b-e0e2-42eb-aa9f-6140722a1d1b", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"output_filename = os.path.join(output_dir, f\"{today}.jsonl\")\n", | ||
"\n", | ||
"save_docs_to_jsonl(docs, output_filename)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "models", | ||
"language": "python", | ||
"name": "models" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.0" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
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
Oops, something went wrong.