Skip to content

Instantly share code, notes, and snippets.

@lookoutking
Created November 12, 2023 07:51
Show Gist options
  • Select an option

  • Save lookoutking/e360ad23d3db7ce97409d8f5169313eb to your computer and use it in GitHub Desktop.

Select an option

Save lookoutking/e360ad23d3db7ce97409d8f5169313eb to your computer and use it in GitHub Desktop.
langflow custom SRT loader with CustomComponent
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