Skip to content

Commit

Permalink
perf: update flake.nix and packages
Browse files Browse the repository at this point in the history
  • Loading branch information
Moraxyc committed May 18, 2024
1 parent 10f4b01 commit 71055b0
Show file tree
Hide file tree
Showing 9 changed files with 940 additions and 499 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/.vscode
/**/__pycache__
/.direnv
.pre-commit-config.yaml
899 changes: 617 additions & 282 deletions LICENSE

Large diffs are not rendered by default.

21 changes: 5 additions & 16 deletions app/dataProcess.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
'''
Author: Moraxyc [email protected]
Date: 2023-08-16 01:55:38
LastEditors: Moraxyc [email protected]
LastEditTime: 2023-08-16 11:50:00
FilePath: /ai-summary-hugo/dataProcess.py
Description: 处理summary.json数据
Copyright (c) 2023 by Moraxyc, All Rights Reserved.
'''
import json
import os
import shutil
Expand All @@ -25,16 +15,15 @@ def __init__(self, file_path):

def load_json(self):
if os.path.exists(self.file_path):
with open(self.file_path, 'r') as json_file:
with open(self.file_path, "r") as json_file:
data = json.load(json_file)
return data if self.validate_json_structure else False
else:
try:
print("summary.json不存在, 初始化中")
os.makedirs(self.file_path.rstrip(
'summary.json'), exist_ok=True)
os.makedirs(self.file_path.rstrip("summary.json"), exist_ok=True)
shutil.copy("./summary.json", self.file_path)
with open(self.file_path, 'r') as json_file:
with open(self.file_path, "r") as json_file:
data = json.load(json_file)
return data
except FileNotFoundError:
Expand Down Expand Up @@ -83,10 +72,10 @@ def edit_json_by_slug(self, target_slug, new_summary, new_state):
return None

def save_json(self):
with open(self.file_path, 'w') as json_file:
with open(self.file_path, "w") as json_file:
json.dump(self.data, json_file, indent=4, ensure_ascii=False)

def add_new_summary(self, new_summary):
if "summaries" not in self.data:
self.data["summaries"] = []
self.data["summaries"].append(new_summary)
self.data["summaries"].append(new_summary)
30 changes: 12 additions & 18 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,46 @@
'''
Author: Moraxyc [email protected]
Date: 2023-08-16 00:40:10
LastEditors: Moraxyc [email protected]
LastEditTime: 2023-08-16 12:01:11
FilePath: /blog-hugo/ai-summary-hugo/main.py
Description: 脚本运行入口
Copyright (c) 2023 by Moraxyc, All Rights Reserved.
'''
import os
import sys
import frontmatter
from .dataProcess import dataProcess
from .requestSummary import generate_summary


def main():
data_process = dataProcess("../data/summary/summary.json")
posts_path = get_posts_path()

for post_path in posts_path:
post = frontmatter.load(post_path)
slug = post['slug']
slug = post["slug"]

if data_process.check_slug_exists(slug):
json_data = data_process.get_json_by_slug(slug)
if not json_data['generated']:
print("Generating summary for" + post['title'])
if not json_data["generated"]:
print("Generating summary for" + post["title"])
summary_content = generate_summary(post.content)
return_status = True if summary_content else False
data_process.edit_json_by_slug(slug, summary_content, return_status)
data_process.save_json()
else:
print("文章已生成总结: " + post["title"])
else:
print("为文章生成总结: " + post['title'])
print("为文章生成总结: " + post["title"])
summary_content = generate_summary(post.content)
return_status = True if summary_content else False
if not return_status:print("为文章生成总结失败: " + post['title'])
if not return_status:
print("为文章生成总结失败: " + post["title"])
new_summary = {
"title": post['title'],
"title": post["title"],
"slug": slug,
"generated": return_status,
"summary": summary_content
"summary": summary_content,
}
data_process.add_new_summary(new_summary)
data_process.save_json()

sys.exit(0)


def get_posts_path():
paths = []
for root, _, files in os.walk("../content/posts"):
Expand All @@ -57,5 +50,6 @@ def get_posts_path():
paths.append(file_path)
return paths

if __name__ == '__main__':

if __name__ == "__main__":
main()
18 changes: 6 additions & 12 deletions app/requestSummary.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
'''
Author: Moraxyc [email protected]
Date: 2023-08-16 02:32:59
LastEditors: Moraxyc [email protected]
LastEditTime: 2023-08-16 11:50:06
FilePath: /ai-summary-hugo/requestSummary.py
Description: 通过API向chatgpt请求文章总结
Copyright (c) 2023 by Moraxyc, All Rights Reserved.
'''
import openai
from openai import OpenAI
import os


def generate_summary(prompt):
try:
client = OpenAI(
api_key=os.environ.get("OPENAI_API_KEY"),
)
response = client.chat.completions.create(
messages=[
{"role": "system", "content": "请在100字内用中文总结以下文章的核心内容: "},
{"role": "user", "content": prompt}
{
"role": "system",
"content": "请在100字内用中文总结以下文章的核心内容: ",
},
{"role": "user", "content": prompt},
],
model="gpt-3.5-turbo",
)
Expand Down
154 changes: 114 additions & 40 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 71055b0

Please sign in to comment.