-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
209 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,6 @@ __pycache__/ | |
*.spec | ||
dist/ | ||
build/ | ||
*.dtBase2/ | ||
*.egg-info/ | ||
examples/ | ||
docs/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import os | ||
import openai | ||
import sys | ||
import json | ||
import re | ||
|
||
sys.path.insert(0, '.') | ||
from pydt3 import DEVONthink3 | ||
|
||
dtp = DEVONthink3() | ||
def get_api_key(): | ||
result = dtp.search("name==__openai_api_key__") | ||
if result: | ||
api_key = result[0].plain_text | ||
else: | ||
response = dtp.display_dialog("Please enter your OpenAI API key", "") | ||
api_key = response["textReturned"] | ||
dtp.create_record_with({ | ||
"name": "__openai_api_key__", | ||
"type": "txt", | ||
"plain text": api_key, | ||
}) | ||
|
||
return api_key | ||
|
||
def generate_tags(content) -> list[str]: | ||
completion = openai.ChatCompletion.create( | ||
model="gpt-3.5-turbo", | ||
messages=[ | ||
{"role": "user", "content": f"Generate the tags for the following content. Tags should be concise and accurate and no more than 10. output the tags directly seperateted by ',':\n {content}"}, | ||
] | ||
) | ||
response = completion.choices[0]['message']['content'] | ||
print(response) | ||
return [tag.strip() for tag in response.split(",")] | ||
|
||
|
||
def add_tags_to_selected_records(): | ||
records = dtp.selected_records | ||
for recod in records: | ||
tags = generate_tags(recod.plain_text) | ||
recod.tags = tags | ||
|
||
|
||
if __name__ == '__main__': | ||
openai.api_key = get_api_key() | ||
add_tags_to_selected_records() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import os | ||
import openai | ||
import sys | ||
import json | ||
import re | ||
|
||
sys.path.insert(0, '.') | ||
from pydt3 import DEVONthink3 | ||
|
||
dtp = DEVONthink3() | ||
def get_api_key(): | ||
result = dtp.search("name==__openai_api_key__") | ||
if result: | ||
api_key = result[0].plain_text | ||
else: | ||
response = dtp.display_dialog("Please enter your OpenAI API key", "") | ||
api_key = response["textReturned"] | ||
dtp.create_record_with({ | ||
"name": "__openai_api_key__", | ||
"type": "txt", | ||
"plain text": api_key, | ||
}) | ||
|
||
return api_key | ||
|
||
|
||
def expand_content(content) -> str: | ||
system = '''You are a skillful writer. Replace the angle brackets you see with the content you generated. Outputs should follow the hints in angle brackets. \n | ||
eg. | ||
User: "Today I watched a famous British movie <<Movie Name>>. Its's written by <<Author>>. It's about <<Plot>>. I like it very much. | ||
AI: {"Movie Name": "The Godfather.", "Author": "Mario Puzo", "Plot": "a mafia family"} | ||
"''' | ||
|
||
completion = openai.ChatCompletion.create( | ||
model="gpt-3.5-turbo", | ||
messages=[ | ||
{"role": "user", "content": system,}, | ||
{"role": "user", "content": content,}, | ||
{"role": "assistant", "content": "Okay, I'll gie the answer in json format.",}, | ||
|
||
] | ||
) | ||
print(completion.choices) | ||
response = completion.choices[0]['message']['content'] | ||
results = json.loads(response) | ||
print("=======", results) | ||
for key in results: | ||
content = re.sub(f'<<{key}>>', f'=={results[key]}==', content, count=1) | ||
return content | ||
|
||
def expand_current_record(): | ||
retry_count = 3 | ||
record = dtp.think_windows[0].content_record | ||
|
||
for _ in range(retry_count): | ||
try: | ||
record.plain_text = expand_content(record.plain_text) | ||
break | ||
except json.decoder.JSONDecodeError as e: | ||
print(e) | ||
print("retrying...") | ||
continue | ||
|
||
if __name__ == '__main__': | ||
openai.api_key = get_api_key() | ||
expand_current_record() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pydt3==0.0.1 | ||
pyobjc-core==9.1.1 | ||
pyobjc-framework-AppleScriptKit==9.1.1 | ||
pyobjc-framework-AppleScriptObjC==9.1.1 | ||
openai==0.27.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import jieba | ||
from wordcloud import WordCloud | ||
import random | ||
import matplotlib.pyplot as plt | ||
|
||
from pydt3 import DEVONthink3 | ||
|
||
ignore_words = """function return var value if else for while break continue switch case default element object key array https component"""\ | ||
.split() | ||
|
||
def generate_wordcloud(text, output_file='wordcloud.png'): | ||
# 对文本进行分词 | ||
word_list = list(jieba.cut(text)) | ||
text = '' | ||
for word in word_list: | ||
if len(word) <= 2 or word in ignore_words: | ||
continue | ||
text += word + ' ' | ||
|
||
# 创建词云对象 | ||
wc = WordCloud( | ||
background_color='white', | ||
width=800, | ||
height=600, | ||
max_words=200, | ||
max_font_size=100, | ||
random_state=42 | ||
) | ||
|
||
wc.generate(text) | ||
|
||
wc.to_file(output_file) | ||
|
||
plt.imshow(wc, interpolation='bilinear') | ||
plt.axis('off') | ||
plt.show() | ||
|
||
if __name__ == '__main__': | ||
texts = [] | ||
dtp3 = DEVONthink3() | ||
db = dtp3.ext.db_by_name('blue-book') | ||
contents = db.contents | ||
print(len(contents)) | ||
sampled_records = random.sample(db.contents, min(40, len(db.contents))) | ||
names = [] | ||
texts = [] | ||
for record in sampled_records: | ||
if record.type == 'picture': | ||
continue | ||
if 'newsletter' in record.location: | ||
continue | ||
name = record.name | ||
names.append(name) | ||
texts.append(name) | ||
texts.append(record.rich_text.splitlines()[0]) | ||
|
||
samples = texts | ||
generate_wordcloud(' '.join(samples), 'wordcloud.png') |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.