-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjee_main.py
More file actions
36 lines (24 loc) · 843 Bytes
/
Copy pathjee_main.py
File metadata and controls
36 lines (24 loc) · 843 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import fitz # PyMuPDF
pdf_path="D:\Professional\AI_ML\Embeddings\jee_mains.pdf"
def extract_text_from_pdf(pdf_path):
doc = fitz.open(pdf_path)
text = ""
for page in doc:
text += page.get_text()
doc.close()
return text
pdf_text = extract_text_from_pdf("jee_mains.pdf")
print(pdf_text[:5000]) # preview
#
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("all-MiniLM-L6-v2") # Small and fast
# Optional: split large text into chunks
def split_into_chunks(text, chunk_size=500):
words = text.split()
for i in range(0, len(words), chunk_size):
yield " ".join(words[i:i+chunk_size])
chunks = list(split_into_chunks(pdf_text, chunk_size=500))
# Generate embeddings
embeddings = model.encode(chunks)
print("The embedding for the text is:\n")
print(embeddings)