Spaces:
Runtime error
Runtime error
Commit
·
88dc7bb
1
Parent(s):
6dcf633
Update app.py
Browse files
app.py
CHANGED
@@ -24,10 +24,6 @@ def pdf_to_text(path, start_page=1, end_page=None):
|
|
24 |
|
25 |
if end_page is None:
|
26 |
end_page = total_pages
|
27 |
-
else:
|
28 |
-
end_page = int(end_page)
|
29 |
-
|
30 |
-
start_page = int(start_page)
|
31 |
|
32 |
text_list = []
|
33 |
|
@@ -40,7 +36,6 @@ def pdf_to_text(path, start_page=1, end_page=None):
|
|
40 |
return text_list
|
41 |
|
42 |
|
43 |
-
|
44 |
def text_to_chunks(texts, word_length=150, start_page=1):
|
45 |
text_toks = [t.split(' ') for t in texts]
|
46 |
page_nums = []
|
@@ -96,40 +91,13 @@ class SemanticSearch:
|
|
96 |
|
97 |
|
98 |
|
99 |
-
|
100 |
-
# global recommender
|
101 |
-
# texts = pdf_to_text(path, start_page=start_page)
|
102 |
-
# chunks = text_to_chunks(texts, start_page=start_page)
|
103 |
-
# recommender.fit(chunks)
|
104 |
-
# return 'Corpus Loaded.'
|
105 |
-
|
106 |
-
# The modified function generates embeddings based on PDF file name and page number and checks if the embeddings file exists before loading or generating it.
|
107 |
-
def load_recommender(path, start_page=1, end_page=None):
|
108 |
global recommender
|
109 |
-
|
110 |
-
embeddings_file = f"{pdf_file}_{start_page}.npy"
|
111 |
-
|
112 |
-
if os.path.isfile(embeddings_file):
|
113 |
-
embeddings = np.load(embeddings_file)
|
114 |
-
recommender.embeddings = embeddings
|
115 |
-
recommender.fitted = True
|
116 |
-
return "Embeddings loaded from file"
|
117 |
-
|
118 |
-
if start_page:
|
119 |
-
start_page = int(start_page)
|
120 |
-
if end_page:
|
121 |
-
end_page = int(end_page)
|
122 |
-
|
123 |
-
texts = pdf_to_text(path, start_page=start_page, end_page=end_page)
|
124 |
chunks = text_to_chunks(texts, start_page=start_page)
|
125 |
recommender.fit(chunks)
|
126 |
-
np.save(embeddings_file, recommender.embeddings)
|
127 |
return 'Corpus Loaded.'
|
128 |
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
def generate_text(openAI_key,prompt, engine="text-davinci-003"):
|
134 |
openai.api_key = openAI_key
|
135 |
completions = openai.Completion.create(
|
@@ -142,22 +110,6 @@ def generate_text(openAI_key,prompt, engine="text-davinci-003"):
|
|
142 |
)
|
143 |
message = completions.choices[0].text
|
144 |
return message
|
145 |
-
|
146 |
-
def generate_text2(openAI_key, prompt, engine="gpt-3.5-turbo-0301"):
|
147 |
-
openai.api_key = openAI_key
|
148 |
-
messages = [{'role': 'system', 'content': 'You are a helpful assistant.'},
|
149 |
-
{'role': 'user', 'content': prompt}]
|
150 |
-
|
151 |
-
completions = openai.ChatCompletion.create(
|
152 |
-
model=engine,
|
153 |
-
messages=messages,
|
154 |
-
max_tokens=512,
|
155 |
-
n=1,
|
156 |
-
stop=None,
|
157 |
-
temperature=0.7,
|
158 |
-
)
|
159 |
-
message = completions.choices[0].message['content']
|
160 |
-
return message
|
161 |
|
162 |
def generate_answer(question,openAI_key):
|
163 |
topn_chunks = recommender(question)
|
@@ -180,58 +132,68 @@ def generate_answer(question,openAI_key):
|
|
180 |
return answer
|
181 |
|
182 |
|
183 |
-
def question_answer(url, file, question, openAI_key,
|
184 |
if openAI_key.strip() == '':
|
185 |
-
return '[ERROR]: Please enter your Open AI Key. Get your key here
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
|
|
191 |
|
192 |
if url.strip() != '':
|
193 |
glob_url = url
|
194 |
download_pdf(glob_url, 'corpus.pdf')
|
195 |
-
load_recommender('corpus.pdf'
|
196 |
|
197 |
else:
|
198 |
old_file_name = file.name
|
199 |
file_name = file.name
|
200 |
file_name = file_name[:-12] + file_name[-4:]
|
201 |
os.rename(old_file_name, file_name)
|
202 |
-
load_recommender(file_name
|
203 |
|
204 |
if question.strip() == '':
|
205 |
return '[ERROR]: Question field is empty'
|
206 |
|
207 |
-
|
|
|
|
|
|
|
208 |
|
209 |
|
210 |
recommender = SemanticSearch()
|
211 |
|
212 |
title = 'PDF GPT'
|
213 |
-
description = """
|
214 |
-
|
215 |
-
2. The returned response can even cite the page number in square brackets([]) where the information is located, adding credibility to the responses and helping to locate pertinent information quickly. The Responses are much better than the naive responses by Open AI."""
|
216 |
|
217 |
with gr.Blocks() as demo:
|
218 |
|
219 |
gr.Markdown(f'<center><h1>{title}</h1></center>')
|
220 |
gr.Markdown(description)
|
|
|
221 |
with gr.Row():
|
|
|
222 |
with gr.Group():
|
223 |
gr.Markdown(f'<p style="text-align:center">Get your Open AI API key <a href="https://platform.openai.com/account/api-keys">here</a></p>')
|
224 |
-
openAI_key
|
225 |
url = gr.Textbox(label='Enter PDF URL here')
|
226 |
gr.Markdown("<center><h4>OR<h4></center>")
|
227 |
file = gr.File(label='Upload your PDF/ Research Paper / Book here', file_types=['.pdf'])
|
228 |
question = gr.Textbox(label='Enter your question here')
|
229 |
-
start_page = gr.Number(label='Start on page (default: 1)', default=1, min_value=1)
|
230 |
-
end_page = gr.Number(label='End on page (default: last page)', default=None, min_value=1, allow_none=True)
|
231 |
btn = gr.Button(value='Submit')
|
232 |
btn.style(full_width=True)
|
|
|
233 |
with gr.Group():
|
234 |
answer = gr.Textbox(label='The answer to your question is :')
|
235 |
-
|
|
|
|
|
|
|
|
|
|
|
236 |
|
237 |
-
|
|
|
|
24 |
|
25 |
if end_page is None:
|
26 |
end_page = total_pages
|
|
|
|
|
|
|
|
|
27 |
|
28 |
text_list = []
|
29 |
|
|
|
36 |
return text_list
|
37 |
|
38 |
|
|
|
39 |
def text_to_chunks(texts, word_length=150, start_page=1):
|
40 |
text_toks = [t.split(' ') for t in texts]
|
41 |
page_nums = []
|
|
|
91 |
|
92 |
|
93 |
|
94 |
+
def load_recommender(path, start_page=1):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
global recommender
|
96 |
+
texts = pdf_to_text(path, start_page=start_page)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
chunks = text_to_chunks(texts, start_page=start_page)
|
98 |
recommender.fit(chunks)
|
|
|
99 |
return 'Corpus Loaded.'
|
100 |
|
|
|
|
|
|
|
|
|
101 |
def generate_text(openAI_key,prompt, engine="text-davinci-003"):
|
102 |
openai.api_key = openAI_key
|
103 |
completions = openai.Completion.create(
|
|
|
110 |
)
|
111 |
message = completions.choices[0].text
|
112 |
return message
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
def generate_answer(question,openAI_key):
|
115 |
topn_chunks = recommender(question)
|
|
|
132 |
return answer
|
133 |
|
134 |
|
135 |
+
def question_answer(url, file, question, openAI_key, history=[]):
|
136 |
if openAI_key.strip() == '':
|
137 |
+
return '[ERROR]: Please enter your Open AI Key. Get your key here: https://platform.openai.com/account/api-keys'
|
138 |
+
|
139 |
+
if url.strip() == '' and file is None:
|
140 |
+
return '[ERROR]: Both URL and PDF are empty. Provide at least one.'
|
141 |
+
|
142 |
+
if url.strip() != '' and file is not None:
|
143 |
+
return '[ERROR]: Both URL and PDF are provided. Please provide only one (either URL or PDF).'
|
144 |
|
145 |
if url.strip() != '':
|
146 |
glob_url = url
|
147 |
download_pdf(glob_url, 'corpus.pdf')
|
148 |
+
load_recommender('corpus.pdf')
|
149 |
|
150 |
else:
|
151 |
old_file_name = file.name
|
152 |
file_name = file.name
|
153 |
file_name = file_name[:-12] + file_name[-4:]
|
154 |
os.rename(old_file_name, file_name)
|
155 |
+
load_recommender(file_name)
|
156 |
|
157 |
if question.strip() == '':
|
158 |
return '[ERROR]: Question field is empty'
|
159 |
|
160 |
+
answer = generate_answer(question, openAI_key)
|
161 |
+
history.append({'question': question, 'answer': answer})
|
162 |
+
return answer
|
163 |
+
|
164 |
|
165 |
|
166 |
recommender = SemanticSearch()
|
167 |
|
168 |
title = 'PDF GPT'
|
169 |
+
description = """ PDF GPT allows you to chat with your PDF file using Universal Sentence Encoder and Open AI. It gives hallucination free response than other tools as the embeddings are better than OpenAI. The returned response can even cite the page number in square brackets([]) where the information is located, adding credibility to the responses and helping to locate pertinent information quickly."""
|
170 |
+
history = []
|
|
|
171 |
|
172 |
with gr.Blocks() as demo:
|
173 |
|
174 |
gr.Markdown(f'<center><h1>{title}</h1></center>')
|
175 |
gr.Markdown(description)
|
176 |
+
|
177 |
with gr.Row():
|
178 |
+
|
179 |
with gr.Group():
|
180 |
gr.Markdown(f'<p style="text-align:center">Get your Open AI API key <a href="https://platform.openai.com/account/api-keys">here</a></p>')
|
181 |
+
openAI_key=gr.Textbox(label='Enter your OpenAI API key here')
|
182 |
url = gr.Textbox(label='Enter PDF URL here')
|
183 |
gr.Markdown("<center><h4>OR<h4></center>")
|
184 |
file = gr.File(label='Upload your PDF/ Research Paper / Book here', file_types=['.pdf'])
|
185 |
question = gr.Textbox(label='Enter your question here')
|
|
|
|
|
186 |
btn = gr.Button(value='Submit')
|
187 |
btn.style(full_width=True)
|
188 |
+
|
189 |
with gr.Group():
|
190 |
answer = gr.Textbox(label='The answer to your question is :')
|
191 |
+
history_box = gr.Textbox(label='History of Questions and Answers', value='', type='output', lines=10)
|
192 |
+
|
193 |
+
|
194 |
+
#btn.click(question_answer, inputs=[url, file, question,openAI_key], outputs=[answer])
|
195 |
+
btn.click(question_answer, inputs=[url, file, question,openAI_key, history], outputs=[answer, history_box])
|
196 |
+
|
197 |
|
198 |
+
#openai.api_key = os.getenv('Your_Key_Here')
|
199 |
+
demo.launch()
|