MCPcopy Create free account
hub / github.com/Paper2Poster/Paper2Poster / from_bytes

Method from_bytes

camel/loaders/base_io.py:294–328  ·  view source on GitHub ↗

r"""Creates a HtmlFile object from a BytesIO object. Args: file (BytesIO): A BytesIO object representing the contents of the html file. filename (str): The name of the file. Returns: HtmlFile: A HtmlFile object.

(cls, file: BytesIO, filename: str)

Source from the content-addressed store, hash-verified

292class HtmlFile(File):
293 @classmethod
294 def from_bytes(cls, file: BytesIO, filename: str) -> "HtmlFile":
295 r"""Creates a HtmlFile object from a BytesIO object.
296
297 Args:
298 file (BytesIO): A BytesIO object representing the contents of the
299 html file.
300 filename (str): The name of the file.
301
302 Returns:
303 HtmlFile: A HtmlFile object.
304 """
305 # Parse the HTML data from the file
306 try:
307 from bs4 import BeautifulSoup
308 except ImportError:
309 raise ImportError(
310 "Please install `beautifulsoup4` first. "
311 "You can install it by running "
312 "`pip install beautifulsoup4`."
313 )
314 soup = BeautifulSoup(file, "html.parser")
315 text = soup.get_text()
316 text = strip_consecutive_newlines(text)
317 # Create a dictionary with the parsed data
318 doc = {"page_content": text.strip()}
319 # Calculate a unique identifier for the file
320 file_id = md5(file.getvalue()).hexdigest()
321 # Reset the file pointer to the beginning
322 file.seek(0)
323 return cls(
324 name=filename,
325 file_id=file_id,
326 docs=[doc],
327 raw_bytes=file.getvalue(),
328 )

Callers

nothing calls this directly

Calls 1

Tested by

no test coverage detected