Spaces:
Sleeping
Sleeping
Download file_processor/processor.py from nikhildsst/RAG_Chatbot: direct link, hf CLI and curl.
- Browser
- Download file 1.13 kB
-
https://huggingface.co/spaces/nikhildsst/RAG_Chatbot/resolve/main/file_processor/processor.py
- Command line
-
hf download hf://spaces/nikhildsst/RAG_Chatbot/file_processor/processor.py
-
curl -L -o processor.py https://huggingface.co/spaces/nikhildsst/RAG_Chatbot/resolve/main/file_processor/processor.py
1.13 kB
| # file_processor/processor.py | |
| import PyPDF2 | |
| from docx import Document | |
| import magic | |
| import os | |
| class FileProcessor: | |
| def process_file(self, filepath): | |
| file_type = magic.from_file(filepath, mime=True) | |
| if file_type == 'text/plain': | |
| return self._process_txt(filepath) | |
| elif file_type == 'application/pdf': | |
| return self._process_pdf(filepath) | |
| elif file_type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': | |
| return self._process_docx(filepath) | |
| else: | |
| raise ValueError(f"Unsupported file type: {file_type}") | |
| def _process_txt(self, filepath): | |
| with open(filepath, 'r', encoding='utf-8') as f: | |
| return f.read() | |
| def _process_pdf(self, filepath): | |
| text = "" | |
| with open(filepath, 'rb') as f: | |
| reader = PyPDF2.PdfReader(f) | |
| for page in reader.pages: | |
| text += page.extract_text() | |
| return text | |
| def _process_docx(self, filepath): | |
| doc = Document(filepath) | |
| return '\n'.join([para.text for para in doc.paragraphs]) |