-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
65 lines (63 loc) · 1.8 KB
/
main.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
import os
try:
import PyPDF2
except:
os.system(f'cmd /c "pip install PyPDF2"')
import PyPDF2
try:
from openai import OpenAI
import ast
except:
os.system(f'cmd /c "pip install openai"')
from openai import OpenAI
import ast
try:
from pyttsx3 import Engine
except:
os.system(f'cmd /c "pip install pyttsx3"')
from pyttsx3 import Engine
def Speak(text):
engine = Engine()
engine.say(text)
engine.runAndWait()
return
def gpt(txt):
try:
client = OpenAI()
api_key="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"
response = client.chat.completions.create(
model="gpt-3.5-turbo-1106",
response_format={ "type": "json_object" },
messages=[
{"role": "system", "content": "You are a helpful assistant designed to output JSON."},
{"role": "user", "content": f"{txt}"}
]
)
x=ast.literal_eval(response.choices[0].message.content)
lst=list(x.keys())
res=x[f'{lst[0]}']
return res
except Exception as e:
return f"error code {e}"
def paragraph(listinp):
if len(listinp)==1:
return listinp[0]
return f"{listinp[0]}'\n'{paragraph(listinp[1:])}"
def summary(paralist):
para=paragraph(paralist)
query=f'''summarise the following paragraph to less than 500 words :\n{para}'''
result=gpt(query)
return result
file = open('G:\Codes\general projects\PDF-Reader\sample.pdf', 'rb')
reader = PyPDF2.PdfReader(file)
l=int(len(reader.pages))
lis=[]
for i in range(l):
page = reader.pages[i]
lis.append(page.extract_text())
for i in range(len(lis)):
lis[i]=lis[i].replace('\n',' ')
print(summary(lis))
Speak(summary(lis))
# closing the pdf file object
file.close()