-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_textbook.py
203 lines (166 loc) · 6.68 KB
/
find_textbook.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import pyperclip
import time
import pyautogui
# from googlesearch import search
import requests
from anthropic import Anthropic, HUMAN_PROMPT, AI_PROMPT
from prompts import SEARCH_TABLE_OF_CONTENTS, EXTRACT_QUESTION, REAL_QUESTION
from pdfminer.high_level import extract_text
import re
import pdfreader
import os
from dotenv import load_dotenv
import sys
load_dotenv()
CLAUDE_API_KEY = os.getenv("CLAUDE_API_KEY")
anthropic = Anthropic(api_key=CLAUDE_API_KEY)
def find_textbook_link(textbook_name):
query = textbook_name + " PDF"
links = list(search(query, num=1, stop=1, pause=2))
if links:
return links[0]
else:
return None
def find_textbook_link_googlev2(textbook_name):
query = "textbook " + str(textbook_name) + " filetype:pdf -site:amazon"
pyautogui.keyDown('command')
pyautogui.press('space')
pyautogui.keyUp('command')
pyautogui.keyUp('Fn') # so we don't press the emoji bar
pyautogui.typewrite("https://www.google.com/")
pyautogui.press('enter')
time.sleep(2)
pyautogui.keyUp('Fn') # so we don't press the emoji bar
pyautogui.typewrite(query)
pyautogui.press('enter')
time.sleep(2)
pyautogui.press('tab', presses=21) # get to the first link
pyautogui.press('enter') # go to the link
pyautogui.keyDown('command')
pyautogui.press('l')
pyautogui.press('c')
pyautogui.keyUp('command')
time.sleep(1)
return pyperclip.paste()
def generate_textbook_pdf(link):
try:
response = requests.get(link)
response.raise_for_status()
with open("downloaded_textbook.pdf", "wb") as f:
f.write(response.content)
except requests.exceptions.HTTPError as err:
print(link)
def extract_pages(pdf_path, limit_pages=10):
text = extract_text(pdf_path, page_numbers=list(range(limit_pages)))
return text
def search_table_of_contents(excerpt, section):
completion = anthropic.completions.create(
model="claude-2",
max_tokens_to_sample=1000,
prompt=f"{HUMAN_PROMPT} {SEARCH_TABLE_OF_CONTENTS} <excerpt>{excerpt}<excerpt> <section>{section}<section> {AI_PROMPT}",
)
return completion.completion
def extract_section_pages(pdf_path, start_page, limit_pages=30):
text = extract_text(pdf_path, page_numbers=list(range(start_page, start_page + limit_pages)))
return text
def extract_question(excerpt, question):
completion = anthropic.completions.create(
model="claude-2",
max_tokens_to_sample=1000,
prompt=f"{HUMAN_PROMPT} {EXTRACT_QUESTION} <excerpt>{excerpt}<excerpt> <question>{question}<question> {AI_PROMPT}",
)
return completion.completion
# textbook_name = "The Elements of Statistical Learning: Data Mining, Inference, and Prediction by Trevor Hastie, Robert Tibshirani, and Jerome Friedman."
# textbook_name = "John A. Rice, Third Edition."
# chapter = "8"
# section = "10"
# question = "21"
def is_real_question(excerpt, question):
completion = anthropic.completions.create(
model="claude-2",
max_tokens_to_sample=1000,
prompt=f"{HUMAN_PROMPT} {REAL_QUESTION} <excerpt>{excerpt}<excerpt> <question>{question}<question> {AI_PROMPT}",
)
return completion.completion
def find_questions():
textbook_name = ""
chapter = ""
section = ""
question = ""
pdf_questions = [{'sources': ['John A. Rice, Third Edition. | Problem 8.10.21']},
{'sources': ['John A. Rice, Third Edition. | Problem 8.10.45', 'Rproject3.script4.Chromatin.r']},
{'sources': ['John A. Rice, Third Edition. | Problem 8.10.51']},
{'sources': ['John A. Rice, Third Edition. | Problem 8.10.58', 'Rproject3.script1.multinomial.simulation.r']}]
# pdf_questions = [{'sources': ['John A. Rice, Third Edition. | Problem 8.10.58', 'Rproject3.script1.multinomial.simulation.r']}]
source = pdf_questions[0]['sources'][0]
pattern = r'^(.*?)\s*\|'
match = re.search(pattern, source)
textbook_name = match.group(1)
source = pdf_questions[0]['sources'][0]
pattern = r'^(.*?)\s*\|'
match = re.search(pattern, source)
textbook_name = match.group(1)
print(textbook_name)
link = find_textbook_link_googlev2(textbook_name)
print(link)
# generate_textbook_pdf(link)
questions_list = []
# question_number = ""
# for questions in pdf_questions:
# source = questions['sources'][0]
# question_number = ""
# for questions in pdf_questions:
# source = questions['sources'][0]
# pattern = r'Problem (\d+\.\d+\.\d+)'
# match = re.search(pattern, source)
# question_number = match.group(1)
# pattern = r'Problem (\d+\.\d+\.\d+)'
# match = re.search(pattern, source)
# question_number = match.group(1)
# chapter, section, question = question_number.split(".")
# print("-------------------")
# print("-------------------")
# print("-------------------")
# print(chapter, section, question)
# chapter, section, question = question_number.split(".")
# pdf_path = "downloaded_textbook.pdf"
# section_title = f"{chapter}.{section}"
# excerpt = extract_pages(pdf_path)
# pattern = fr"({section_title}.{{0,500}})"
# match = re.search(pattern, excerpt, re.DOTALL)
# excerpt = match.group(1)
# page_text = search_table_of_contents(excerpt, section_title)
# # print(page_text)
# page_text = search_table_of_contents(excerpt, section_title)
# # print(page_text)
# match = re.search(r"(\b\d+\b)(?!.*\b\d+\b)", page_text)
# try:
# page = int(match.group(1))
# except AttributeError:
# print("ERROR: Page not found.")
# page = 312
# match = re.search(r"(\b\d+\b)(?!.*\b\d+\b)", page_text)
# try:
# page = int(match.group(1))
# except AttributeError:
# print("ERROR: Page not found.")
# page = 312
# output = extract_section_pages(pdf_path, page)
# pattern = fr"({question}(?:\.|\)).{{0,2000}})"
# match = re.search(pattern, output, re.DOTALL)
# try:
# output = match.group(1)
# question_content = extract_question(output, question)
# questions_list.append(question_content)
# print(question_content)
# except AttributeError:
# print("ERROR: Question not found.")
return questions_list
print("-------------------")
print("-------------------")
print("-------------------")
my_list = find_questions()
print(my_list)
# def main():
# print(find_textbook_link_googlev2("Mathematical Statistics and Data Analysis John A. Rice"))
# main()