-
Notifications
You must be signed in to change notification settings - Fork 7.3k
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
Update ingest_service.py to fix issue Error: 'utf-8' codec can't decode #1171
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import tempfile | ||
import chardet # Chardet must be put in requirements or manually install with pip install chardet | ||
from pathlib import Path | ||
from typing import TYPE_CHECKING, Any, AnyStr | ||
|
||
|
@@ -77,7 +78,10 @@ def ingest(self, file_name: str, file_data: AnyStr | Path) -> list[IngestedDoc]: | |
# Read as a plain text | ||
string_reader = StringIterableReader() | ||
if isinstance(file_data, Path): | ||
text = file_data.read_text() | ||
with open(file_data, 'rb') as f2: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try to give this variables proper names. Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would go even further, and could you also try to avoid to read the file in it's entirety to detect the charset? More information in While the existing implementation work, it is suboptimal in the sense that it reads the file in its entirety before re-reading it? One could also do the following: read the file in binary mode, store it in This is doing only a single read, and it re-uses the buffer in memory instead of re-reading it. |
||
result2 = chardet.detect(f2.read()) | ||
text = file_data.read_text(encoding=result2['encoding']) | ||
#text = file_data.read_text() | ||
documents = string_reader.load_data([text]) | ||
elif isinstance(file_data, bytes): | ||
documents = string_reader.load_data([file_data.decode("utf-8")]) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should add the dependency,
poetry add chardet