Spaces:
Sleeping
Sleeping
updated curriculum
Browse files- preprocess_curriculum.py +28 -19
- processed/chunks.json +0 -0
preprocess_curriculum.py
CHANGED
|
@@ -7,7 +7,7 @@ import nltk
|
|
| 7 |
import tiktoken
|
| 8 |
import json
|
| 9 |
|
| 10 |
-
nltk.download('
|
| 11 |
|
| 12 |
def extract_text_from_pdf(pdf_path, page_ranges):
|
| 13 |
text = ''
|
|
@@ -46,22 +46,30 @@ def extract_text_from_images(pdf_path, page_ranges):
|
|
| 46 |
pages_to_process = []
|
| 47 |
for start, end in page_ranges:
|
| 48 |
pages_to_process.extend(range(start, end + 1))
|
| 49 |
-
print(f"
|
| 50 |
pages_to_process = sorted(set(pages_to_process))
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
else:
|
| 62 |
-
print(f"
|
| 63 |
-
|
| 64 |
-
print(f"
|
| 65 |
return text
|
| 66 |
|
| 67 |
def split_into_chunks(text, max_tokens=512):
|
|
@@ -77,10 +85,11 @@ def split_into_chunks(text, max_tokens=512):
|
|
| 77 |
current_chunk = ''
|
| 78 |
current_chunk_tokens = 0
|
| 79 |
for sentence in sentences:
|
| 80 |
-
|
| 81 |
-
|
|
|
|
| 82 |
current_chunk += ' ' + sentence
|
| 83 |
-
current_chunk_tokens +=
|
| 84 |
else:
|
| 85 |
# Save the current chunk
|
| 86 |
chunks.append({
|
|
@@ -89,7 +98,7 @@ def split_into_chunks(text, max_tokens=512):
|
|
| 89 |
})
|
| 90 |
# Start a new chunk with the current sentence
|
| 91 |
current_chunk = sentence
|
| 92 |
-
current_chunk_tokens =
|
| 93 |
if current_chunk.strip():
|
| 94 |
# Add any remaining text as a chunk
|
| 95 |
chunks.append({
|
|
|
|
| 7 |
import tiktoken
|
| 8 |
import json
|
| 9 |
|
| 10 |
+
nltk.download('punkt')
|
| 11 |
|
| 12 |
def extract_text_from_pdf(pdf_path, page_ranges):
|
| 13 |
text = ''
|
|
|
|
| 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 |
+
images = convert_from_path(
|
| 57 |
+
pdf_path,
|
| 58 |
+
first_page=page_num,
|
| 59 |
+
last_page=page_num,
|
| 60 |
+
dpi=200 # Adjust DPI if needed
|
| 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"Failed to convert page {page_num} to image.")
|
| 71 |
+
except Exception as e:
|
| 72 |
+
print(f"Error processing page {page_num}: {e}")
|
| 73 |
return text
|
| 74 |
|
| 75 |
def split_into_chunks(text, max_tokens=512):
|
|
|
|
| 85 |
current_chunk = ''
|
| 86 |
current_chunk_tokens = 0
|
| 87 |
for sentence in sentences:
|
| 88 |
+
tokens = tokenizer.encode(sentence)
|
| 89 |
+
sentence_token_count = len(tokens)
|
| 90 |
+
if current_chunk_tokens + sentence_token_count <= max_tokens:
|
| 91 |
current_chunk += ' ' + sentence
|
| 92 |
+
current_chunk_tokens += sentence_token_count
|
| 93 |
else:
|
| 94 |
# Save the current chunk
|
| 95 |
chunks.append({
|
|
|
|
| 98 |
})
|
| 99 |
# Start a new chunk with the current sentence
|
| 100 |
current_chunk = sentence
|
| 101 |
+
current_chunk_tokens = sentence_token_count
|
| 102 |
if current_chunk.strip():
|
| 103 |
# Add any remaining text as a chunk
|
| 104 |
chunks.append({
|
processed/chunks.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|