Spaces:
Sleeping
Sleeping
File size: 2,942 Bytes
2d25973 88c4bb0 2d25973 88c4bb0 2d25973 88c4bb0 2d25973 | 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 | import io
import docx
import pptx
from pypdf import PdfReader
from PIL import Image
from pdf2image import convert_from_bytes
from ..core.vision_client import extract_text_from_image
def extract_universal_text(uploaded_file):
"""
Extracts text from uploaded files universally supporting PDF, DOCX, PPTX, TXT, and MD.
Expects a Streamlit UploadedFile or file-like object with a `.name` attribute.
"""
try:
filename = uploaded_file.name.lower()
if hasattr(uploaded_file, "seek"):
uploaded_file.seek(0)
if filename.endswith(".pdf"):
reader = PdfReader(uploaded_file)
text = ""
for page in reader.pages:
extracted = page.extract_text()
if extracted:
text += extracted + "\n"
# Smart Fallback for scanned PDFs
num_pages = len(reader.pages)
if num_pages > 0 and len(text.strip()) / num_pages < 50:
print("LOG: [Extractor] -> PDF text suspiciously short, falling back to Vision OCR via pdf2image...")
if hasattr(uploaded_file, "seek"):
uploaded_file.seek(0)
pdf_bytes = uploaded_file.read()
images = convert_from_bytes(pdf_bytes)
text = ""
for idx, img in enumerate(images):
print(f"LOG: [Extractor] -> OCR on PDF page {idx+1}/{len(images)}...")
page_text = extract_text_from_image(img)
text += f"\\n--- Page {idx+1} ---\\n{page_text}\\n"
return text
elif filename.endswith(".docx"):
doc = docx.Document(uploaded_file)
text = ""
for paragraph in doc.paragraphs:
text += paragraph.text + "\n"
return text
elif filename.endswith(".pptx"):
prs = pptx.Presentation(uploaded_file)
text = ""
for slide in prs.slides:
for shape in slide.shapes:
if hasattr(shape, "has_text_frame") and shape.has_text_frame:
text += shape.text + "\n"
return text
elif filename.endswith(".txt") or filename.endswith(".md"):
content = uploaded_file.read()
if isinstance(content, bytes):
return content.decode("utf-8", errors="replace")
return str(content)
elif filename.endswith((".png", ".jpg", ".jpeg")):
img = Image.open(uploaded_file)
if img.mode != "RGB":
img = img.convert("RGB")
text = extract_text_from_image(img)
return text
else:
return ""
except Exception as e:
print(f"Extraction error: {e}")
return ""
|