|
22 | 22 | from camel.utils import dependencies_required
|
23 | 23 |
|
24 | 24 |
|
| 25 | +def create_file(file: BytesIO, filename: str) -> "File": |
| 26 | + r"""Reads an uploaded file and returns a File object. |
| 27 | +
|
| 28 | + Args: |
| 29 | + file (BytesIO): A BytesIO object representing the contents of the |
| 30 | + file. |
| 31 | + filename (str): The name of the file. |
| 32 | +
|
| 33 | + Returns: |
| 34 | + File: A File object. |
| 35 | + """ |
| 36 | + ext_to_cls = { |
| 37 | + "docx": DocxFile, |
| 38 | + "pdf": PdfFile, |
| 39 | + "txt": TxtFile, |
| 40 | + "json": JsonFile, |
| 41 | + "html": HtmlFile, |
| 42 | + } |
| 43 | + |
| 44 | + ext = filename.split(".")[-1].lower() |
| 45 | + if ext not in ext_to_cls: |
| 46 | + raise NotImplementedError(f"File type {ext} not supported") |
| 47 | + |
| 48 | + out_file = ext_to_cls[ext].from_bytes(file, filename) |
| 49 | + return out_file |
| 50 | + |
| 51 | + |
| 52 | +def create_file_from_raw_bytes(raw_bytes: bytes, filename: str) -> "File": |
| 53 | + r"""Reads raw bytes and returns a File object. |
| 54 | +
|
| 55 | + Args: |
| 56 | + raw_bytes (bytes): The raw bytes content of the file. |
| 57 | + filename (str): The name of the file. |
| 58 | +
|
| 59 | + Returns: |
| 60 | + File: A File object. |
| 61 | + """ |
| 62 | + file = BytesIO(raw_bytes) |
| 63 | + return create_file(file, filename) |
| 64 | + |
| 65 | + |
25 | 66 | class File(ABC):
|
26 | 67 | r"""Represents an uploaded file comprised of Documents.
|
27 | 68 |
|
@@ -79,47 +120,6 @@ def from_raw_bytes(cls, raw_bytes: bytes, filename: str) -> "File":
|
79 | 120 | file = BytesIO(raw_bytes)
|
80 | 121 | return cls.from_bytes(file, filename)
|
81 | 122 |
|
82 |
| - @staticmethod |
83 |
| - def create_file(file: BytesIO, filename: str) -> "File": |
84 |
| - r"""Reads an uploaded file and returns a File object. |
85 |
| -
|
86 |
| - Args: |
87 |
| - file (BytesIO): A BytesIO object representing the contents of the |
88 |
| - file. |
89 |
| - filename (str): The name of the file. |
90 |
| -
|
91 |
| - Returns: |
92 |
| - File: A File object. |
93 |
| - """ |
94 |
| - ext_to_cls = { |
95 |
| - "docx": DocxFile, |
96 |
| - "pdf": PdfFile, |
97 |
| - "txt": TxtFile, |
98 |
| - "json": JsonFile, |
99 |
| - "html": HtmlFile, |
100 |
| - } |
101 |
| - |
102 |
| - ext = filename.split(".")[-1].lower() |
103 |
| - if ext not in ext_to_cls: |
104 |
| - raise NotImplementedError(f"File type {ext} not supported") |
105 |
| - |
106 |
| - out_file = ext_to_cls[ext].from_bytes(file, filename) |
107 |
| - return out_file |
108 |
| - |
109 |
| - @staticmethod |
110 |
| - def create_file_from_raw_bytes(raw_bytes: bytes, filename: str) -> "File": |
111 |
| - r"""Reads raw bytes and returns a File object. |
112 |
| -
|
113 |
| - Args: |
114 |
| - raw_bytes (bytes): The raw bytes content of the file. |
115 |
| - filename (str): The name of the file. |
116 |
| -
|
117 |
| - Returns: |
118 |
| - File: A File object. |
119 |
| - """ |
120 |
| - file = BytesIO(raw_bytes) |
121 |
| - return File.create_file(file, filename) |
122 |
| - |
123 | 123 | def __repr__(self) -> str:
|
124 | 124 | return (
|
125 | 125 | f"File(name={self.name}, id={self.file_id}, "
|
|
0 commit comments