-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable HTML uploads from UI (#422)
resolves #418
- Loading branch information
1 parent
cba6615
commit ce9233c
Showing
2 changed files
with
45 additions
and
0 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,42 @@ | ||
import os, re | ||
from slugify import slugify | ||
from langchain.document_loaders import BSHTMLLoader | ||
from ..utils import guid, file_creation_time, write_to_server_documents, move_source | ||
from ...utils import tokenize | ||
|
||
# Process all html-related documents. | ||
def as_html(**kwargs): | ||
parent_dir = kwargs.get('directory', 'hotdir') | ||
filename = kwargs.get('filename') | ||
ext = kwargs.get('ext', '.html') | ||
remove = kwargs.get('remove_on_complete', False) | ||
fullpath = f"{parent_dir}/{filename}{ext}" | ||
|
||
loader = BSHTMLLoader(fullpath) | ||
document = loader.load()[0] | ||
content = re.sub(r"\n+", "\n", document.page_content) | ||
|
||
if len(content) == 0: | ||
print(f"Resulting text content was empty for {filename}{ext}.") | ||
return(False, f"No text content found in {filename}{ext}") | ||
|
||
print(f"-- Working {fullpath} --") | ||
data = { | ||
'id': guid(), | ||
'url': "file://"+os.path.abspath(f"{parent_dir}/processed/{filename}{ext}"), | ||
'title': document.metadata.get('title', f"{filename}{ext}"), | ||
'docAuthor': 'Unknown', # TODO: Find a better author | ||
'description': 'Unknown', # TODO: Find a better description | ||
'docSource': 'an HTML file uploaded by the user.', | ||
'chunkSource': f"{filename}{ext}", | ||
'published': file_creation_time(fullpath), | ||
'wordCount': len(content), | ||
'pageContent': content, | ||
'token_count_estimate': len(tokenize(content)) | ||
} | ||
|
||
write_to_server_documents(data, f"{slugify(filename)}-{data.get('id')}") | ||
move_source(parent_dir, f"{filename}{ext}", remove=remove) | ||
|
||
print(f"[SUCCESS]: {filename}{ext} converted & ready for embedding.\n") | ||
return(True, 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