Spaces:
Runtime error
Runtime error
Commit Β·
1fa5f16
1
Parent(s): ad9d2f6
Update app.py
Browse files- Remove save document button
- Modification of the prompt
app.py
CHANGED
|
@@ -67,28 +67,6 @@ def construct_index(doc):
|
|
| 67 |
return index
|
| 68 |
|
| 69 |
|
| 70 |
-
def upload_doc(file):
|
| 71 |
-
# Open the PDF file in binary mode
|
| 72 |
-
with open(file.name, 'rb') as f:
|
| 73 |
-
# Initialize a PDF file reader object
|
| 74 |
-
pdf_reader = PdfReader(f)
|
| 75 |
-
|
| 76 |
-
# Initialize an empty string for storing the extracted text
|
| 77 |
-
text = ''
|
| 78 |
-
|
| 79 |
-
# Loop through the number of pages
|
| 80 |
-
for page in pdf_reader.pages:
|
| 81 |
-
# Add the text from each page to the text string
|
| 82 |
-
text += page.extract_text()
|
| 83 |
-
|
| 84 |
-
index = construct_index([Document(text)])
|
| 85 |
-
|
| 86 |
-
# Save index to Azure blob storage
|
| 87 |
-
file_name = os.path.basename(file.name)
|
| 88 |
-
index.storage_context.persist(f'gpt/storage_demo/{file_name}', fs=fs)
|
| 89 |
-
|
| 90 |
-
return ''
|
| 91 |
-
|
| 92 |
def extract_text(file):
|
| 93 |
# Open the PDF file in binary mode
|
| 94 |
with open(file.name, 'rb') as f:
|
|
@@ -133,7 +111,7 @@ def ask_ai_upload(doc, question):
|
|
| 133 |
|
| 134 |
# Define the query & the querying method
|
| 135 |
query_engine = index.as_query_engine(optimizer=SentenceEmbeddingOptimizer(percentile_cutoff=0.8), similarity_top_k=7)
|
| 136 |
-
query = 'Answer the question truthfully based on the text provided.
|
| 137 |
response = query_engine.query(query)
|
| 138 |
|
| 139 |
return response.response
|
|
@@ -158,7 +136,7 @@ def ask_ai_choose(doc, question):
|
|
| 158 |
|
| 159 |
# Define the query & the querying method
|
| 160 |
query_engine = index.as_query_engine(optimizer=SentenceEmbeddingOptimizer(percentile_cutoff=0.8), similarity_top_k=7)
|
| 161 |
-
query = 'Answer the question truthfully based on the text provided.
|
| 162 |
response = query_engine.query(query)
|
| 163 |
|
| 164 |
return response.response
|
|
@@ -191,14 +169,12 @@ with gr.Blocks(theme=theme) as demo:
|
|
| 191 |
|
| 192 |
with gr.Tab("Upload a document & ask a question π₯"):
|
| 193 |
upload_file = gr.inputs.File(label="Upload your PDF document")
|
| 194 |
-
upload_button = gr.Button("Save the document in Ardian Knowledge Library")
|
| 195 |
output = gr.Textbox(label='Output', visible=False)
|
| 196 |
chatbot = gr.Chatbot()
|
| 197 |
question = gr.Textbox(label='Question', info="Please write your question here.")
|
| 198 |
clear = gr.Button("Clear")
|
| 199 |
|
| 200 |
question.submit(respond_document_upload, [question, chatbot, upload_file], [question, chatbot])
|
| 201 |
-
upload_button.click(upload_doc, inputs=upload_file, outputs=output)
|
| 202 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 203 |
|
| 204 |
with gr.Tab("Choose a document & ask a question π"):
|
|
|
|
| 67 |
return index
|
| 68 |
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
def extract_text(file):
|
| 71 |
# Open the PDF file in binary mode
|
| 72 |
with open(file.name, 'rb') as f:
|
|
|
|
| 111 |
|
| 112 |
# Define the query & the querying method
|
| 113 |
query_engine = index.as_query_engine(optimizer=SentenceEmbeddingOptimizer(percentile_cutoff=0.8), similarity_top_k=7)
|
| 114 |
+
query = 'Answer the question truthfully based on the text provided. Use bullet points. After each quote, write a step by step explanation. Provide an answer as detailed and precise as possible. The task is:' + str(question)
|
| 115 |
response = query_engine.query(query)
|
| 116 |
|
| 117 |
return response.response
|
|
|
|
| 136 |
|
| 137 |
# Define the query & the querying method
|
| 138 |
query_engine = index.as_query_engine(optimizer=SentenceEmbeddingOptimizer(percentile_cutoff=0.8), similarity_top_k=7)
|
| 139 |
+
query = 'Answer the question truthfully based on the text provided. Use bullet points. After each quote, write a step by step explanation. Provide an answer as detailed and precise as possible. The task is:' + str(question)
|
| 140 |
response = query_engine.query(query)
|
| 141 |
|
| 142 |
return response.response
|
|
|
|
| 169 |
|
| 170 |
with gr.Tab("Upload a document & ask a question π₯"):
|
| 171 |
upload_file = gr.inputs.File(label="Upload your PDF document")
|
|
|
|
| 172 |
output = gr.Textbox(label='Output', visible=False)
|
| 173 |
chatbot = gr.Chatbot()
|
| 174 |
question = gr.Textbox(label='Question', info="Please write your question here.")
|
| 175 |
clear = gr.Button("Clear")
|
| 176 |
|
| 177 |
question.submit(respond_document_upload, [question, chatbot, upload_file], [question, chatbot])
|
|
|
|
| 178 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 179 |
|
| 180 |
with gr.Tab("Choose a document & ask a question π"):
|