refactor: Update FileComponent to handle missing file path

Update the FileComponent in File.py to handle the case where no file path is provided. If the path is empty, raise a ValueError with the message "Please, upload a file to use this component." This change improves the error handling and user experience of the component.
This commit is contained in:
ogabrielluiz 2024-06-13 01:00:07 -03:00
commit 1ac7555150

View file

@ -1,9 +1,10 @@
from pathlib import Path
from langflow.custom import Component
from langflow.inputs import FileInput, BoolInput
from langflow.template import Output
from langflow.schema import Data
from langflow.base.data.utils import TEXT_FILE_TYPES, parse_text_file_to_data
from langflow.custom import Component
from langflow.inputs import BoolInput, FileInput
from langflow.schema import Data
from langflow.template import Output
class FileComponent(Component):
@ -15,6 +16,7 @@ class FileComponent(Component):
FileInput(
name="path",
display_name="Path",
file_types=TEXT_FILE_TYPES,
info=f"Supported file types: {', '.join(TEXT_FILE_TYPES)}",
),
BoolInput(
@ -30,11 +32,12 @@ class FileComponent(Component):
]
def load_file(self) -> Data:
path = self.path
if not self.path:
raise ValueError("Please, upload a file to use this component.")
resolved_path = self.resolve_path(self.path)
silent_errors = self.silent_errors
resolved_path = Path(path).resolve()
extension = resolved_path.suffix[1:].lower()
extension = Path(resolved_path).suffix[1:].lower()
if extension == "doc":
raise ValueError("doc files are not supported. Please save as .docx")