-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_to_other.py
135 lines (71 loc) · 2.86 KB
/
text_to_other.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
import pyttsx3#text to speech
import PyPDF2#reading pdfs
import requests#for bionic reading api
from bs4 import BeautifulSoup
import openai
url = "https://bionic-reading1.p.rapidapi.com/convert"#bionic api link
user_inp = input("Would you like to convert the pdf to audio, convert to a bionic reading format, or have a summary of the pdf: ")
open_pdf = PyPDF2.PdfReader(open('FILE_NAME.pdf', 'rb'))#open pdf
bionic_text = " "
for page in (open_pdf.pages):
text = page.extract_text()
bionic_text += text
cleaned_text = text.strip().replace('\n', ' ')
#-------------------------
def audio_convert(cleaned_text):
speaker = pyttsx3.init()
speaker.save_to_file(cleaned_text, 'audio.mp3')
speaker.runAndWait()
speaker.stop()
#-----------------------------
if user_inp == 'audio':
audio_convert('The Little Prince.pdf')
def summarize(text):
chat_log = []
openai.api_key = 'api_key'
#giving summary
prompt = "Briefly summarize the following text:\n\n" + text
chat_log.append({"role": "user", "content": prompt})
response = openai.Completion.create(
engine='text-davinci-003',
prompt=prompt,
max_tokens=100,
temperature=0.3,
n=1,
stop=None,
)
th3_summary = response.choices[0].text.strip()
chat_log.append({"role": "assistant", "content": th3_summary})
print(th3_summary)
#post summary questions
print("You may ask further questions about the text.\nSay \"Done!\" when finished with questions: \n")
while input != "quit()":
message = input()#take input from user
chat_log.append({"role": "user", "content": message})#add user input to chat log
response = openai.ChatCompletion.create(#telling gpt what the user asked, answer will be stored in response
model="gpt-3.5-turbo",
messages=chat_log)
gpt_reply = response["choices"][0]["message"]["content"]#basically gets the message instead of the whole json output
chat_log.append({"role": "assistant", "content": gpt_reply})
print("\n" + gpt_reply + "\n")
if user_inp == 'summary':
(summarize(cleaned_text))
def bionic_reading(bionic_text):
#bionic reading stuff
payload = {
"content": "{}".format(bionic_text),
"response_type": "html",
"request_type": "html",
"fixation": "1",
"saccade": "10"
}
headers = {
"content-type": "application/x-www-form-urlencoded",
"X-RapidAPI-Key": "4fa1e20f7bmsha3298d2081dd68ep142cccjsn399d548298a8",
"X-RapidAPI-Host": "bionic-reading1.p.rapidapi.com"
}
response = requests.post(url, data=payload, headers=headers)
with open('file.html', 'w') as file:
file.write(str(response.text))
if user_inp == 'bionic':
(bionic_reading(bionic_text))