Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -50,7 +50,7 @@ def construct_index(doc):
|
|
| 50 |
max_chunk_overlap = 1
|
| 51 |
|
| 52 |
# Set chunk overlap ratio
|
| 53 |
-
chunk_overlap_ratio = 0.
|
| 54 |
|
| 55 |
# Define prompt helper
|
| 56 |
prompt_helper = PromptHelper(max_input_size, num_output, max_chunk_overlap, chunk_size_limit, chunk_overlap_ratio)
|
|
@@ -64,11 +64,31 @@ def construct_index(doc):
|
|
| 64 |
## Indexation process and saving in the disk
|
| 65 |
index = GPTVectorStoreIndex.from_documents(doc, service_context=service_context)
|
| 66 |
|
| 67 |
-
# save index to disk
|
| 68 |
-
index.set_index_id("vector_index")
|
| 69 |
-
|
| 70 |
return index
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
def extract_text(file):
|
| 73 |
# Open the PDF file in binary mode
|
| 74 |
with open(file.name, 'rb') as f:
|
|
@@ -85,7 +105,8 @@ def extract_text(file):
|
|
| 85 |
|
| 86 |
return text, os.path.basename(file.name)
|
| 87 |
|
| 88 |
-
|
|
|
|
| 89 |
|
| 90 |
text, file_name = extract_text(doc)
|
| 91 |
index = construct_index([Document(text)])
|
|
@@ -103,45 +124,82 @@ def ask_ai(doc, question):
|
|
| 103 |
query_engine = index.as_query_engine(optimizer=SentenceEmbeddingOptimizer(percentile_cutoff=0.8))
|
| 104 |
query = 'Your task is to answer a question on the report loaded and give insights to an investment team in Infrastructure. Make your response as clear and precise as possible. The question is:' + str(question)
|
| 105 |
response = query_engine.query(query)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
-
#
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
return response.response
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
header = """<center><b><p style=\"color: #E13C32; font-size: 36px;\">My Ardian Chatbot</p></b></center>
|
| 117 |
<i><p style=\"font-size: 16px; color: grey;\">Please make sure to formulate clear and precise questions and to add contextual information when possible. This will help the tool produce the most relevant response. Adopt an iterative approach and ask for more details or explanations when necessary.</br><i/></p>"""
|
| 118 |
|
| 119 |
footnote = "<p style=\"font-size: 16px; color: grey;\"> ⚠ The chatbot doesn't have a memory, it doesn't remember what it previously generated.</a></p>"
|
| 120 |
|
| 121 |
theme = gr.themes.Base(
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
)
|
| 126 |
|
| 127 |
with gr.Blocks(theme=theme) as demo:
|
| 128 |
gr.Markdown(header)
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
demo.launch(auth=(os.environ['username'],os.environ['password']))
|
| 147 |
|
|
|
|
| 50 |
max_chunk_overlap = 1
|
| 51 |
|
| 52 |
# Set chunk overlap ratio
|
| 53 |
+
chunk_overlap_ratio = 0.2
|
| 54 |
|
| 55 |
# Define prompt helper
|
| 56 |
prompt_helper = PromptHelper(max_input_size, num_output, max_chunk_overlap, chunk_size_limit, chunk_overlap_ratio)
|
|
|
|
| 64 |
## Indexation process and saving in the disk
|
| 65 |
index = GPTVectorStoreIndex.from_documents(doc, service_context=service_context)
|
| 66 |
|
|
|
|
|
|
|
|
|
|
| 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:
|
|
|
|
| 105 |
|
| 106 |
return text, os.path.basename(file.name)
|
| 107 |
|
| 108 |
+
|
| 109 |
+
def ask_ai_upload(doc, question):
|
| 110 |
|
| 111 |
text, file_name = extract_text(doc)
|
| 112 |
index = construct_index([Document(text)])
|
|
|
|
| 124 |
query_engine = index.as_query_engine(optimizer=SentenceEmbeddingOptimizer(percentile_cutoff=0.8))
|
| 125 |
query = 'Your task is to answer a question on the report loaded and give insights to an investment team in Infrastructure. Make your response as clear and precise as possible. The question is:' + str(question)
|
| 126 |
response = query_engine.query(query)
|
| 127 |
+
|
| 128 |
+
return response.response
|
| 129 |
+
|
| 130 |
+
def respond_document_upload(message, chat_history, doc):
|
| 131 |
+
|
| 132 |
+
bot_message = ask_ai_upload(doc, message)
|
| 133 |
+
chat_history.append((message, bot_message))
|
| 134 |
+
time.sleep(2)
|
| 135 |
+
|
| 136 |
+
return "", chat_history
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
def ask_ai_choose(doc, question):
|
| 140 |
|
| 141 |
+
# Rebuild storage context
|
| 142 |
+
name_doc = str(doc)+'.pdf'
|
| 143 |
+
storage_context = StorageContext.from_defaults(persist_dir=f'gpt/storage_demo/{name_doc}', fs=fs)
|
| 144 |
+
|
| 145 |
+
# Load index
|
| 146 |
+
index = load_index_from_storage(storage_context)
|
| 147 |
+
|
| 148 |
+
# Define the query & the querying method
|
| 149 |
+
query_engine = index.as_query_engine(optimizer=SentenceEmbeddingOptimizer(percentile_cutoff=0.8))
|
| 150 |
+
query = 'Your task is to answer a question on the report loaded and give insights to an investment team in Infrastructure. Make your response as clear and precise as possible. The question is:' + str(question)
|
| 151 |
+
response = query_engine.query(query)
|
| 152 |
|
| 153 |
return response.response
|
| 154 |
|
| 155 |
+
def respond_document_choose(message, chat_history, doc):
|
| 156 |
+
|
| 157 |
+
bot_message = ask_ai_choose(doc, message)
|
| 158 |
+
chat_history.append((message, bot_message))
|
| 159 |
+
time.sleep(2)
|
| 160 |
+
|
| 161 |
+
return "", chat_history
|
| 162 |
+
|
| 163 |
+
# Retrieve the documents name whose indexes are stored
|
| 164 |
+
path_list = fs.ls('gpt/storage_demo')
|
| 165 |
+
documents_list = [Path(path).name[:-4] for path in path_list]
|
| 166 |
+
|
| 167 |
+
# Configure Gradio platform
|
| 168 |
+
|
| 169 |
header = """<center><b><p style=\"color: #E13C32; font-size: 36px;\">My Ardian Chatbot</p></b></center>
|
| 170 |
<i><p style=\"font-size: 16px; color: grey;\">Please make sure to formulate clear and precise questions and to add contextual information when possible. This will help the tool produce the most relevant response. Adopt an iterative approach and ask for more details or explanations when necessary.</br><i/></p>"""
|
| 171 |
|
| 172 |
footnote = "<p style=\"font-size: 16px; color: grey;\"> ⚠ The chatbot doesn't have a memory, it doesn't remember what it previously generated.</a></p>"
|
| 173 |
|
| 174 |
theme = gr.themes.Base(
|
| 175 |
+
primary_hue="red",
|
| 176 |
+
secondary_hue="gray",
|
| 177 |
+
font=['FuturaTOT', '=']
|
| 178 |
)
|
| 179 |
|
| 180 |
with gr.Blocks(theme=theme) as demo:
|
| 181 |
gr.Markdown(header)
|
| 182 |
|
| 183 |
+
with gr.Tab("Upload a document & ask a question 📥"):
|
| 184 |
+
upload_file = gr.inputs.File(label="Upload your PDF document")
|
| 185 |
+
upload_button = gr.Button("Save the document in Ardian Knowledge Library")
|
| 186 |
+
output = gr.Textbox(label='Output', visible=False)
|
| 187 |
+
chatbot = gr.Chatbot()
|
| 188 |
+
question = gr.Textbox(label='Question', info="Please write your question here.")
|
| 189 |
+
clear = gr.Button("Clear")
|
| 190 |
+
|
| 191 |
+
question.submit(respond_document_upload, [question, chatbot, upload_file], [question, chatbot])
|
| 192 |
+
upload_button.click(upload_doc, inputs=upload_file, outputs=output)
|
| 193 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 194 |
+
|
| 195 |
+
with gr.Tab("Choose a document & ask a question 📚"):
|
| 196 |
+
list_button = gr.Dropdown(documents_list, multiselect=False, label="Document", info="Please select the report you want to ask questions on.")
|
| 197 |
+
chatbot = gr.Chatbot()
|
| 198 |
+
question = gr.Textbox(label='Question', info="Please write your question here.")
|
| 199 |
+
clear = gr.Button("Clear")
|
| 200 |
+
|
| 201 |
+
question.submit(respond_document_choose, [question, chatbot, list_button], [question, chatbot])
|
| 202 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 203 |
|
| 204 |
demo.launch(auth=(os.environ['username'],os.environ['password']))
|
| 205 |
|