Spaces:
Running
Running
File size: 5,414 Bytes
7b7cab6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
from langchain_community.document_loaders import (CSVLoader, WikipediaLoader, UnstructuredURLLoader,
YoutubeLoader, PyPDFLoader, BSHTMLLoader,
Docx2txtLoader, UnstructuredMarkdownLoader)
from langchain_unstructured import UnstructuredLoader
class DocumentLoader:
def load_unstructured(self, path):
"""
Load data from a file at the specified path:
supported files:
"csv", "doc", "docx", "epub", "image", "md", "msg", "odt", "org", "pdf", "ppt", "pptx", "rtf", "rst", "tsv", "xlsx"
Args:
path (str): The file paths
Returns:
The loaded data.
Exceptions:
Prints an error message if the loading fails.
"""
try:
loader = UnstructuredLoader(path)
data = loader.load()
return data
except Exception as e:
print(f"Error loading Unstructured: {e}")
def load_csv(self, path):
"""
Load data from a CSV file at the specified path.
Args:
path (str): The file path to the CSV file.
Returns:
The loaded CSV data.
Exceptions:
Prints an error message if the CSV loading fails.
"""
try:
loader = CSVLoader(file_path=path)
data = loader.load()
return data
except Exception as e:
print(f"Error loading CSV: {e}")
def wikipedia_query(self, search_query):
"""
Query Wikipedia using a given search term and return the results.
Args:
search_query (str): The search term to query on Wikipedia.
Returns:
The query results.
Exceptions:
Prints an error message if the Wikipedia query fails.
"""
try:
data = WikipediaLoader(query=search_query, load_max_docs=2).load()
return data
except Exception as e:
print(f"Error querying Wikipedia: {e}")
def load_urls(self, urls):
"""
Load and parse content from a list of URLs.
Args:
urls (list): A list of URLs to load.
Returns:
The loaded data from the URLs.
Exceptions:
Prints an error message if loading URLs fails.
"""
try:
loader = UnstructuredURLLoader(urls=urls)
data = loader.load()
return data
except Exception as e:
print(f"Error loading URLs: {e}")
def load_YouTubeVideo(self, urls):
"""
Load YouTube video information from provided URLs.
Args:
urls (list): A list of YouTube video URLs.
Returns:
The loaded documents from the YouTube URLs.
Exceptions:
Prints an error message if loading YouTube videos fails.
"""
try:
loader = YoutubeLoader.from_youtube_url(
urls, add_video_info=True, language=["en", "pt", "zh-Hans", "es", "ur", "hi"],
translation="en")
documents = loader.load()
return documents
except Exception as e:
print(f"Error loading YouTube video: {e}")
def load_pdf(self, path):
"""
Load data from a PDF file at the specified path.
Args:
path (str): The file path to the PDF file.
Returns:
The loaded and split PDF pages.
Exceptions:
Prints an error message if the PDF loading fails.
"""
try:
loader = PyPDFLoader(path)
pages = loader.load_and_split()
return pages
except Exception as e:
print(f"Error loading PDF: {e}")
def load_text_from_html(self, path):
"""
Load and parse text content from an HTML file at the specified path.
Args:
path (str): The file path to the HTML file.
Returns:
The loaded HTML data.
Exceptions:
Prints an error message if loading text from HTML fails.
"""
try:
loader = BSHTMLLoader(path)
data = loader.load()
return data
except Exception as e:
print(f"Error loading text from HTML: {e}")
def load_markdown(self, path):
"""
Load data from a Markdown file at the specified path.
Args:
path (str): The file path to the Markdown file.
Returns:
The loaded Markdown data.
Exceptions:
Prints an error message if loading Markdown fails.
"""
try:
loader = UnstructuredMarkdownLoader(path)
data = loader.load()
return data
except Exception as e:
print(f"Error loading Markdown: {e}")
def load_doc(self, path):
"""
Load data from a DOCX file at the specified path.
Args:
path (str): The file path to the DOCX file.
Returns:
The loaded DOCX data.
Exceptions:
Prints an error message if loading DOCX fails.
"""
try:
loader = Docx2txtLoader(path)
data = loader.load()
return data
except Exception as e:
print(f"Error loading DOCX: {e}")
|