-
Notifications
You must be signed in to change notification settings - Fork 2
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
9 changed files
with
940 additions
and
499 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 |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
/.vscode | ||
/**/__pycache__ | ||
/.direnv | ||
.pre-commit-config.yaml |
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 |
---|---|---|
@@ -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 | ||
|
@@ -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: | ||
|
@@ -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) |
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 |
---|---|---|
@@ -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"): | ||
|
@@ -57,5 +50,6 @@ def get_posts_path(): | |
paths.append(file_path) | ||
return paths | ||
|
||
if __name__ == '__main__': | ||
|
||
if __name__ == "__main__": | ||
main() |
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 |
---|---|---|
@@ -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", | ||
) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.