Spaces:
Runtime error
Runtime error
Update preprocess_chunks.py
Browse files- preprocess_chunks.py +17 -25
preprocess_chunks.py
CHANGED
|
@@ -1,40 +1,32 @@
|
|
| 1 |
-
# preprocess_chunks.py
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
-
import orjson
|
| 5 |
from langchain.text_splitter import CharacterTextSplitter
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
# 1)
|
| 8 |
-
SRC_JSON = "research_methods_info.json"
|
| 9 |
-
|
| 10 |
-
OUT_DIR = "chunks"
|
| 11 |
-
|
| 12 |
-
# Klasörü yaratın (zaten varsa atla)
|
| 13 |
os.makedirs(OUT_DIR, exist_ok=True)
|
| 14 |
-
|
| 15 |
-
# JSON’u oku
|
| 16 |
with open(SRC_JSON, "rb") as f:
|
| 17 |
-
data = orjson.loads(f.read())
|
| 18 |
-
|
| 19 |
-
# Tokenizer/ayırıcıyı oluşturun
|
| 20 |
splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 21 |
-
|
| 22 |
-
# Her yöntem kaydını parçalayıp diske yaz
|
| 23 |
for idx, rec in enumerate(data.get("methods", [])):
|
| 24 |
-
# İstediğiniz alanları birleştirin
|
| 25 |
parts = [
|
| 26 |
f"Name:\n{rec.get('name','')}",
|
| 27 |
f"Description:\n{rec.get('description','')}",
|
| 28 |
-
|
| 29 |
-
# … dilediğiniz diğer bloklar …
|
| 30 |
]
|
| 31 |
text = "\n\n".join([p for p in parts if p.strip()])
|
| 32 |
-
|
| 33 |
-
# Metni chunk’lara bölün
|
| 34 |
chunks = splitter.split_text(text)
|
| 35 |
-
|
| 36 |
-
# Her bir chunk’ı ayrı dosya olarak kaydedin
|
| 37 |
for j, chunk in enumerate(chunks):
|
| 38 |
-
|
| 39 |
-
with open(out_path, "w", encoding="utf-8") as outf:
|
| 40 |
outf.write(chunk)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import orjson
|
| 3 |
from langchain.text_splitter import CharacterTextSplitter
|
| 4 |
+
from langchain.embeddings import OpenAIEmbeddings
|
| 5 |
+
from langchain.vectorstores import Chroma
|
| 6 |
+
from langchain.document_loaders import TextLoader
|
| 7 |
|
| 8 |
+
# 1) Chunk’ları oluşturma (mevcut kodunuz)
|
| 9 |
+
SRC_JSON = "research_methods_info.json"
|
| 10 |
+
OUT_DIR = "chunks"
|
|
|
|
|
|
|
|
|
|
| 11 |
os.makedirs(OUT_DIR, exist_ok=True)
|
|
|
|
|
|
|
| 12 |
with open(SRC_JSON, "rb") as f:
|
| 13 |
+
data = orjson.loads(f.read())
|
|
|
|
|
|
|
| 14 |
splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
|
|
|
|
|
|
| 15 |
for idx, rec in enumerate(data.get("methods", [])):
|
|
|
|
| 16 |
parts = [
|
| 17 |
f"Name:\n{rec.get('name','')}",
|
| 18 |
f"Description:\n{rec.get('description','')}",
|
| 19 |
+
# … diğer bloklar …
|
|
|
|
| 20 |
]
|
| 21 |
text = "\n\n".join([p for p in parts if p.strip()])
|
|
|
|
|
|
|
| 22 |
chunks = splitter.split_text(text)
|
|
|
|
|
|
|
| 23 |
for j, chunk in enumerate(chunks):
|
| 24 |
+
with open(f"{OUT_DIR}/{idx:03d}_{j:02d}.txt", "w", encoding="utf-8") as outf:
|
|
|
|
| 25 |
outf.write(chunk)
|
| 26 |
+
|
| 27 |
+
# 2) Embedding’leri oluşturup Chroma’ya kaydetme
|
| 28 |
+
print("⚙️ Generating embeddings and persisting to chromadb/ …")
|
| 29 |
+
docs = TextLoader(OUT_DIR).load()
|
| 30 |
+
db = Chroma.from_documents(docs, OpenAIEmbeddings(), persist_directory="chromadb")
|
| 31 |
+
db.persist()
|
| 32 |
+
print("✅ Done preprocessing and embedding.")
|