Skip to content

Commit 6656d81

Browse files
committed
added functionality to obtain the openai_key from environment variables, and also fixed some minor bugs that caused certain variables to not be properly retrieved and result in errors.
1 parent 69e7d5a commit 6656d81

File tree

7 files changed

+45
-30
lines changed

7 files changed

+45
-30
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ python3 app.py
223223

224224
访问 http://127.0.0.1:5000/ 后,您将看到主页。在主页上,您可以点击不同的链接来调用各种服务。您可以通过修改链接中的参数值来实现不同的效果。有关参数详细信息,请参阅上一步骤中的详细介绍
225225

226-
![image-20230331042557655](/Users/jessytsui/Library/Application Support/typora-user-images/image-20230331042557655.png)
226+
![flask主界面](./images/flask_web_home.png)
227227

228228

229229

app.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ def home():
2929
reviewer_url = url_for("reviewer", _external=True, paper_path="", file_format="txt",
3030
research_fields="computer science, artificial intelligence and reinforcement learning",
3131
language="en")
32-
3332
return f'''
34-
<h1>Flask 版本的优势</h1>
33+
<h1>ChatPaper,Flask版本的优势</h1>
34+
<p>GitHub 项目地址:<a href="https://github.com/kaixindelele/ChatPaper" target="_blank">https://github.com/kaixindelele/ChatPaper</a></p>
3535
<p>将原始的 Python 脚本改为使用 Flask 构建的 Web 服务具有以下优点:</p>
3636
<ul>
3737
<li><strong>易用性</strong>:通过简单的 HTTP 请求,用户可以轻松访问和使用各个功能,无需在本地安装 Python 或其他依赖。</li>

chat_arxiv.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,13 @@ def __init__(self, key_word, query,
318318
self.config = configparser.ConfigParser()
319319
# 读取配置文件
320320
self.config.read('apikey.ini')
321+
OPENAI_KEY = os.environ.get("OPENAI_KEY", "")
321322
# 获取某个键对应的值
322323
self.chat_api_list = self.config.get('OpenAI', 'OPENAI_API_KEYS')[1:-1].replace('\'', '').split(',')
323-
self.chat_api_list = [api.strip() for api in self.chat_api_list if len(api) > 5]
324+
self.chat_api_list.append(OPENAI_KEY)
325+
326+
# prevent short strings from being incorrectly used as API keys.
327+
self.chat_api_list = [api.strip() for api in self.chat_api_list if len(api) > 20]
324328
self.cur_api = 0
325329
self.file_format = args.file_format
326330
if args.save_image:
@@ -458,7 +462,9 @@ def summary_with_chat(self, paper_list):
458462

459463
htmls.append('## Paper:' + str(paper_index + 1))
460464
htmls.append('\n\n\n')
461-
htmls.append(chat_summary_text)
465+
chat_summary_text = ""
466+
if "chat_summary_text" in locals():
467+
htmls.append(chat_summary_text)
462468

463469
# 第二步总结方法:
464470
# TODO,由于有些文章的方法章节名是算法名,所以简单的通过关键词来筛选,很难获取,后面需要用其他的方案去优化。
@@ -487,7 +493,10 @@ def summary_with_chat(self, paper_list):
487493
offset = int(str(e)[current_tokens_index:current_tokens_index + 4])
488494
method_prompt_token = offset + 800 + 150
489495
chat_method_text = self.chat_method(text=text, method_prompt_token=method_prompt_token)
490-
htmls.append(chat_method_text)
496+
chat_method_text = ""
497+
if "chat_method_text" in locals():
498+
htmls.append(chat_method_text)
499+
# htmls.append(chat_method_text)
491500
else:
492501
chat_method_text = ''
493502
htmls.append("\n" * 4)
@@ -521,16 +530,16 @@ def summary_with_chat(self, paper_list):
521530
conclusion_prompt_token = offset + 800 + 150
522531
chat_conclusion_text = self.chat_conclusion(text=text,
523532
conclusion_prompt_token=conclusion_prompt_token)
524-
htmls.append(chat_conclusion_text)
533+
chat_conclusion_text = ""
534+
if "chat_conclusion_text" in locals():
535+
htmls.append(chat_conclusion_text)
525536
htmls.append("\n" * 4)
526537

527538
# # 整合成一个文件,打包保存下来。
528539
date_str = str(datetime.datetime.now())[:13].replace(' ', '-')
529-
try:
530-
export_path = os.path.join(self.root_path, 'export')
540+
export_path = os.path.join(self.root_path, 'export')
541+
if not os.path.exists(export_path):
531542
os.makedirs(export_path)
532-
except:
533-
pass
534543
mode = 'w' if paper_index == 0 else 'a'
535544
file_name = os.path.join(export_path,
536545
date_str + '-' + self.validateTitle(self.query) + "." + self.file_format)

chat_paper.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,13 @@ def __init__(self, key_word, query, filter_keys,
5555
self.config = configparser.ConfigParser()
5656
# 读取配置文件
5757
self.config.read('apikey.ini')
58-
# 获取某个键对应的值
58+
OPENAI_KEY = os.environ.get("OPENAI_KEY", "")
59+
# 获取某个键对应的值
5960
self.chat_api_list = self.config.get('OpenAI', 'OPENAI_API_KEYS')[1:-1].replace('\'', '').split(',')
60-
self.chat_api_list = [api.strip() for api in self.chat_api_list if len(api) > 5]
61+
self.chat_api_list.append(OPENAI_KEY)
62+
63+
# prevent short strings from being incorrectly used as API keys.
64+
self.chat_api_list = [api.strip() for api in self.chat_api_list if len(api) > 20]
6165
self.cur_api = 0
6266
self.file_format = args.file_format
6367
if args.save_image:
@@ -280,11 +284,9 @@ def summary_with_chat(self, paper_list):
280284

281285
# # 整合成一个文件,打包保存下来。
282286
date_str = str(datetime.datetime.now())[:13].replace(' ', '-')
283-
try:
284-
export_path = os.path.join(self.root_path, 'export')
287+
export_path = os.path.join(self.root_path, 'export')
288+
if not os.path.exists(export_path):
285289
os.makedirs(export_path)
286-
except:
287-
pass
288290
mode = 'w' if paper_index == 0 else 'a'
289291
file_name = os.path.join(export_path,
290292
date_str + '-' + self.validateTitle(paper.title[:80]) + "." + self.file_format)

chat_response.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,13 @@ def __init__(self, args=None):
3535
self.config = configparser.ConfigParser()
3636
# 读取配置文件
3737
self.config.read('apikey.ini')
38-
# 获取某个键对应的值
38+
OPENAI_KEY = os.environ.get("OPENAI_KEY", "")
39+
# 获取某个键对应的值
3940
self.chat_api_list = self.config.get('OpenAI', 'OPENAI_API_KEYS')[1:-1].replace('\'', '').split(',')
40-
self.chat_api_list = [api.strip() for api in self.chat_api_list if len(api) > 5]
41+
self.chat_api_list.append(OPENAI_KEY)
42+
43+
# prevent short strings from being incorrectly used as API keys.
44+
self.chat_api_list = [api.strip() for api in self.chat_api_list if len(api) > 20]
4145
self.cur_api = 0
4246
self.file_format = args.file_format
4347
self.max_token_num = 4096
@@ -54,11 +58,9 @@ def response_by_chatgpt(self, comment_path):
5458

5559
# 将审稿意见保存起来
5660
date_str = str(datetime.datetime.now())[:13].replace(' ', '-')
57-
try:
58-
export_path = os.path.join('./', 'response_file')
61+
export_path = os.path.join('./', 'response_file')
62+
if not os.path.exists(export_path):
5963
os.makedirs(export_path)
60-
except:
61-
pass
6264
file_name = os.path.join(export_path, date_str + '-Response.' + self.file_format)
6365
self.export_to_markdown("\n".join(htmls), file_name=file_name)
6466
htmls = []

chat_reviewer.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ def __init__(self, args=None):
3333
self.config = configparser.ConfigParser()
3434
# 读取配置文件
3535
self.config.read('apikey.ini')
36-
# 获取某个键对应的值
36+
OPENAI_KEY = os.environ.get("OPENAI_KEY", "")
37+
# 获取某个键对应的值
3738
self.chat_api_list = self.config.get('OpenAI', 'OPENAI_API_KEYS')[1:-1].replace('\'', '').split(',')
38-
self.chat_api_list = [api.strip() for api in self.chat_api_list if len(api) > 5]
39+
self.chat_api_list.append(OPENAI_KEY)
40+
41+
# prevent short strings from being incorrectly used as API keys.
42+
self.chat_api_list = [api.strip() for api in self.chat_api_list if len(api) > 20]
3943
self.cur_api = 0
4044
self.file_format = args.file_format
4145
self.max_token_num = 4096
@@ -72,11 +76,9 @@ def review_by_chatgpt(self, paper_list):
7276

7377
# 将审稿意见保存起来
7478
date_str = str(datetime.datetime.now())[:13].replace(' ', '-')
75-
try:
76-
export_path = os.path.join('./', 'output_file')
79+
export_path = os.path.join(self.root_path, 'export')
80+
if not os.path.exists(export_path):
7781
os.makedirs(export_path)
78-
except:
79-
pass
8082
mode = 'w' if paper_index == 0 else 'a'
8183
file_name = os.path.join(export_path,
8284
date_str + '-' + self.validateTitle(paper.title) + "." + self.file_format)

readme_en.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ After visiting http://127.0.0.1:5000/, you will see the homepage. On the homepag
128128

129129

130130

131-
![image-20230331042557655](/Users/jessytsui/Library/Application Support/typora-user-images/image-20230331042557655.png)
131+
![flask_home_page](./images/flask_web_home.png)
132132

133133

134134

0 commit comments

Comments
 (0)