Spaces:
Sleeping
Sleeping
curriculum update
Browse files- preprocess_curriculum.py +46 -62
preprocess_curriculum.py
CHANGED
|
@@ -7,69 +7,47 @@ import nltk
|
|
| 7 |
import tiktoken
|
| 8 |
import json
|
| 9 |
|
| 10 |
-
nltk.download('
|
| 11 |
|
| 12 |
-
def
|
|
|
|
| 13 |
text = ''
|
|
|
|
| 14 |
with open(pdf_path, 'rb') as file:
|
| 15 |
reader = PyPDF2.PdfReader(file)
|
| 16 |
num_pages = len(reader.pages)
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
for start, end in page_ranges:
|
| 22 |
-
# Pages are zero-indexed in PyPDF2
|
| 23 |
-
selected_pages.extend(range(start - 1, end))
|
| 24 |
-
|
| 25 |
-
selected_pages = sorted(set(selected_pages))
|
| 26 |
-
print(f"Processing pages: {selected_pages}")
|
| 27 |
-
|
| 28 |
-
for page_num in selected_pages:
|
| 29 |
-
if page_num < 0 or page_num >= num_pages:
|
| 30 |
-
print(f"Page {page_num + 1} is out of range.")
|
| 31 |
-
continue
|
| 32 |
-
page = reader.pages[page_num]
|
| 33 |
-
try:
|
| 34 |
-
page_text = page.extract_text()
|
| 35 |
-
if page_text:
|
| 36 |
-
text += f"<PAGE {page_num+1}>\n{page_text}\n"
|
| 37 |
-
else:
|
| 38 |
-
print(f"No text found on page {page_num + 1}")
|
| 39 |
-
except Exception as e:
|
| 40 |
-
print(f"Error reading page {page_num + 1}: {e}")
|
| 41 |
-
return text
|
| 42 |
-
|
| 43 |
-
def extract_text_from_images(pdf_path, page_ranges):
|
| 44 |
-
text = ''
|
| 45 |
-
print("Performing OCR on images...")
|
| 46 |
-
pages_to_process = []
|
| 47 |
-
for start, end in page_ranges:
|
| 48 |
-
pages_to_process.extend(range(start, end + 1))
|
| 49 |
-
print(f"Adding pages {start} to {end} to the list of pages to process...")
|
| 50 |
-
pages_to_process = sorted(set(pages_to_process))
|
| 51 |
-
print(f"Total number of pages to process: {len(pages_to_process)}")
|
| 52 |
-
|
| 53 |
-
for page_num in pages_to_process:
|
| 54 |
-
print(f"Converting page {page_num} to image...")
|
| 55 |
try:
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
)
|
| 62 |
-
if images:
|
| 63 |
-
image = images[0]
|
| 64 |
-
ocr_text = pytesseract.image_to_string(image)
|
| 65 |
-
if ocr_text.strip():
|
| 66 |
-
text += f"<PAGE {page_num}>\n{ocr_text}\n"
|
| 67 |
-
else:
|
| 68 |
-
print(f"No text found in image of page {page_num}")
|
| 69 |
else:
|
| 70 |
-
print(f"
|
| 71 |
except Exception as e:
|
| 72 |
-
print(f"Error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
return text
|
| 74 |
|
| 75 |
def split_into_chunks(text, max_tokens=512):
|
|
@@ -123,14 +101,20 @@ if __name__ == '__main__':
|
|
| 123 |
(167, 194)
|
| 124 |
]
|
| 125 |
|
| 126 |
-
#
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
if total_text:
|
| 136 |
# Split into chunks
|
|
|
|
| 7 |
import tiktoken
|
| 8 |
import json
|
| 9 |
|
| 10 |
+
nltk.download('punkt_tab')
|
| 11 |
|
| 12 |
+
def extract_text_from_page(pdf_path, page_num):
|
| 13 |
+
# page_num is 1-indexed
|
| 14 |
text = ''
|
| 15 |
+
# Try to extract text using PyPDF2
|
| 16 |
with open(pdf_path, 'rb') as file:
|
| 17 |
reader = PyPDF2.PdfReader(file)
|
| 18 |
num_pages = len(reader.pages)
|
| 19 |
+
if page_num < 1 or page_num > num_pages:
|
| 20 |
+
print(f"Page {page_num} is out of range.")
|
| 21 |
+
return ''
|
| 22 |
+
page = reader.pages[page_num - 1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
try:
|
| 24 |
+
page_text = page.extract_text()
|
| 25 |
+
if page_text and page_text.strip():
|
| 26 |
+
print(f"Text extracted from page {page_num} using PyPDF2.")
|
| 27 |
+
text = f"<PAGE {page_num}>\n{page_text}\n"
|
| 28 |
+
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
else:
|
| 30 |
+
print(f"No text found on page {page_num} using PyPDF2.")
|
| 31 |
except Exception as e:
|
| 32 |
+
print(f"Error reading page {page_num} with PyPDF2: {e}")
|
| 33 |
+
|
| 34 |
+
# If no text was extracted, perform OCR
|
| 35 |
+
try:
|
| 36 |
+
print(f"Performing OCR on page {page_num}...")
|
| 37 |
+
images = convert_from_path(pdf_path, first_page=page_num, last_page=page_num, dpi=200)
|
| 38 |
+
if images:
|
| 39 |
+
image = images[0]
|
| 40 |
+
ocr_text = pytesseract.image_to_string(image)
|
| 41 |
+
if ocr_text and ocr_text.strip():
|
| 42 |
+
print(f"OCR text extracted from page {page_num}.")
|
| 43 |
+
text = f"<PAGE {page_num}>\n{ocr_text}\n"
|
| 44 |
+
else:
|
| 45 |
+
print(f"No text found on page {page_num} using OCR.")
|
| 46 |
+
else:
|
| 47 |
+
print(f"Failed to convert page {page_num} to image.")
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"Error performing OCR on page {page_num}: {e}")
|
| 50 |
+
|
| 51 |
return text
|
| 52 |
|
| 53 |
def split_into_chunks(text, max_tokens=512):
|
|
|
|
| 101 |
(167, 194)
|
| 102 |
]
|
| 103 |
|
| 104 |
+
# Create a list of all page numbers to process
|
| 105 |
+
pages_to_process = []
|
| 106 |
+
for start, end in page_ranges:
|
| 107 |
+
pages_to_process.extend(range(start, end + 1))
|
| 108 |
+
pages_to_process = sorted(set(pages_to_process))
|
| 109 |
+
print(f"Total number of pages to process: {len(pages_to_process)}")
|
| 110 |
|
| 111 |
+
total_text = ''
|
| 112 |
+
for page_num in pages_to_process:
|
| 113 |
+
page_text = extract_text_from_page(pdf_path, page_num)
|
| 114 |
+
if page_text:
|
| 115 |
+
total_text += page_text
|
| 116 |
+
else:
|
| 117 |
+
print(f"No text extracted from page {page_num}.")
|
| 118 |
|
| 119 |
if total_text:
|
| 120 |
# Split into chunks
|