Spaces:
YZ03
/
Runtime error

YZ03 commited on
Commit
b25b520
·
verified ·
1 Parent(s): 775b885

Update pinecone_rag.py

Browse files
Files changed (1) hide show
  1. pinecone_rag.py +20 -18
pinecone_rag.py CHANGED
@@ -30,7 +30,7 @@ embedding_model = OpenAIEmbeddings(
30
 
31
  nlp = spacy.load("xx_sent_ud_sm") # For multilingual support including Arabic
32
 
33
- def sentence_overlap_chunks(text, chunk_size=900):
34
  doc = nlp(text)
35
  sentences = [sent.text.strip() for sent in doc.sents]
36
 
@@ -50,34 +50,36 @@ def sentence_overlap_chunks(text, chunk_size=900):
50
 
51
  # Join and store chunk
52
  if chunk:
53
- chunks.append(" ".join(chunk))
54
- i+=1
 
 
 
 
 
55
 
56
  # Overlap: start next chunk with last sentence of current chunk
57
- i = i - 1
58
 
59
  return chunks
60
 
61
 
62
- # Process each .txt file and upsert to separate namespace
63
  for filename in os.listdir(DATA_DIR):
64
- if filename.endswith(".txt"):
65
  filepath = os.path.join(DATA_DIR, filename)
66
  namespace = "ns4"
67
 
68
  print(f"Processing {filename} → namespace: {namespace}")
69
 
70
- # Detect encoding
71
- with open(filepath, "rb") as f:
72
- raw_data = f.read()
73
- encoding = chardet.detect(raw_data)['encoding']
74
-
75
- # Read file
76
- with open(filepath, "r", encoding=encoding) as f:
77
- content = f.read()
78
 
79
  # Wrap in Langchain doc
80
- #document = LangchainDocument(page_content=content, metadata={"source": filename})
81
  docs_processed = sentence_overlap_chunks(content)
82
 
83
  # Embed and prepare for upsert
@@ -85,7 +87,7 @@ for filename in os.listdir(DATA_DIR):
85
  for i, chunk in tqdm(enumerate(docs_processed), total=len(docs_processed), desc="Embedding chunks"):
86
  vector = embedding_model.embed_query(chunk)
87
  upsert_data.append({
88
- "id": f"{namespace}_chunk_{i}",
89
  "values": vector,
90
  "metadata": {
91
  "text": chunk,
@@ -98,7 +100,7 @@ for filename in os.listdir(DATA_DIR):
98
  index.upsert(vectors=upsert_data, namespace=namespace)
99
  print(f"✅ Done with {filename}\n")
100
 
101
- # Process each .txt file and upsert to separate namespace
102
  for filename in os.listdir(JOURNAL_DIR):
103
  if filename.endswith(".txt"):
104
  filepath = os.path.join(JOURNAL_DIR, filename)
@@ -134,4 +136,4 @@ for filename in os.listdir(JOURNAL_DIR):
134
  # Upsert to Pinecone under this file's namespace
135
  print(f"⬆️ Upserting {len(upsert_data)} vectors to namespace '{namespace}'...")
136
  index.upsert(vectors=upsert_data, namespace=namespace)
137
- print(f"✅ Done with {filename}\n")
 
30
 
31
  nlp = spacy.load("xx_sent_ud_sm") # For multilingual support including Arabic
32
 
33
+ def sentence_overlap_chunks(text, chunk_size=2000):
34
  doc = nlp(text)
35
  sentences = [sent.text.strip() for sent in doc.sents]
36
 
 
50
 
51
  # Join and store chunk
52
  if chunk:
53
+ try:
54
+ chunk = " ".join(chunk)
55
+ chunks.append(chunk)
56
+ i+=1
57
+ except:
58
+ print("can't process line.")
59
+ i+=4
60
 
61
  # Overlap: start next chunk with last sentence of current chunk
62
+ i = i - 3
63
 
64
  return chunks
65
 
66
 
67
+ # for data
68
  for filename in os.listdir(DATA_DIR):
69
+ if filename.endswith(".pdf"):
70
  filepath = os.path.join(DATA_DIR, filename)
71
  namespace = "ns4"
72
 
73
  print(f"Processing {filename} → namespace: {namespace}")
74
 
75
+ reader = fitz.open(filepath)
76
+ content = ""
77
+ for page in reader:
78
+ text = page.get_text()
79
+ if text:
80
+ content += text + "\n"
 
 
81
 
82
  # Wrap in Langchain doc
 
83
  docs_processed = sentence_overlap_chunks(content)
84
 
85
  # Embed and prepare for upsert
 
87
  for i, chunk in tqdm(enumerate(docs_processed), total=len(docs_processed), desc="Embedding chunks"):
88
  vector = embedding_model.embed_query(chunk)
89
  upsert_data.append({
90
+ "id": f"{filename[:-4]}_chunk_{i}",
91
  "values": vector,
92
  "metadata": {
93
  "text": chunk,
 
100
  index.upsert(vectors=upsert_data, namespace=namespace)
101
  print(f"✅ Done with {filename}\n")
102
 
103
+ # for journals
104
  for filename in os.listdir(JOURNAL_DIR):
105
  if filename.endswith(".txt"):
106
  filepath = os.path.join(JOURNAL_DIR, filename)
 
136
  # Upsert to Pinecone under this file's namespace
137
  print(f"⬆️ Upserting {len(upsert_data)} vectors to namespace '{namespace}'...")
138
  index.upsert(vectors=upsert_data, namespace=namespace)
139
+ print(f"✅ Done with {filename}\n")