Repository navigation
Expand file tree
/
Copy pathgenerate.py
More file actions
167 lines (138 loc) · 5.16 KB
/
Copy pathgenerate.py
File metadata and controls
167 lines (138 loc) · 5.16 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import json
import re
import subprocess
from pathlib import Path
import torch
from optimum.onnxruntime import ORTModelForFeatureExtraction
from pygments.lexer import Lexer
from pygments.lexers import get_lexer_for_filename
from pygments.token import Text
from pygments.util import ClassNotFound
from tqdm import tqdm
from transformers import AutoTokenizer
CLEAN = re.compile(r"\\[a-zA-Z]+\{.*?\}|\\[a-zA-Z]+")
def get_pdf_page_for_line(source_path: Path, line: int):
cmd = [
"synctex", "view",
"-i", f"{line}:0:{str(source_path.resolve())}",
"-o", str(source_path.with_suffix(".pdf").resolve())
]
try:
output = subprocess.check_output(cmd, universal_newlines=True)
for out in output.splitlines():
if out.strip().startswith("Page:"):
return int(out.strip().split(":")[1])
except Exception as e:
print(f"Error running synctex: {e}")
return None
def chunk_text(text: str, n: int):
chunk = []
lines = text.splitlines()
for i, line in enumerate(lines):
words = line.split()
take = n - len(words)
chunk.extend(words[:take])
if len(chunk) >= n:
yield " ".join(chunk), i
chunk = []
if chunk:
yield " ".join(chunk), len(lines) - 1
def lexer_latex_check(lexer: Lexer) -> bool:
return lexer.name == "TeX"
def get_plaintext_tokens(text: str, lexer: Lexer):
math_content: str | None = None
latex_mode = lexer_latex_check(lexer)
for type, token in lexer.get_tokens(text):
if (token == "$$" or token == "$") and latex_mode:
if math_content is not None:
parsed = f"${math_content.strip()}$"
math_content = None
yield parsed
else:
math_content = ""
continue
if math_content is not None:
math_content += token
continue
if type is Text:
yield token
def extract_plain_text(text: str, lexer: Lexer) -> str:
tokens = get_plaintext_tokens(text, lexer)
plain_text = "".join(str(token) for token in tokens)
return plain_text
def get_embedding(
text: str, model: ORTModelForFeatureExtraction, tokenizer: AutoTokenizer
):
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
with torch.no_grad():
outputs = model(**inputs)
embedding = outputs.last_hidden_state.mean(dim=1).squeeze().cpu().numpy()
return embedding
def main():
import argparse
parser = argparse.ArgumentParser(description="Generate embeddings for files")
parser.add_argument("files", nargs="+", help="List of files to process")
parser.add_argument(
"--n", type=int, default=20, help="Chunk width (words per chunk)"
)
parser.add_argument(
"--model", default="./static/model", help="Sentence-transformers model name"
)
parser.add_argument(
"--outfile", default="./static/embeddings.json", help="Output JSON file"
)
parser.add_argument("--raw", action="store_true", help="Output raw text chunks")
parser.add_argument(
"--strip_paths",
type=int,
default=0,
help="Strip n levels of paths from file names",
)
parser.add_argument("--path_suffix", help="Suffix to add to file paths")
args = parser.parse_args()
model = ORTModelForFeatureExtraction.from_pretrained(args.model)
tokenizer = AutoTokenizer.from_pretrained(args.model)
result = []
for fname in tqdm(args.files, desc="Processing files"):
fname = Path(fname)
with open(fname, "r", encoding="utf-8") as f:
contents = f.read()
latex_mode = False
if not args.raw:
try:
lexer = get_lexer_for_filename(fname.name)
latex_mode = lexer_latex_check(lexer)
contents = extract_plain_text(contents, lexer)
except ClassNotFound as e:
print(f"Warning: {e}")
contents = CLEAN.sub("", contents)
chunks = tuple(chunk_text(contents, args.n))
if not chunks:
continue
for (chunk, line), embedding in zip(
chunks, (get_embedding(chunk, model, tokenizer) for chunk, _ in chunks)
):
path = Path(*fname.parts[args.strip_paths :])
if args.path_suffix:
path = path.with_suffix(args.path_suffix)
if latex_mode:
synctex_name = fname.with_suffix(".synctex.gz")
if synctex_name.exists():
pdf_page = get_pdf_page_for_line(fname, line)
if pdf_page is not None:
path = path.with_suffix(f".pdf#page={pdf_page}")
result.append(
{
"file": fname.stem,
"chunk": chunk,
"embedding": embedding.tolist(),
"line": line,
"path": str(path),
}
)
# Save to JSON
with open(args.outfile, "w", encoding="utf-8") as f:
json.dump(result, f, indent=2)
print(f"Saved {len(result)} embeddings to {args.outfile}")
if __name__ == "__main__":
main()