Skip to content

Commit 68da35c

Browse files
committed
added support for csv + txt file
1 parent 01ca7dd commit 68da35c

1 file changed

Lines changed: 32 additions & 9 deletions

File tree

src/utils/utils.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from weaviate.classes.config import Property, DataType, Configure, VectorDistances
3535
from GrobidArticleExtractor import GrobidArticleExtractor
3636
import requests
37+
import pandas as pd
3738
from requests.exceptions import RequestException
3839

3940

@@ -62,10 +63,11 @@ def process_input_data(source:str):
6263
# Log all paths being tried
6364
logger.info(f"Trying paths: {[str(p) for p in paths_to_try]}")
6465

66+
6567
# Check if this is raw text input
6668
is_raw_text = (
6769
# do not contains any extensions like .pdf
68-
(not source.lower().endswith(".pdf")) or
70+
(not source.lower().endswith((".pdf", ".csv", ".txt"))) or
6971
# If it's a very long string, treat as raw text
7072
len(source) > 500 or
7173
# Or if it contains newlines
@@ -108,14 +110,35 @@ def process_input_data(source:str):
108110
# Process single file
109111
if source_path.is_file():
110112
logger.info(f"Processing single file: {source_path}")
111-
GROBID_SERVER_URL_OR_EXTERNAL_SERVICE = os.getenv("GROBID_SERVER_URL_OR_EXTERNAL_SERVICE",
112-
"http://localhost:8070")
113-
EXTERNAL_PDF_EXTRACTION_SERVICE = os.getenv("EXTERNAL_PDF_EXTRACTION_SERVICE", "False")
114-
return extract_pdf_content(
115-
file_path=source_path,
116-
grobid_server=GROBID_SERVER_URL_OR_EXTERNAL_SERVICE,
117-
external_service=EXTERNAL_PDF_EXTRACTION_SERVICE
118-
)
113+
ext = source_path.suffix.lower()
114+
if ext ==".pdf":
115+
GROBID_SERVER_URL_OR_EXTERNAL_SERVICE = os.getenv("GROBID_SERVER_URL_OR_EXTERNAL_SERVICE",
116+
"http://localhost:8070")
117+
EXTERNAL_PDF_EXTRACTION_SERVICE = os.getenv("EXTERNAL_PDF_EXTRACTION_SERVICE", "False")
118+
return extract_pdf_content(
119+
file_path=source_path,
120+
grobid_server=GROBID_SERVER_URL_OR_EXTERNAL_SERVICE,
121+
external_service=EXTERNAL_PDF_EXTRACTION_SERVICE
122+
)
123+
elif ext == ".csv":
124+
try:
125+
df = pd.read_csv(source_path)
126+
return df.to_dict(orient="records")
127+
except Exception as e:
128+
logger.error(f"Error reading CSV file: {e}")
129+
return {"status": "Error", "error": str(e)}
130+
elif ext == ".txt":
131+
try:
132+
with open(source_path, "r", encoding="utf-8") as f:
133+
return f.read()
134+
except Exception as e:
135+
logger.error(f"Error reading TXT file: {e}")
136+
return {"status": "Error", "error": str(e)}
137+
else:
138+
error_msg = f"Unsupported file format: {ext}"
139+
logger.error(error_msg)
140+
return {"status": "Error", "error": error_msg}
141+
119142

120143
def extract_pdf_content(file_path: str, grobid_server: str , external_service: str ) -> dict:
121144
"""

0 commit comments

Comments
 (0)