-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_pdf_text_o1_review.py
71 lines (55 loc) · 2.31 KB
/
2_pdf_text_o1_review.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
import argparse
import openai
import PyPDF2
import os
import sys
sys.path.append(os.getcwd())
from util.util import extract_text_from_pdf, get_openai_api_key
def compute_usage_cost(model_type, prompt_tokens, completion_tokens):
if model_type == "o1":
return prompt_tokens * 15/1e6 + completion_tokens * 60/1e6
elif model_type == "gpt-4o":
return prompt_tokens * 2.5/1e6 + completion_tokens * 60/1e6
def generate_review(openai_api_key, text):
current_file_path = os.path.abspath(__file__)
review_prompt_path = os.path.join(os.path.dirname(current_file_path), "review_prompt.txt")
review_prompt = open(review_prompt_path, "r").read().strip()
openai.api_key = openai_api_key
print("Generating a review of the paper...")
review_response = openai.ChatCompletion.create(
model="o1",
messages=[
{"role": "system", "content": "You are an expert reviewer for AI conference papers."},
{"role": "user", "content": f"Read the following paper, in preparation to review it:\n{text}"},
{"role": "user", "content": f"{review_prompt}"}
]
)
review = review_response['choices'][0]['message']['content']
print("Usage:")
print(review_response['usage'])
prompt_tokens = review_response['usage']['prompt_tokens']
completion_tokens = review_response['usage']['completion_tokens']
print(f"Cost: ${compute_usage_cost('o1', prompt_tokens, completion_tokens):.2f}")
return review
def main():
# Required argument: paper PDF path.
if len(sys.argv) < 2:
print("Usage: python 2_pdf_text_o1_review.py <path_to_pdf>")
return
openai_api_key = get_openai_api_key()
pdf_path = sys.argv[1]
if not os.path.exists(pdf_path):
print("Error: The specified file does not exist.")
return
print(f"Reviewing paper: {pdf_path}")
pdf_dir = os.path.dirname(pdf_path)
print("Extracting text from the PDF...")
paper_text = extract_text_from_pdf(pdf_path)
print("Generating review...")
review = generate_review(openai_api_key, paper_text)
review_file_path = os.path.join(pdf_dir, "2_review.txt")
with open(review_file_path, "w") as review_file:
review_file.write(review)
print(f"Review has been saved to {review_file_path}")
if __name__ == "__main__":
main()