-
-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATS][ csv_to_text] [json_to_text] [txt_to_text] [data_to_text] [Ag…
…ent][ingest_docs]
- Loading branch information
Kye
committed
Jan 16, 2024
1 parent
b61c250
commit ef47594
Showing
3 changed files
with
95 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import os | ||
import csv | ||
import json | ||
from swarms.utils.pdf_to_text import pdf_to_text | ||
|
||
|
||
def csv_to_text(file): | ||
with open(file, "r") as file: | ||
reader = csv.reader(file) | ||
data = list(reader) | ||
return str(data) | ||
|
||
|
||
def json_to_text(file): | ||
with open(file, "r") as file: | ||
data = json.load(file) | ||
return json.dumps(data) | ||
|
||
|
||
def txt_to_text(file): | ||
with open(file, "r") as file: | ||
data = file.read() | ||
return data | ||
|
||
|
||
def data_to_text(file): | ||
""" | ||
Converts the given data file to text format. | ||
Args: | ||
file (str): The path to the data file. | ||
Returns: | ||
str: The text representation of the data file. | ||
Raises: | ||
ValueError: If the file extension is not supported. | ||
""" | ||
_, ext = os.path.splitext(file) | ||
if ext == ".csv": | ||
return csv_to_text(file) | ||
elif ext == ".json": | ||
return json_to_text(file) | ||
elif ext == ".txt": | ||
return txt_to_text(file) | ||
elif ext == ".pdf": | ||
return pdf_to_text(file) | ||
else: | ||
raise ValueError(f"Unsupported file extension: {ext}") |