-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract-title.py
More file actions
executable file
·65 lines (48 loc) · 1.67 KB
/
extract-title.py
File metadata and controls
executable file
·65 lines (48 loc) · 1.67 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
#!/Users/pas/development/advanced-stack/experiments/reading-notes/venv/bin/python3
# -*- coding: utf-8 -*-
import argparse
import os
from dataclasses import dataclass
from llm_core.parsers import LLaMACPPParser
from unstructured.partition.auto import partition
UNABLE_TO_PARSE = "unable-to-parse"
@dataclass
class Document:
title: str
def extract_content(filename):
try:
elements = partition(
filename=filename,
strategy="auto",
)
except ValueError:
print(f"Skipping {filename}")
content = UNABLE_TO_PARSE
content = "\n\n".join(str(e) for e in elements)
return content
def main():
parser = argparse.ArgumentParser(description="Extract title from document")
parser.add_argument("filename", type=str)
parser.add_argument(
"--model_name",
type=str,
default="mistral-7b-instruct-v0.1.Q4_K_M.gguf",
help="The model file in ~/.cache/py-llm-core/models.",
)
args = parser.parse_args()
content = extract_content(args.filename)
with LLaMACPPParser(Document, args.model_name) as parser:
document = parser.parse(content[:1000])
if document.title == UNABLE_TO_PARSE:
return
# Get the original file extension
_, extension = os.path.splitext(args.filename)
# Get the original file directory
directory = os.path.dirname(args.filename)
# Generate the new file name using the document title and original extension
new_filename = f"{directory}/{document.title.title()}{extension}"
# Rename the file
os.rename(args.filename, new_filename)
print(f"File renamed to: {new_filename}")
if __name__ == "__main__":
main()