From 6d6d1d73dbd79e2bb2ff628de0263e0d935d5c81 Mon Sep 17 00:00:00 2001 From: igorrCarvalho Date: Wed, 24 Apr 2024 22:37:20 -0300 Subject: [PATCH] Feat: add File input component --- .../langflow/components/inputs/FileInput.py | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/backend/base/langflow/components/inputs/FileInput.py diff --git a/src/backend/base/langflow/components/inputs/FileInput.py b/src/backend/base/langflow/components/inputs/FileInput.py new file mode 100644 index 000000000..0cd7c12b1 --- /dev/null +++ b/src/backend/base/langflow/components/inputs/FileInput.py @@ -0,0 +1,48 @@ +from pathlib import Path +from typing import Any, Dict + +from langflow.base.data.utils import TEXT_FILE_TYPES, parse_text_file_to_record +from langflow.interface.custom.custom_component import CustomComponent +from langflow.schema import Record + + +class FileInput(CustomComponent): + display_name = "File Input" + description = "A generic file input." + icon = "file-text" + + def build_config(self) -> Dict[str, Any]: + return { + "path": { + "display_name": "Path", + "field_type": "file", + "file_types": TEXT_FILE_TYPES, + "info": f"Supported file types: {', '.join(TEXT_FILE_TYPES)}", + }, + "silent_errors": { + "display_name": "Silent Errors", + "advanced": True, + "info": "If true, errors will not raise an exception.", + }, + } + + def load_file(self, path: str, silent_errors: bool = False) -> Record: + resolved_path = self.resolve_path(path) + path_obj = Path(resolved_path) + extension = path_obj.suffix[1:].lower() + if extension == "doc": + raise ValueError("doc files are not supported. Please save as .docx") + if extension not in TEXT_FILE_TYPES: + raise ValueError(f"Unsupported file type: {extension}") + record = parse_text_file_to_record(resolved_path, silent_errors) + self.status = record if record else "No data" + return record or Record() + + def build( + self, + path: str, + silent_errors: bool = False, + ) -> Record: + record = self.load_file(path, silent_errors) + self.status = record + return record