Spaces:
Runtime error
Runtime error
Download scrappers/process_pdfs.py from manasmaskar/Customer-Support-AI-Agent: direct link, hf CLI and curl.
- Browser
- Download file 3.74 kB
-
https://huggingface.co/spaces/manasmaskar/Customer-Support-AI-Agent/resolve/main/scrappers/process_pdfs.py
- Command line
-
hf download hf://spaces/manasmaskar/Customer-Support-AI-Agent/scrappers/process_pdfs.py
-
curl -L -o process_pdfs.py https://huggingface.co/spaces/manasmaskar/Customer-Support-AI-Agent/resolve/main/scrappers/process_pdfs.py
3.74 kB
| import os | |
| import json | |
| from pathlib import Path | |
| import PyPDF2 | |
| from typing import List, Dict | |
| class PDFProcessor: | |
| def __init__(self, pdf_folder: str = "data/pdfs"): | |
| self.pdf_folder = pdf_folder | |
| self.documents = [] | |
| def extract_text_from_pdf(self, pdf_path: str) -> str: | |
| """Extract text from a single PDF file""" | |
| try: | |
| with open(pdf_path, 'rb') as file: | |
| pdf_reader = PyPDF2.PdfReader(file) | |
| text = "" | |
| for page_num in range(len(pdf_reader.pages)): | |
| page = pdf_reader.pages[page_num] | |
| text += page.extract_text() + "\n" | |
| return text.strip() | |
| except Exception as e: | |
| print(f"Error reading {pdf_path}: {str(e)}") | |
| return "" | |
| def process_all_pdfs(self) -> List[Dict]: | |
| """Process all PDFs in the folder""" | |
| pdf_files = list(Path(self.pdf_folder).glob("*.pdf")) | |
| print(f"Found {len(pdf_files)} PDF files in {self.pdf_folder}\n") | |
| for pdf_file in pdf_files: | |
| print(f"Processing: {pdf_file.name}") | |
| text = self.extract_text_from_pdf(str(pdf_file)) | |
| if text: | |
| # Extract device name from filename | |
| device_name = pdf_file.stem.replace('_', ' ').replace('-', ' ') | |
| doc = { | |
| 'title': f"{device_name} - Device Documentation", | |
| 'source': str(pdf_file.name), | |
| 'content': text, | |
| 'type': 'pdf_documentation', | |
| 'device': device_name | |
| } | |
| self.documents.append(doc) | |
| print(f" ✓ Extracted {len(text)} characters\n") | |
| else: | |
| print(f" ✗ Failed to extract text\n") | |
| return self.documents | |
| def save_to_json(self, output_file: str = "data/pdf_docs.json"): | |
| """Save extracted PDF content to JSON""" | |
| with open(output_file, 'w', encoding='utf-8') as f: | |
| json.dump(self.documents, f, indent=2, ensure_ascii=False) | |
| print(f"✓ Saved {len(self.documents)} PDF documents to {output_file}") | |
| # Print summary | |
| print("\nSummary:") | |
| for doc in self.documents: | |
| print(f" - {doc['title']} ({len(doc['content'])} chars)") | |
| def merge_with_existing(self, existing_file: str = "data/arlo_docs.json", | |
| output_file: str = "data/all_arlo_docs.json"): | |
| """Merge PDF docs with existing scraped articles""" | |
| # Load existing docs | |
| with open(existing_file, 'r', encoding='utf-8') as f: | |
| existing_docs = json.load(f) | |
| # Combine | |
| all_docs = existing_docs + self.documents | |
| # Save combined | |
| with open(output_file, 'w', encoding='utf-8') as f: | |
| json.dump(all_docs, f, indent=2, ensure_ascii=False) | |
| print(f"\n✓ Merged documents saved to {output_file}") | |
| print(f" Total: {len(all_docs)} documents") | |
| print(f" - Scraped articles: {len(existing_docs)}") | |
| print(f" - PDF documents: {len(self.documents)}") | |
| # Run the processor | |
| if __name__ == "__main__": | |
| processor = PDFProcessor(pdf_folder="data/pdfs") | |
| # Process all PDFs | |
| docs = processor.process_all_pdfs() | |
| # Save to JSON | |
| processor.save_to_json("data/pdf_docs.json") | |
| # Merge with existing scraped articles | |
| processor.merge_with_existing( | |
| existing_file="data/arlo_docs.json", | |
| output_file="data/all_arlo_docs.json" | |
| ) | |