mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-04 23:40:28 +08:00
309 lines
8.2 KiB
Python
309 lines
8.2 KiB
Python
"""
|
|
File Type Router
|
|
================
|
|
|
|
Centralized file type classification and routing for RAG pipelines.
|
|
Determines the appropriate processing method for each document type.
|
|
"""
|
|
|
|
from dataclasses import dataclass
|
|
from enum import Enum
|
|
from pathlib import Path
|
|
from typing import List
|
|
|
|
from deeptutor.logging import get_logger
|
|
|
|
logger = get_logger("FileTypeRouter")
|
|
|
|
|
|
class DocumentType(Enum):
|
|
"""Document type classification"""
|
|
|
|
PDF = "pdf" # Requires PDF parser
|
|
TEXT = "text" # Plain text, direct read
|
|
MARKDOWN = "markdown" # Structured text
|
|
DOCX = "docx" # Word documents
|
|
IMAGE = "image" # Images (may need OCR)
|
|
UNKNOWN = "unknown" # Unsupported
|
|
|
|
|
|
@dataclass
|
|
class FileClassification:
|
|
"""
|
|
Result of file classification.
|
|
|
|
Attributes:
|
|
parser_files: Files requiring parser processing (currently PDF)
|
|
text_files: Files that can be read directly as text
|
|
unsupported: Files with unsupported formats
|
|
"""
|
|
|
|
parser_files: List[str]
|
|
text_files: List[str]
|
|
unsupported: List[str]
|
|
|
|
class FileTypeRouter:
|
|
"""
|
|
File type router for RAG pipelines.
|
|
|
|
Classifies files before processing to route them to appropriate handlers:
|
|
- PDF files -> PDF parsing
|
|
- Text files -> Direct read (fast, simple)
|
|
- Unsupported -> Skip with warning
|
|
|
|
Usage:
|
|
router = FileTypeRouter()
|
|
classification = router.classify_files(file_paths)
|
|
|
|
# Process PDF files with parser
|
|
for pdf in classification.parser_files:
|
|
...
|
|
|
|
# Process text files directly
|
|
for txt in classification.text_files:
|
|
content = await FileTypeRouter.read_text_file(txt)
|
|
...
|
|
"""
|
|
|
|
# Extensions requiring parser processing (currently PDF)
|
|
PARSER_EXTENSIONS = {".pdf"}
|
|
|
|
# Extensions for direct text reading
|
|
TEXT_EXTENSIONS = {
|
|
# Plain text
|
|
".txt",
|
|
".text",
|
|
".log",
|
|
# Markup languages
|
|
".md",
|
|
".markdown",
|
|
".rst",
|
|
".asciidoc",
|
|
# Data formats
|
|
".json",
|
|
".yaml",
|
|
".yml",
|
|
".toml",
|
|
".csv",
|
|
".tsv",
|
|
# LaTeX
|
|
".tex",
|
|
".latex",
|
|
".bib",
|
|
# Code files
|
|
".py",
|
|
".js",
|
|
".ts",
|
|
".jsx",
|
|
".tsx",
|
|
".java",
|
|
".c",
|
|
".cpp",
|
|
".h",
|
|
".hpp",
|
|
".go",
|
|
".rs",
|
|
".rb",
|
|
".php",
|
|
".swift",
|
|
".kt",
|
|
".scala",
|
|
".r",
|
|
".sql",
|
|
".sh",
|
|
".bash",
|
|
".zsh",
|
|
".ps1",
|
|
# Web
|
|
".html",
|
|
".htm",
|
|
".xml",
|
|
".css",
|
|
".scss",
|
|
".sass",
|
|
".less",
|
|
# Config
|
|
".ini",
|
|
".cfg",
|
|
".conf",
|
|
".env",
|
|
".properties",
|
|
}
|
|
|
|
# Word document extensions (unsupported in llamaindex-only mode)
|
|
DOCX_EXTENSIONS = {".docx", ".doc"}
|
|
|
|
# Image extensions (unsupported in llamaindex-only mode)
|
|
IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".tiff", ".tif"}
|
|
|
|
@classmethod
|
|
def get_document_type(cls, file_path: str) -> DocumentType:
|
|
"""
|
|
Classify a single file by its type.
|
|
|
|
Args:
|
|
file_path: Path to the file
|
|
|
|
Returns:
|
|
DocumentType enum value
|
|
"""
|
|
ext = Path(file_path).suffix.lower()
|
|
|
|
if ext in cls.PARSER_EXTENSIONS:
|
|
return DocumentType.PDF
|
|
elif ext in cls.TEXT_EXTENSIONS:
|
|
return DocumentType.TEXT
|
|
elif ext in cls.DOCX_EXTENSIONS:
|
|
return DocumentType.DOCX
|
|
elif ext in cls.IMAGE_EXTENSIONS:
|
|
return DocumentType.IMAGE
|
|
else:
|
|
# Try to detect if it's a text file by content
|
|
if cls._is_text_file(file_path):
|
|
return DocumentType.TEXT
|
|
return DocumentType.UNKNOWN
|
|
|
|
@classmethod
|
|
def _is_text_file(cls, file_path: str, sample_size: int = 8192) -> bool:
|
|
"""
|
|
Detect if a file is text-based by examining its content.
|
|
|
|
Args:
|
|
file_path: Path to the file
|
|
sample_size: Number of bytes to sample
|
|
|
|
Returns:
|
|
True if file appears to be text
|
|
"""
|
|
try:
|
|
with open(file_path, "rb") as f:
|
|
chunk = f.read(sample_size)
|
|
|
|
# Check for null bytes (binary file indicator)
|
|
if b"\x00" in chunk:
|
|
return False
|
|
|
|
# Try to decode as UTF-8
|
|
chunk.decode("utf-8")
|
|
return True
|
|
except (UnicodeDecodeError, IOError, OSError):
|
|
return False
|
|
|
|
@classmethod
|
|
def classify_files(cls, file_paths: List[str]) -> FileClassification:
|
|
"""
|
|
Classify a list of files by processing method.
|
|
|
|
Args:
|
|
file_paths: List of file paths to classify
|
|
|
|
Returns:
|
|
FileClassification with files grouped by processing method
|
|
"""
|
|
parser_files = []
|
|
text_files = []
|
|
unsupported = []
|
|
|
|
for path in file_paths:
|
|
doc_type = cls.get_document_type(path)
|
|
|
|
if doc_type == DocumentType.PDF:
|
|
parser_files.append(path)
|
|
elif doc_type in (DocumentType.TEXT, DocumentType.MARKDOWN):
|
|
text_files.append(path)
|
|
else:
|
|
unsupported.append(path)
|
|
|
|
logger.debug(
|
|
f"Classified {len(file_paths)} files: "
|
|
f"{len(parser_files)} parser, {len(text_files)} text, {len(unsupported)} unsupported"
|
|
)
|
|
|
|
return FileClassification(
|
|
parser_files=parser_files,
|
|
text_files=text_files,
|
|
unsupported=unsupported,
|
|
)
|
|
|
|
@classmethod
|
|
async def read_text_file(cls, file_path: str) -> str:
|
|
"""
|
|
Read a text file with automatic encoding detection.
|
|
|
|
Args:
|
|
file_path: Path to the text file
|
|
|
|
Returns:
|
|
File content as string
|
|
"""
|
|
encodings = ["utf-8", "utf-8-sig", "gbk", "gb2312", "gb18030", "latin-1", "cp1252"]
|
|
|
|
for encoding in encodings:
|
|
try:
|
|
with open(file_path, "r", encoding=encoding) as f:
|
|
return f.read()
|
|
except UnicodeDecodeError:
|
|
continue
|
|
|
|
# Last resort: read with error replacement
|
|
with open(file_path, "rb") as f:
|
|
return f.read().decode("utf-8", errors="replace")
|
|
|
|
@classmethod
|
|
def needs_parser(cls, file_path: str) -> bool:
|
|
"""
|
|
Quick check if a single file needs parser processing.
|
|
|
|
Args:
|
|
file_path: Path to the file
|
|
|
|
Returns:
|
|
True if file requires parser processing
|
|
"""
|
|
doc_type = cls.get_document_type(file_path)
|
|
return doc_type in (DocumentType.PDF, DocumentType.DOCX, DocumentType.IMAGE)
|
|
|
|
@classmethod
|
|
def is_text_readable(cls, file_path: str) -> bool:
|
|
"""
|
|
Check if a file can be read directly as text.
|
|
|
|
Args:
|
|
file_path: Path to the file
|
|
|
|
Returns:
|
|
True if file can be read as text
|
|
"""
|
|
doc_type = cls.get_document_type(file_path)
|
|
return doc_type in (DocumentType.TEXT, DocumentType.MARKDOWN)
|
|
|
|
@classmethod
|
|
def get_extensions_for_provider(cls, provider: str) -> set[str]:
|
|
"""
|
|
Get supported file extensions for a specific RAG provider.
|
|
|
|
Args:
|
|
provider: RAG provider name
|
|
|
|
Returns:
|
|
Set of supported file extensions (with leading dot, e.g., {".pdf", ".txt"})
|
|
"""
|
|
# Only llamaindex is supported; keep provider param for forward compatibility.
|
|
if provider and provider != "llamaindex":
|
|
logger.warning(f"Unknown/legacy provider '{provider}', using llamaindex extension set")
|
|
return cls.PARSER_EXTENSIONS | cls.TEXT_EXTENSIONS
|
|
|
|
@classmethod
|
|
def get_glob_patterns_for_provider(cls, provider: str) -> list[str]:
|
|
"""
|
|
Get glob patterns for file searching based on RAG provider.
|
|
|
|
Args:
|
|
provider: RAG provider name
|
|
|
|
Returns:
|
|
List of glob patterns (e.g., ["*.pdf", "*.txt", "*.md"])
|
|
"""
|
|
extensions = cls.get_extensions_for_provider(provider)
|
|
return [f"*{ext}" for ext in sorted(extensions)]
|