|
34 | 34 | from weaviate.classes.config import Property, DataType, Configure, VectorDistances |
35 | 35 | from GrobidArticleExtractor import GrobidArticleExtractor |
36 | 36 | import requests |
| 37 | +import pandas as pd |
37 | 38 | from requests.exceptions import RequestException |
38 | 39 |
|
39 | 40 |
|
@@ -62,10 +63,11 @@ def process_input_data(source:str): |
62 | 63 | # Log all paths being tried |
63 | 64 | logger.info(f"Trying paths: {[str(p) for p in paths_to_try]}") |
64 | 65 |
|
| 66 | + |
65 | 67 | # Check if this is raw text input |
66 | 68 | is_raw_text = ( |
67 | 69 | # do not contains any extensions like .pdf |
68 | | - (not source.lower().endswith(".pdf")) or |
| 70 | + (not source.lower().endswith((".pdf", ".csv", ".txt"))) or |
69 | 71 | # If it's a very long string, treat as raw text |
70 | 72 | len(source) > 500 or |
71 | 73 | # Or if it contains newlines |
@@ -108,14 +110,35 @@ def process_input_data(source:str): |
108 | 110 | # Process single file |
109 | 111 | if source_path.is_file(): |
110 | 112 | 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 | + |
119 | 142 |
|
120 | 143 | def extract_pdf_content(file_path: str, grobid_server: str , external_service: str ) -> dict: |
121 | 144 | """ |
|
0 commit comments