-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_tag_notes.py
189 lines (152 loc) · 6.47 KB
/
auto_tag_notes.py
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
auto_tag_notes.py - Automatically generates tags for Obsidian notes using OpenAI
This script reads each note in an Obsidian vault, uses the OpenAI API to generate
appropriate tags based on the content, and updates or adds a #tags section to each note.
Features include filtering directories, preserving existing tags, and debugging information.
Author: Jonathan Care <jonc@lacunae.org>
"""
import os, re
from openai import OpenAI
from dotenv import load_dotenv
from tqdm import tqdm
import traceback
import utils
import signal_handler
load_dotenv()
# Initialize the OpenAI client
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
VAULT_PATH = os.getenv("OBSIDIAN_VAULT_PATH", "/Users/jonc/Obsidian/Jonathans Brain")
print(f"Using vault path: {VAULT_PATH}")
TAG_PROMPT = """
You are an AI assistant tasked with assigning 3-5 concise, relevant hashtags to the following note. Provide tags in lowercase without spaces (e.g., #projectmanagement, #python).
The note already has these existing tags: {existing_tags}
Please suggest additional relevant tags that DON'T duplicate the existing ones.
Note:
{note_content}
New Tags (don't include existing ones):
"""
def load_notes(vault_path):
notes = {}
count = 0
skipped = 0
print(f"Loading notes from {vault_path}")
for root, dirs, files in os.walk(vault_path):
# Skip venv directory
dirs[:] = [d for d in dirs if d != "venv"]
for file in files:
if file.endswith(".md"):
path = os.path.join(root, file)
try:
with open(path, "r", encoding="utf-8") as f:
content = f.read()
notes[path] = content # Store full path as key instead of just filename
count += 1
except Exception as e:
print(f"Error reading file {path}: {str(e)}")
skipped += 1
print(f"Loaded {count} notes, skipped {skipped} due to errors")
return notes
def generate_tags_for_note(note_content, existing_tags):
"""Generate new tags for a note, taking into account existing tags."""
existing_tags_str = ", ".join(existing_tags) if existing_tags else "none"
try:
response = client.chat.completions.create(
model="gpt-4-turbo-preview",
messages=[
{"role": "user", "content": TAG_PROMPT.format(
existing_tags=existing_tags_str,
note_content=note_content[:2000]
)}
],
temperature=0.2,
)
new_tags = response.choices[0].message.content.strip()
# Ensure new tags have the # prefix and are unique
formatted_tags = []
for tag in new_tags.split():
if not tag.startswith('#'):
tag = f'#{tag}'
formatted_tags.append(tag)
# Deduplicate against existing tags
return [tag for tag in formatted_tags
if not any(existing.lower() == tag.lower() for existing in existing_tags)]
except Exception as e:
print(f"Error generating tags: {str(e)}")
return ["#error"]
def insert_tags(notes):
updated = 0
skipped = 0
for path in tqdm(notes, desc="Generating tags"):
content = notes[path]
file_name = os.path.basename(path)
try:
# Extract existing tags using the utility function
existing_tags = utils.extract_existing_tags(content)
print(f"Found {len(existing_tags)} existing tags in {file_name}")
# Generate new tags
new_tags = generate_tags_for_note(content, existing_tags)
if not new_tags:
print(f"No new tags generated for {file_name}, keeping existing tags")
continue
print(f"Generated {len(new_tags)} new tags for {file_name}")
# Combine all tags and deduplicate
all_tags = utils.deduplicate_tags(existing_tags + new_tags)
# Format all tags for the #tags section
tags_text = " ".join(all_tags)
if "#tags:" not in content.lower():
print(f"Adding new tags section to {file_name}")
tag_section = f"\n\n#tags: {tags_text}\n"
notes[path] = content + tag_section
updated += 1
else:
print(f"Updating existing tags section in {file_name}")
notes[path] = re.sub(r"#tags:.*?(\n\n|\n$|$)", f"#tags: {tags_text}\n", content, flags=re.DOTALL)
updated += 1
except Exception as e:
print(f"Error processing {file_name}: {str(e)}")
traceback.print_exc()
skipped += 1
print(f"Updated {updated} notes, skipped {skipped} due to errors")
return updated
def save_notes(notes, vault_path):
saved = 0
failed = 0
for path, content in notes.items():
try:
with open(path, "w", encoding="utf-8") as f:
f.write(content)
saved += 1
except Exception as e:
print(f"Error writing to file {path}: {str(e)}")
failed += 1
print(f"Saved {saved} notes, failed to save {failed} notes")
return saved
def cleanup_before_exit():
"""Clean up resources before exiting."""
print("Performing cleanup before exit...")
print("Auto-tagging tool interrupted. No files have been modified.")
print("Cleanup completed. Goodbye!")
if __name__ == "__main__":
try:
# Set up clean interrupt handling
signal_handler.setup_interrupt_handling()
# Register cleanup function
signal_handler.register_cleanup_function(cleanup_before_exit)
notes = load_notes(VAULT_PATH)
if not notes:
print("No notes found! Check the vault path.")
exit(1)
updated = insert_tags(notes)
if updated == 0:
print("No notes were updated. Check the tagging logic.")
exit(1)
saved = save_notes(notes, VAULT_PATH)
if saved > 0:
print(f"✅ Auto-tagging completed! Updated and saved {saved} notes.")
else:
print("❌ No notes were saved. Check file permissions.")
except Exception as e:
print(f"❌ Error during execution: {str(e)}")
traceback.print_exc()