FD900 commited on
Commit
5fc87ee
·
verified ·
1 Parent(s): ca8e089

Update tools/file_loader.py

Browse files
Files changed (1) hide show
  1. tools/file_loader.py +25 -1
tools/file_loader.py CHANGED
@@ -1,4 +1,5 @@
1
  import io
 
2
  import pandas as pd
3
  from PyPDF2 import PdfReader
4
 
@@ -34,4 +35,27 @@ def read_txt(file_bytes: bytes) -> str:
34
  except UnicodeDecodeError:
35
  return file_bytes.decode('latin1', errors='ignore').strip()
36
  except Exception as e:
37
- return f"[ERROR] Failed to read TXT: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import io
2
+ import os
3
  import pandas as pd
4
  from PyPDF2 import PdfReader
5
 
 
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}"