Spaces:
Build error
Build error
File size: 7,966 Bytes
18a68e7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
import os
import pymupdf4llm
import pandas as pd
import tempfile
from typing import Dict, Any, Optional, List
# Import unstructured components for different file types
from unstructured.partition.auto import partition
from unstructured.partition.pdf import partition_pdf
from unstructured.partition.docx import partition_docx
from unstructured.partition.pptx import partition_pptx
from unstructured.partition.xlsx import partition_xlsx
from unstructured.partition.md import partition_md
from unstructured.partition.html import partition_html
from unstructured.partition.xml import partition_xml
from unstructured.partition.email import partition_email
from unstructured.partition.text import partition_text
from unstructured.partition.epub import partition_epub
def get_processor_for_file(file_path: str) -> Optional[callable]:
"""
Determine the appropriate processor function for the given file type
"""
file_extension = os.path.splitext(file_path)[1].lower()
# Map file extensions to specific partition functions
processors = {
".pdf": process_pdf,
".docx": process_docx,
".doc": process_docx,
".pptx": process_pptx,
".ppt": process_pptx,
".xlsx": process_xlsx,
".xls": process_xlsx,
".md": process_markdown,
".html": process_html,
".htm": process_html,
".xml": process_xml,
".msg": process_email,
".eml": process_email,
".epub": process_epub,
".txt": process_text,
".csv": process_text,
".rtf": process_text,
# Code files
".py": process_text,
".js": process_text,
".java": process_text,
".ts": process_text,
".tsx": process_text,
".jsx": process_text,
".c": process_text,
".cpp": process_text,
".h": process_text,
".cs": process_text,
".rb": process_text,
".go": process_text,
".rs": process_text,
".php": process_text,
".sql": process_text,
".css": process_text,
}
return processors.get(file_extension, process_generic)
def process_document(file_path: str) -> Optional[str]:
"""
Process a document using the appropriate processor based on file type
"""
processor = get_processor_for_file(file_path)
if processor:
return processor(file_path)
return None
def process_pdf(file_path: str) -> str:
"""
Process PDF documents using unstructured
"""
temp_dir = tempfile.mkdtemp()
try:
# Try hi_res mode first with OCR capabilities
elements = partition_pdf(
filename=file_path,
strategy="hi_res",
extract_images_in_pdf=True,
extract_image_block_types=["Image", "Table"],
extract_image_block_to_payload=False,
extract_image_block_output_dir=temp_dir,
hi_res_model_name="yolox",
infer_table_structure=True,
chunking_strategy="by_title",
max_characters=4000,
new_after_n_chars=3800,
combine_text_under_n_chars=2000,
)
except Exception as e:
# Fall back to fast mode if hi_res fails
elements = partition_pdf(
filename=file_path,
strategy="fast",
chunking_strategy="by_title",
max_characters=4000,
new_after_n_chars=3800,
combine_text_under_n_chars=2000,
)
# Extract text from elements
texts = [element.text for element in elements if hasattr(element, 'text') and element.text]
combined_text = "\n\n".join(texts)
return combined_text
def process_docx(file_path: str) -> str:
"""
Process DOCX documents using unstructured
"""
elements = partition_docx(
filename=file_path,
chunking_strategy="by_title",
max_characters=4000,
new_after_n_chars=3800,
combine_text_under_n_chars=2000,
)
texts = [element.text for element in elements if hasattr(element, 'text') and element.text]
combined_text = "\n\n".join(texts)
return combined_text
def process_pptx(file_path: str) -> str:
"""
Process PPTX documents using unstructured
"""
elements = partition_pptx(
filename=file_path,
)
texts = [element.text for element in elements if hasattr(element, 'text') and element.text]
combined_text = "\n\n".join(texts)
return combined_text
def process_xlsx(file_path: str) -> str:
"""
Process XLSX documents using unstructured
"""
elements = partition_xlsx(
filename=file_path,
)
texts = [element.text for element in elements if hasattr(element, 'text') and element.text]
combined_text = "\n\n".join(texts)
return combined_text
def process_markdown(file_path: str) -> str:
"""
Process Markdown documents using unstructured
"""
elements = partition_md(
filename=file_path,
)
texts = [element.text for element in elements if hasattr(element, 'text') and element.text]
combined_text = "\n\n".join(texts)
return combined_text
def process_html(file_path: str) -> str:
"""
Process HTML documents using unstructured
"""
elements = partition_html(
filename=file_path,
)
texts = [element.text for element in elements if hasattr(element, 'text') and element.text]
combined_text = "\n\n".join(texts)
return combined_text
def process_xml(file_path: str) -> str:
"""
Process XML documents using unstructured
"""
elements = partition_xml(
filename=file_path,
)
texts = [element.text for element in elements if hasattr(element, 'text') and element.text]
combined_text = "\n\n".join(texts)
return combined_text
def process_email(file_path: str) -> str:
"""
Process email documents using unstructured
"""
elements = partition_email(
filename=file_path,
)
texts = [element.text for element in elements if hasattr(element, 'text') and element.text]
combined_text = "\n\n".join(texts)
return combined_text
def process_text(file_path: str) -> str:
"""
Process text documents using unstructured
"""
elements = partition_text(
filename=file_path,
chunking_strategy="by_title",
max_characters=4000,
new_after_n_chars=3800,
combine_text_under_n_chars=2000,
)
texts = [element.text for element in elements if hasattr(element, 'text') and element.text]
combined_text = "\n\n".join(texts)
return combined_text
def process_epub(file_path: str) -> str:
"""
Process EPUB documents using unstructured
"""
elements = partition_epub(
filename=file_path,
)
texts = [element.text for element in elements if hasattr(element, 'text') and element.text]
combined_text = "\n\n".join(texts)
return combined_text
def process_generic(file_path: str) -> str:
"""
Generic document processor using unstructured's auto partitioning
"""
try:
elements = partition(
filename=file_path,
)
texts = [element.text for element in elements if hasattr(element, 'text') and element.text]
combined_text = "\n\n".join(texts)
return combined_text
except Exception as e:
# Fall back to basic text processing if auto-partition fails
try:
with open(file_path, 'r', encoding='utf-8') as f:
return f.read()
except Exception:
# Try with a different encoding if utf-8 fails
try:
with open(file_path, 'r', encoding='latin-1') as f:
return f.read()
except Exception as e2:
raise Exception(f"Could not process file: {str(e)} / {str(e2)}") |