Spaces:
Sleeping
Sleeping
Delete tools/file_loader.py
Browse files- tools/file_loader.py +0 -61
tools/file_loader.py
DELETED
|
@@ -1,61 +0,0 @@
|
|
| 1 |
-
import io
|
| 2 |
-
import os
|
| 3 |
-
import pandas as pd
|
| 4 |
-
from PyPDF2 import PdfReader
|
| 5 |
-
|
| 6 |
-
def read_pdf(file_bytes: bytes) -> str:
|
| 7 |
-
"""
|
| 8 |
-
Extracts text from a PDF file provided as bytes.
|
| 9 |
-
"""
|
| 10 |
-
try:
|
| 11 |
-
reader = PdfReader(io.BytesIO(file_bytes))
|
| 12 |
-
text = ""
|
| 13 |
-
for page in reader.pages:
|
| 14 |
-
text += page.extract_text() or ""
|
| 15 |
-
return text.strip()
|
| 16 |
-
except Exception as e:
|
| 17 |
-
return f"[ERROR] Failed to read PDF: {e}"
|
| 18 |
-
|
| 19 |
-
def read_csv(file_bytes: bytes) -> str:
|
| 20 |
-
"""
|
| 21 |
-
Reads the first few rows of a CSV file provided as bytes.
|
| 22 |
-
"""
|
| 23 |
-
try:
|
| 24 |
-
df = pd.read_csv(io.BytesIO(file_bytes))
|
| 25 |
-
return df.head(10).to_string(index=False)
|
| 26 |
-
except Exception as e:
|
| 27 |
-
return f"[ERROR] Failed to read CSV: {e}"
|
| 28 |
-
|
| 29 |
-
def read_txt(file_bytes: bytes) -> str:
|
| 30 |
-
"""
|
| 31 |
-
Reads a plain text file provided as bytes.
|
| 32 |
-
"""
|
| 33 |
-
try:
|
| 34 |
-
return file_bytes.decode('utf-8').strip()
|
| 35 |
-
except UnicodeDecodeError:
|
| 36 |
-
return file_bytes.decode('latin1', errors='ignore').strip()
|
| 37 |
-
except Exception as e:
|
| 38 |
-
return f"[ERROR] Failed to read TXT: {e}"
|
| 39 |
-
|
| 40 |
-
def load_file_if_any(file_path: str) -> str:
|
| 41 |
-
"""
|
| 42 |
-
Loads and reads file content based on file extension.
|
| 43 |
-
Supports .pdf, .csv, .txt files. Returns "" if no file.
|
| 44 |
-
"""
|
| 45 |
-
if not file_path or not os.path.exists(file_path):
|
| 46 |
-
return ""
|
| 47 |
-
|
| 48 |
-
try:
|
| 49 |
-
with open(file_path, "rb") as f:
|
| 50 |
-
file_bytes = f.read()
|
| 51 |
-
|
| 52 |
-
if file_path.endswith(".pdf"):
|
| 53 |
-
return read_pdf(file_bytes)
|
| 54 |
-
elif file_path.endswith(".csv"):
|
| 55 |
-
return read_csv(file_bytes)
|
| 56 |
-
elif file_path.endswith(".txt"):
|
| 57 |
-
return read_txt(file_bytes)
|
| 58 |
-
else:
|
| 59 |
-
return "[WARNING] Unsupported file format."
|
| 60 |
-
except Exception as e:
|
| 61 |
-
return f"[ERROR] Failed to load file: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|