Upload 3 files
Browse files- .gitattributes +2 -0
- HD_Brochure_2025_26.pdf +3 -0
- os.pdf +3 -0
- simplerag.py +145 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
HD_Brochure_2025_26.pdf filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
os.pdf filter=lfs diff=lfs merge=lfs -text
|
HD_Brochure_2025_26.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:71dc91142fe5e1f30a2a8b4ccd5bea7d97448b0814cf767ef2438d455b63cda0
|
| 3 |
+
size 1178289
|
os.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0e3c71200900a821ada81ebb20df8347d3fb5a49017a29b6d62962927e231bc2
|
| 3 |
+
size 13070525
|
simplerag.py
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
from langchain_community.document_loaders import PyMuPDFLoader
|
| 4 |
+
from pinecone import Pinecone
|
| 5 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 6 |
+
from sentence_transformers import SentenceTransformer
|
| 7 |
+
import ollama
|
| 8 |
+
import numpy as np
|
| 9 |
+
from tqdm import tqdm
|
| 10 |
+
import hashlib
|
| 11 |
+
|
| 12 |
+
# PDF_FILE = "DeepSeekR1.pdf"
|
| 13 |
+
PDF_FILE = "os.pdf"
|
| 14 |
+
# PDF_FILE = "HD_Brochure_2025_26.pdf"
|
| 15 |
+
PINECONE_API_KEY = "pcsk_71Vjnk_5MBhhy4u8VajTtHBoJTCULFf2D2FxzssiGgsEqWheg9fAx1nxXFWCynRQtUx384"
|
| 16 |
+
INDEX_NAME = "chatdatabase"
|
| 17 |
+
|
| 18 |
+
# Initialize connections
|
| 19 |
+
pc = Pinecone(api_key=PINECONE_API_KEY)
|
| 20 |
+
index = pc.Index(INDEX_NAME)
|
| 21 |
+
embedder = SentenceTransformer("thenlper/gte-large")
|
| 22 |
+
|
| 23 |
+
def get_pdf_hash(file_path):
|
| 24 |
+
"""Get MD5 hash of PDF file"""
|
| 25 |
+
with open(file_path, 'rb') as f:
|
| 26 |
+
return hashlib.md5(f.read()).hexdigest()[:12] # Use first 12 chars
|
| 27 |
+
|
| 28 |
+
def check_embeddings_exist(pdf_hash):
|
| 29 |
+
try:
|
| 30 |
+
dummy_vector = [0.0] * 1024 # gte-large dimension
|
| 31 |
+
results = index.query(
|
| 32 |
+
vector=dummy_vector,
|
| 33 |
+
filter={"pdf_hash": pdf_hash},
|
| 34 |
+
top_k=1
|
| 35 |
+
)
|
| 36 |
+
return len(results.matches) > 0
|
| 37 |
+
except:
|
| 38 |
+
return False
|
| 39 |
+
|
| 40 |
+
def create_chunks(file_path):
|
| 41 |
+
"""Load PDF and create chunks"""
|
| 42 |
+
print(f"π Loading PDF: {file_path}")
|
| 43 |
+
loader = PyMuPDFLoader(file_path)
|
| 44 |
+
docs = loader.load()
|
| 45 |
+
|
| 46 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
|
| 47 |
+
chunks = splitter.split_documents(docs)
|
| 48 |
+
|
| 49 |
+
print(f"Created {len(chunks)} chunks from {len(docs)} pages")
|
| 50 |
+
return chunks
|
| 51 |
+
|
| 52 |
+
def create_and_store_embeddings(chunks, pdf_hash):
|
| 53 |
+
"""Create embeddings and store in Pinecone"""
|
| 54 |
+
print("π Creating embeddings...")
|
| 55 |
+
|
| 56 |
+
vectors = []
|
| 57 |
+
for i, chunk in enumerate(tqdm(chunks, desc="Processing chunks")):
|
| 58 |
+
# Create embedding
|
| 59 |
+
embedding = embedder.encode(chunk.page_content)
|
| 60 |
+
|
| 61 |
+
# Prepare vector for Pinecone
|
| 62 |
+
vectors.append({
|
| 63 |
+
"id": f"{pdf_hash}_{i}",
|
| 64 |
+
"values": embedding.tolist(),
|
| 65 |
+
"metadata": {
|
| 66 |
+
"text": chunk.page_content,
|
| 67 |
+
"pdf_hash": pdf_hash,
|
| 68 |
+
"chunk_id": i,
|
| 69 |
+
"page": chunk.metadata.get('page', 0)
|
| 70 |
+
}
|
| 71 |
+
})
|
| 72 |
+
|
| 73 |
+
# Upload to Pinecone in batches
|
| 74 |
+
print("β¬οΈ Uploading to Pinecone...")
|
| 75 |
+
BATCH_SIZE = 100
|
| 76 |
+
for i in range(0, len(vectors), BATCH_SIZE):
|
| 77 |
+
batch = vectors[i:i+BATCH_SIZE]
|
| 78 |
+
index.upsert(vectors=batch)
|
| 79 |
+
print(f"Uploaded batch {i//BATCH_SIZE + 1}/{(len(vectors)-1)//BATCH_SIZE + 1}")
|
| 80 |
+
|
| 81 |
+
print(f"β
Stored {len(vectors)} embeddings")
|
| 82 |
+
|
| 83 |
+
def query_system(question, top_k=5):
|
| 84 |
+
"""Query the system and get answer"""
|
| 85 |
+
print(f"\nπ Question: {question}")
|
| 86 |
+
|
| 87 |
+
# Create embedding for question
|
| 88 |
+
question_embedding = embedder.encode(question)
|
| 89 |
+
|
| 90 |
+
# Search Pinecone
|
| 91 |
+
results = index.query(
|
| 92 |
+
vector=question_embedding.tolist(),
|
| 93 |
+
top_k=top_k,
|
| 94 |
+
include_metadata=True
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
if not results.matches:
|
| 98 |
+
return "No relevant information found."
|
| 99 |
+
|
| 100 |
+
context = "\n\n".join([match.metadata['text'] for match in results.matches])
|
| 101 |
+
|
| 102 |
+
prompt = f"""Based on the following context, answer the question. If the answer is not in the context, say so.
|
| 103 |
+
|
| 104 |
+
Context:
|
| 105 |
+
{context}
|
| 106 |
+
|
| 107 |
+
Question: {question}
|
| 108 |
+
|
| 109 |
+
Answer:"""
|
| 110 |
+
|
| 111 |
+
print("π€ Generating answer...")
|
| 112 |
+
try:
|
| 113 |
+
response = ollama.generate(model='llama3:latest', prompt=prompt)
|
| 114 |
+
answer = response['response']
|
| 115 |
+
|
| 116 |
+
print(f"π― Answer: {answer}")
|
| 117 |
+
|
| 118 |
+
return answer
|
| 119 |
+
except Exception as e:
|
| 120 |
+
print(f"β Error generating answer: {e}")
|
| 121 |
+
return "Error generating answer. Check if Ollama is running."
|
| 122 |
+
|
| 123 |
+
def main():
|
| 124 |
+
pdf_hash = get_pdf_hash(PDF_FILE)
|
| 125 |
+
print(f"π PDF Hash: {pdf_hash}")
|
| 126 |
+
|
| 127 |
+
if check_embeddings_exist(pdf_hash):
|
| 128 |
+
print("β
Embeddings already exist, skipping creation")
|
| 129 |
+
else:
|
| 130 |
+
print("π Creating new embeddings...")
|
| 131 |
+
chunks = create_chunks(PDF_FILE)
|
| 132 |
+
create_and_store_embeddings(chunks, pdf_hash)
|
| 133 |
+
|
| 134 |
+
print("\nπ System ready! Ask your questions (type 'quit' to exit)")
|
| 135 |
+
while True:
|
| 136 |
+
question = input("\nβ Your question: ").strip()
|
| 137 |
+
|
| 138 |
+
if question.lower() in ['quit', 'exit', 'q']:
|
| 139 |
+
break
|
| 140 |
+
|
| 141 |
+
if question:
|
| 142 |
+
query_system(question)
|
| 143 |
+
|
| 144 |
+
if __name__ == "__main__":
|
| 145 |
+
main()
|