Spaces:
Runtime error
Runtime error
Download Backend/utils/extractor.py from jinksQspider/ExamCraftAI: direct link, hf CLI and curl.
- Browser
- Download file 1.14 kB
-
https://huggingface.co/spaces/jinksQspider/ExamCraftAI/resolve/main/Backend/utils/extractor.py
- Command line
-
hf download hf://spaces/jinksQspider/ExamCraftAI/Backend/utils/extractor.py
-
curl -L -o extractor.py https://huggingface.co/spaces/jinksQspider/ExamCraftAI/resolve/main/Backend/utils/extractor.py
1.14 kB
| import os | |
| from PyPDF2 import PdfReader | |
| from docx import Document | |
| def extract_text_from_pdf(file_path): | |
| text = "" | |
| try: | |
| reader = PdfReader(file_path) | |
| for page in reader.pages: | |
| text += page.extract_text() + "\n" | |
| except Exception as e: | |
| print(f"Error extracting PDF: {e}") | |
| return text | |
| def extract_text_from_docx(file_path): | |
| text = "" | |
| try: | |
| doc = Document(file_path) | |
| for para in doc.paragraphs: | |
| text += para.text + "\n" | |
| except Exception as e: | |
| print(f"Error extracting DOCX: {e}") | |
| return text | |
| def extract_text_from_txt(file_path): | |
| try: | |
| with open(file_path, 'r', encoding='utf-8') as f: | |
| return f.read() | |
| except Exception as e: | |
| print(f"Error extracting TXT: {e}") | |
| return "" | |
| def get_text_from_file(file_path): | |
| ext = os.path.splitext(file_path)[1].lower() | |
| if ext == '.pdf': | |
| return extract_text_from_pdf(file_path) | |
| elif ext == '.docx': | |
| return extract_text_from_docx(file_path) | |
| elif ext == '.txt': | |
| return extract_text_from_txt(file_path) | |
| else: | |
| return "" | |