Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,19 +19,85 @@ from llama_index.indices.vector_store.base import GPTVectorStoreIndex
|
|
| 19 |
from adlfs import AzureBlobFileSystem
|
| 20 |
import time
|
| 21 |
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
|
|
|
|
|
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
header = """<center><b><p style=\"color: #E13C32; font-size: 36px;\">My Ardian Chatbot</p></b></center>
|
| 37 |
<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>"""
|
|
@@ -44,9 +110,6 @@ theme = gr.themes.Base(
|
|
| 44 |
font=['FuturaTOT', '=', '36px']
|
| 45 |
)
|
| 46 |
|
| 47 |
-
def ask_ai(doc,message):
|
| 48 |
-
return message
|
| 49 |
-
|
| 50 |
with gr.Blocks(theme=theme) as demo:
|
| 51 |
gr.Markdown(header)
|
| 52 |
|
|
@@ -66,4 +129,6 @@ with gr.Blocks(theme=theme) as demo:
|
|
| 66 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 67 |
gr.Markdown(footnote)
|
| 68 |
|
| 69 |
-
demo.launch(auth=("
|
|
|
|
|
|
|
|
|
| 19 |
from adlfs import AzureBlobFileSystem
|
| 20 |
import time
|
| 21 |
|
| 22 |
+
def construct_index(doc):
|
| 23 |
|
| 24 |
+
## Define the prompt helper
|
| 25 |
+
# Set maximum input size
|
| 26 |
+
max_input_size = 400
|
| 27 |
|
| 28 |
+
# Set number of output tokens
|
| 29 |
+
num_output = 400 # About 300 words
|
| 30 |
|
| 31 |
+
#Set the chunk size limit
|
| 32 |
+
chunk_size_limit = 600 # About 450 words ~ 1 page
|
| 33 |
+
|
| 34 |
+
# Set maximum chunk overlap
|
| 35 |
+
max_chunk_overlap = 1
|
| 36 |
+
|
| 37 |
+
# Set chunk overlap ratio
|
| 38 |
+
chunk_overlap_ratio = 0.5
|
| 39 |
+
|
| 40 |
+
# Define prompt helper
|
| 41 |
+
prompt_helper = PromptHelper(max_input_size, num_output, max_chunk_overlap, chunk_size_limit, chunk_overlap_ratio)
|
| 42 |
+
|
| 43 |
+
## Define the LLM predictor
|
| 44 |
+
llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.4, model_name="gpt-4-32k", max_tokens=num_output))
|
| 45 |
+
|
| 46 |
+
## Define Service Context
|
| 47 |
+
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper)
|
| 48 |
|
| 49 |
+
## Indexation process and saving in the disk
|
| 50 |
+
index = GPTVectorStoreIndex.from_documents(doc, service_context=service_context)
|
| 51 |
|
| 52 |
+
# save index to disk
|
| 53 |
+
index.set_index_id("vector_index")
|
| 54 |
+
index.storage_context.persist('/dbfs/gpttest')
|
| 55 |
+
|
| 56 |
+
return index
|
| 57 |
+
|
| 58 |
+
def extract_text(file):
|
| 59 |
+
# Open the PDF file in binary mode
|
| 60 |
+
with open(file.name, 'rb') as f:
|
| 61 |
+
# Initialize a PDF file reader object
|
| 62 |
+
pdf_reader = PdfReader(f)
|
| 63 |
+
|
| 64 |
+
# Initialize an empty string for storing the extracted text
|
| 65 |
+
text = ''
|
| 66 |
+
|
| 67 |
+
# Loop through the number of pages
|
| 68 |
+
for page in pdf_reader.pages:
|
| 69 |
+
# Add the text from each page to the text string
|
| 70 |
+
text += page.extract_text()
|
| 71 |
+
|
| 72 |
+
return text, os.path.basename(file.name)
|
| 73 |
+
|
| 74 |
+
def ask_ai(doc, question):
|
| 75 |
+
|
| 76 |
+
text, file_name = extract_text(doc)
|
| 77 |
+
index = construct_index([Document(text)])
|
| 78 |
+
|
| 79 |
+
# Save index to Azure blob storage
|
| 80 |
+
index.storage_context.persist(f'gpt/storage_demo/{file_name}', fs=fs)
|
| 81 |
+
|
| 82 |
+
# Rebuild storage context
|
| 83 |
+
storage_context = StorageContext.from_defaults(persist_dir=f'gpt/storage_demo/{file_name}', fs=fs)
|
| 84 |
+
|
| 85 |
+
# Load index
|
| 86 |
+
index = load_index_from_storage(storage_context)
|
| 87 |
+
|
| 88 |
+
# Define the query & the querying method
|
| 89 |
+
query_engine = index.as_query_engine(optimizer=SentenceEmbeddingOptimizer(percentile_cutoff=0.8))
|
| 90 |
+
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)
|
| 91 |
+
response = query_engine.query(query)
|
| 92 |
+
|
| 93 |
+
# Display the chunks retrieved to produce the response
|
| 94 |
+
sources = []
|
| 95 |
+
for node in response.source_nodes:
|
| 96 |
+
node_text_start= 'START: ' + node.node.text.strip().replace('\n', ' ')[:100]
|
| 97 |
+
node_text_end = 'END: ' + node.node.text.strip().replace('\n', ' ')[-100:]
|
| 98 |
+
sources.append((node_text_start, node_text_end))
|
| 99 |
+
|
| 100 |
+
return response.response
|
| 101 |
|
| 102 |
header = """<center><b><p style=\"color: #E13C32; font-size: 36px;\">My Ardian Chatbot</p></b></center>
|
| 103 |
<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>"""
|
|
|
|
| 110 |
font=['FuturaTOT', '=', '36px']
|
| 111 |
)
|
| 112 |
|
|
|
|
|
|
|
|
|
|
| 113 |
with gr.Blocks(theme=theme) as demo:
|
| 114 |
gr.Markdown(header)
|
| 115 |
|
|
|
|
| 129 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 130 |
gr.Markdown(footnote)
|
| 131 |
|
| 132 |
+
demo.launch(auth=("username", "password"), debug=True)
|
| 133 |
+
|
| 134 |
+
|