This repository was archived by the owner on Apr 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenhanced_file_processor.py
More file actions
128 lines (105 loc) · 4.19 KB
/
Copy pathenhanced_file_processor.py
File metadata and controls
128 lines (105 loc) · 4.19 KB
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
"""
Enhanced File Processing Module for PSA Resume Optimizer
Supports PDF, DOCX, TXT, OCR, and advanced text extraction
"""
import streamlit as st
import io
import tempfile
import os
from typing import Dict, List, Optional, Tuple
import hashlib
import json
from datetime import datetime, timedelta
# Basic enhanced file processor
class EnhancedFileProcessor:
def __init__(self, cache_dir: str = "file_cache"):
self.cache_dir = cache_dir
self.max_file_size = 50 * 1024 * 1024 # 50MB
os.makedirs(cache_dir, exist_ok=True)
def extract_text_from_file(self, file) -> Tuple[str, Dict]:
"""Enhanced file text extraction with better support"""
if not self.validate_file(file):
return "", {}
metadata = {
'file_name': file.name,
'file_size': file.size,
'file_type': file.type,
'extraction_method': 'enhanced',
'word_count': 0,
'extraction_timestamp': datetime.now().isoformat()
}
try:
if file.type == "application/pdf":
text = self._extract_from_pdf(file)
metadata['extraction_method'] = 'pdf_enhanced'
elif file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
text = self._extract_from_docx(file)
metadata['extraction_method'] = 'docx'
elif file.type == "text/plain":
text = self._extract_from_text(file)
metadata['extraction_method'] = 'text'
else:
st.error(f"Unsupported file type: {file.type}")
return "", metadata
metadata['word_count'] = len(text.split())
return text, metadata
except Exception as e:
st.error(f"Error extracting text from {file.name}: {str(e)}")
return "", metadata
def _extract_from_pdf(self, file) -> str:
"""Extract text from PDF"""
try:
from PyPDF2 import PdfReader
file_stream = io.BytesIO(file.getvalue())
reader = PdfReader(file_stream)
text_parts = []
for page in reader.pages:
page_text = page.extract_text()
if page_text:
text_parts.append(page_text)
return "\n".join(text_parts)
except Exception as e:
st.warning(f"PDF extraction error: {str(e)}")
return ""
def _extract_from_docx(self, file) -> str:
"""Extract text from DOCX files"""
try:
from docx import Document
file_stream = io.BytesIO(file.getvalue())
doc = Document(file_stream)
text_parts = []
for paragraph in doc.paragraphs:
if paragraph.text.strip():
text_parts.append(paragraph.text)
return "\n".join(text_parts)
except Exception as e:
st.warning(f"DOCX extraction error: {str(e)}")
return ""
def _extract_from_text(self, file) -> str:
"""Extract text from plain text files"""
try:
return str(file.getvalue(), "utf-8")
except UnicodeDecodeError:
try:
return str(file.getvalue(), "latin-1")
except Exception as e:
st.warning(f"Text extraction error: {str(e)}")
return ""
def validate_file(self, file) -> bool:
"""Enhanced file validation"""
if file is None:
return False
if file.size > self.max_file_size:
st.error(f"File '{file.name}' is too large. Maximum size: {self.max_file_size // (1024*1024)}MB")
return False
valid_types = [
"application/pdf",
"text/plain",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
]
if file.type not in valid_types:
st.error(f"Invalid file type '{file.type}'. Supported: PDF, DOCX, TXT")
return False
return True
# Global instance
file_processor = EnhancedFileProcessor()