Created
November 12, 2023 07:51
-
-
Save lookoutking/e360ad23d3db7ce97409d8f5169313eb to your computer and use it in GitHub Desktop.
langflow custom SRT loader with CustomComponent
This file contains hidden or 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
| from langflow import CustomComponent | |
| from langchain.schema import Document | |
| class PlainSRTLoader(CustomComponent): | |
| display_name: str = "Plain SRT Loader" | |
| description: str = "Read .srt file as plain text" | |
| def build_config(self): | |
| return { | |
| 'uploaded_file': { | |
| "suffixes": ['.srt'], | |
| 'field_type': 'file', | |
| 'file_types': ['srt'], | |
| 'required': True | |
| } | |
| } | |
| def build(self, uploaded_file: bytes) -> Document: | |
| if not isinstance(uploaded_file, str): | |
| raise TypeError("uploaded_file should be the path to the .srt file in string format") | |
| result = "" | |
| try: | |
| with open(uploaded_file, 'r') as file: | |
| result = file.read() | |
| return Document(page_content=file_contents) | |
| except IOError as e: | |
| result = f"An I/O error occurred: {e}" | |
| raise | |
| except Exception as e: | |
| result = f"An unexpected error occurred: {e}" | |
| raise | |
| self.repr_value = result | |
| return Document(page_content=result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment