Spaces:
Paused
Paused
Create backend/codebase_index.py
Browse files
backend/codebase_index.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import os, faiss
|
| 2 |
from pathlib import Path
|
| 3 |
from typing import List, Dict
|
| 4 |
from sentence_transformers import SentenceTransformer
|
|
@@ -13,17 +13,14 @@ class CodebaseIndex:
|
|
| 13 |
self.chunks = []
|
| 14 |
|
| 15 |
def build_index(self):
|
| 16 |
-
# Walk all code files
|
| 17 |
exts = {'.py', '.js', '.ts', '.tsx', '.jsx', '.go', '.rs', '.java', '.cpp', '.c', '.h'}
|
| 18 |
for filepath in self.project_path.rglob('*'):
|
| 19 |
if filepath.suffix in exts and filepath.is_file():
|
| 20 |
self.file_list.append(str(filepath.relative_to(self.project_path)))
|
| 21 |
content = filepath.read_text()
|
| 22 |
-
# Split into chunks (by functions/paragraphs)
|
| 23 |
chunks = self._split_into_chunks(content)
|
| 24 |
for chunk in chunks:
|
| 25 |
self.chunks.append((str(filepath.relative_to(self.project_path)), chunk))
|
| 26 |
-
# Encode and build FAISS
|
| 27 |
texts = [chunk[1] for chunk in self.chunks]
|
| 28 |
embeddings = self.model.encode(texts, show_progress_bar=False)
|
| 29 |
dim = embeddings.shape[1]
|
|
@@ -43,7 +40,6 @@ class CodebaseIndex:
|
|
| 43 |
return results
|
| 44 |
|
| 45 |
def _split_into_chunks(self, text, max_chars=1000):
|
| 46 |
-
# Simple split by double newlines, with overlap
|
| 47 |
paragraphs = text.split('\n\n')
|
| 48 |
chunks = []
|
| 49 |
current = ""
|
|
|
|
| 1 |
+
import os, faiss
|
| 2 |
from pathlib import Path
|
| 3 |
from typing import List, Dict
|
| 4 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 13 |
self.chunks = []
|
| 14 |
|
| 15 |
def build_index(self):
|
|
|
|
| 16 |
exts = {'.py', '.js', '.ts', '.tsx', '.jsx', '.go', '.rs', '.java', '.cpp', '.c', '.h'}
|
| 17 |
for filepath in self.project_path.rglob('*'):
|
| 18 |
if filepath.suffix in exts and filepath.is_file():
|
| 19 |
self.file_list.append(str(filepath.relative_to(self.project_path)))
|
| 20 |
content = filepath.read_text()
|
|
|
|
| 21 |
chunks = self._split_into_chunks(content)
|
| 22 |
for chunk in chunks:
|
| 23 |
self.chunks.append((str(filepath.relative_to(self.project_path)), chunk))
|
|
|
|
| 24 |
texts = [chunk[1] for chunk in self.chunks]
|
| 25 |
embeddings = self.model.encode(texts, show_progress_bar=False)
|
| 26 |
dim = embeddings.shape[1]
|
|
|
|
| 40 |
return results
|
| 41 |
|
| 42 |
def _split_into_chunks(self, text, max_chars=1000):
|
|
|
|
| 43 |
paragraphs = text.split('\n\n')
|
| 44 |
chunks = []
|
| 45 |
current = ""
|