Spaces:
Runtime error
Runtime error
Download core/doc_processor.py from utkarsh134/FastAPIExperience2: direct link, hf CLI and curl.
- Browser
- Download file 2.45 kB
-
https://huggingface.co/spaces/utkarsh134/FastAPIExperience2/resolve/112b38aa61b7b6eb844c0dfb1106818c06d66aed/core/doc_processor.py
- Command line
-
hf download hf://spaces/utkarsh134/FastAPIExperience2@112b38aa61b7b6eb844c0dfb1106818c06d66aed/core/doc_processor.py
-
curl -L -o doc_processor.py https://huggingface.co/spaces/utkarsh134/FastAPIExperience2/resolve/112b38aa61b7b6eb844c0dfb1106818c06d66aed/core/doc_processor.py
2.45 kB
| import pymupdf | |
| from io import BytesIO | |
| import docx2txt | |
| from datetime import datetime | |
| import zipfile | |
| from xml.etree import ElementTree as ET | |
| def extract_text_from_bytes(pdf_bytes): | |
| try: | |
| memory_stream = BytesIO(pdf_bytes) | |
| doc = pymupdf.open(stream=memory_stream, filetype="pdf") | |
| # Extract text | |
| text = "" | |
| for page in doc: | |
| text += page.get_text() | |
| # Get modification date | |
| metadata = doc.metadata | |
| mod_date = metadata.get("modDate", None) | |
| doc.close() | |
| return text, mod_date | |
| except Exception as e: | |
| return f"Error reading PDF: {str(e)}", None | |
| def get_docx_last_modified(docx_stream): | |
| try: | |
| docx_stream.seek(0) # Reset stream position to start | |
| with zipfile.ZipFile(docx_stream, 'r') as docx: | |
| if 'docProps/core.xml' in docx.namelist(): | |
| with docx.open('docProps/core.xml') as f: | |
| core_xml = f.read() | |
| root = ET.fromstring(core_xml) | |
| namespaces = { | |
| 'cp': 'http://schemas.openxmlformats.org/package/2006/metadata/core-properties', | |
| 'dcterms': 'http://purl.org/dc/terms/' | |
| } | |
| modified_element = root.find('.//dcterms:modified', namespaces) | |
| if modified_element is not None: | |
| modified_date_str = modified_element.text | |
| return datetime.fromisoformat(modified_date_str.replace('Z', '+00:00')) | |
| except Exception as e: | |
| print(f"Error extracting DOCX metadata: {e}") | |
| return None | |
| def extract_text_from_docx(docx_bytes): | |
| try: | |
| docx_stream = BytesIO(docx_bytes) | |
| text = docx2txt.process(docx_stream) | |
| metadata_stream = BytesIO(docx_bytes) | |
| mod_date = get_docx_last_modified(metadata_stream) | |
| return text, mod_date | |
| except Exception as e: | |
| return f"Error reading DOCX: {str(e)}", None | |
| def parse_pdf_date(pdf_date: str) -> datetime: | |
| if not pdf_date or not pdf_date.startswith("D:"): | |
| return datetime.now() | |
| try: | |
| date_str = pdf_date[2:14] | |
| return datetime.strptime(date_str, "%Y%m%d%H%M") | |
| except Exception: | |
| return datetime.now() | |