Spaces:
Runtime error
Runtime error
Commit
·
0604330
1
Parent(s):
433d4e2
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
import urllib.request
|
2 |
import fitz
|
3 |
import re
|
@@ -8,7 +10,6 @@ import gradio as gr
|
|
8 |
import os
|
9 |
from sklearn.neighbors import NearestNeighbors
|
10 |
|
11 |
-
|
12 |
def download_pdf(url, output_path):
|
13 |
urllib.request.urlretrieve(url, output_path)
|
14 |
|
@@ -50,11 +51,10 @@ def text_to_chunks(texts, word_length=150, start_page=1):
|
|
50 |
text_toks[idx+1] = chunk + text_toks[idx+1]
|
51 |
continue
|
52 |
chunk = ' '.join(chunk).strip()
|
53 |
-
chunk = f'[{idx+start_page}]' + ' ' + '"' + chunk + '"'
|
54 |
chunks.append(chunk)
|
55 |
return chunks
|
56 |
|
57 |
-
|
58 |
class SemanticSearch:
|
59 |
|
60 |
def __init__(self):
|
@@ -156,18 +156,14 @@ def question_answer(url, file, question,openAI_key):
|
|
156 |
|
157 |
if question.strip() == '':
|
158 |
return '[ERROR]: Question field is empty'
|
159 |
-
|
160 |
-
answer = generate_answer(question,openAI_key)
|
161 |
-
# Convert answer to HTML with clickable citations
|
162 |
-
answer_html = re.sub(r'\[(\d+)\]', r'<a href="#" onclick="navigateToPage(\1)">[\1]</a>', answer)
|
163 |
|
164 |
-
return
|
165 |
|
166 |
|
167 |
recommender = SemanticSearch()
|
168 |
|
169 |
title = 'PDF GPT'
|
170 |
-
description = """PDF GPT allows you to chat with your PDF file using Universal Sentence Encoder and Open AI. It gives hallucination
|
171 |
|
172 |
with gr.Blocks() as demo:
|
173 |
|
@@ -175,10 +171,10 @@ with gr.Blocks() as demo:
|
|
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
|
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'])
|
@@ -189,6 +185,6 @@ with gr.Blocks() as demo:
|
|
189 |
with gr.Group():
|
190 |
answer = gr.Textbox(label='The answer to your question is :')
|
191 |
|
192 |
-
btn.click(question_answer, inputs=[url, file, question,
|
193 |
-
|
194 |
-
demo.launch()
|
|
|
1 |
+
In the below code, please tell me how to modify it so that the citations appear as [Page no. XX] instead of [XX]:
|
2 |
+
|
3 |
import urllib.request
|
4 |
import fitz
|
5 |
import re
|
|
|
10 |
import os
|
11 |
from sklearn.neighbors import NearestNeighbors
|
12 |
|
|
|
13 |
def download_pdf(url, output_path):
|
14 |
urllib.request.urlretrieve(url, output_path)
|
15 |
|
|
|
51 |
text_toks[idx+1] = chunk + text_toks[idx+1]
|
52 |
continue
|
53 |
chunk = ' '.join(chunk).strip()
|
54 |
+
chunk = f'[Page no. {idx+start_page}]' + ' ' + '"' + chunk + '"'
|
55 |
chunks.append(chunk)
|
56 |
return chunks
|
57 |
|
|
|
58 |
class SemanticSearch:
|
59 |
|
60 |
def __init__(self):
|
|
|
156 |
|
157 |
if question.strip() == '':
|
158 |
return '[ERROR]: Question field is empty'
|
|
|
|
|
|
|
|
|
159 |
|
160 |
+
return generate_answer(question,openAI_key)
|
161 |
|
162 |
|
163 |
recommender = SemanticSearch()
|
164 |
|
165 |
title = 'PDF GPT'
|
166 |
+
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."""
|
167 |
|
168 |
with gr.Blocks() as demo:
|
169 |
|
|
|
171 |
gr.Markdown(description)
|
172 |
|
173 |
with gr.Row():
|
174 |
+
|
175 |
with gr.Group():
|
176 |
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>')
|
177 |
+
openAI_key=gr.Textbox(label='Enter your OpenAI API key here')
|
178 |
url = gr.Textbox(label='Enter PDF URL here')
|
179 |
gr.Markdown("<center><h4>OR<h4></center>")
|
180 |
file = gr.File(label='Upload your PDF/ Research Paper / Book here', file_types=['.pdf'])
|
|
|
185 |
with gr.Group():
|
186 |
answer = gr.Textbox(label='The answer to your question is :')
|
187 |
|
188 |
+
btn.click(question_answer, inputs=[url, file, question,openAI_key], outputs=[answer])
|
189 |
+
#openai.api_key = os.getenv('Your_Key_Here')
|
190 |
+
demo.launch()
|