-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
89 lines (74 loc) · 2.58 KB
/
utils.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
# Import the necessary libraries
import openai
import pdfplumber
import os
import tiktoken
# Set the OpenAI API key
openai.api_key = os.getenv("OPENAI_API_KEY")
def extract_text_from_pdf(pdf_file, page_range) -> str:
"""
Function to extract text from a PDF file using pdfplumber
"""
# Parse the page range
if page_range:
start_page, end_page = map(int, page_range.split("-"))
else:
start_page, end_page = None, None
try:
# Open the PDF file
with pdfplumber.open(pdf_file) as pdf:
# Initialize an empty string for the text
text = ""
# Iterate over the pages within the range if specified, else all pages
for page in (
pdf.pages[start_page - 1 : end_page]
if start_page and end_page
else pdf.pages
):
# for page in pdf.pages:
# Extract the text from the page and add it to the text string
text += page.extract_text()
except Exception as e:
print(f"Error occured: {e}")
text = ""
return text
def chunk_text(text, max_tokens=8000) -> list:
chunk_count = 0
chunks_log = []
while text:
chunk = " ".join(text.split()[:max_tokens])
encoding = tiktoken.get_encoding("cl100k_base")
token_count = len(encoding.encode(chunk))
# reduce
while token_count > max_tokens:
chunk = " ".join(chunk.split()[:-1])
token_count = len(encoding.encode(chunk))
text = " ".join(text.split()[len(chunk.split()) :])
chunk_count += 1
chunk_data = {
"chunk_count": chunk_count,
"word_count": len(chunk.split()),
"token_count": token_count,
"text": chunk, # add the chunk text to the dictionary
}
chunks_log.append(chunk_data)
return chunks_log
def summarize(document_prompt, user_prompt, system_prompt, model, temp=0.7) -> str:
try:
response = openai.ChatCompletion.create(
model=model,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": document_prompt},
{"role": "user", "content": user_prompt},
],
temperature=temp,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
)
output_summary = response["choices"][0]["message"]["content"]
except Exception as e:
print(f"Error occurred: {e}")
output_summary = ""
return output_summary