seanpedrickcase commited on
Commit
a73f005
·
1 Parent(s): 5151113

Updated packages, functions, fixed a couple of bugs

Browse files
.gitignore CHANGED
@@ -4,7 +4,8 @@
4
  *.spec
5
  *.toc
6
  *.csv
 
7
  bootstrapper.py
8
  build/*
9
  dist/*
10
- Q tests/*
 
4
  *.spec
5
  *.toc
6
  *.csv
7
+ *.bin
8
  bootstrapper.py
9
  build/*
10
  dist/*
11
+ test/*
Dockerfile CHANGED
@@ -1,10 +1,23 @@
1
- FROM python:3.10
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  WORKDIR /src
4
 
5
  COPY requirements.txt .
6
 
7
- RUN pip install --no-cache-dir -r requirements.txt
 
 
8
 
9
  # Set up a new user named "user" with user ID 1000
10
  RUN useradd -m -u 1000 user
@@ -20,7 +33,7 @@ ENV HOME=/home/user \
20
  GRADIO_SERVER_NAME=0.0.0.0 \
21
  GRADIO_THEME=huggingface \
22
  SYSTEM=spaces \
23
- LLAMA_CUBLAS=1
24
 
25
  # Set the working directory to the user's home directory
26
  WORKDIR $HOME/app
 
1
+ FROM public.ecr.aws/docker/library/python:3.11.11-slim-bookworm
2
+
3
+ RUN apt-get update \
4
+ && apt-get install -y \
5
+ g++ \
6
+ make \
7
+ cmake \
8
+ unzip \
9
+ libcurl4-openssl-dev \
10
+ git \
11
+ && apt-get clean \
12
+ && rm -rf /var/lib/apt/lists/*
13
 
14
  WORKDIR /src
15
 
16
  COPY requirements.txt .
17
 
18
+ RUN pip install --no-cache-dir -r requirements_cpu.txt
19
+
20
+
21
 
22
  # Set up a new user named "user" with user ID 1000
23
  RUN useradd -m -u 1000 user
 
33
  GRADIO_SERVER_NAME=0.0.0.0 \
34
  GRADIO_THEME=huggingface \
35
  SYSTEM=spaces \
36
+ LLAMA_CUBLAS=0
37
 
38
  # Set the working directory to the user's home directory
39
  WORKDIR $HOME/app
README.md CHANGED
@@ -1,10 +1,10 @@
1
  ---
2
  title: Light PDF web QA chatbot
3
  emoji: 🌍
4
- colorFrom: yellow
5
  colorTo: yellow
6
  sdk: gradio
7
- sdk_version: 5.8.0
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
 
1
  ---
2
  title: Light PDF web QA chatbot
3
  emoji: 🌍
4
+ colorFrom: blue
5
  colorTo: yellow
6
  sdk: gradio
7
+ sdk_version: 5.25.2
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
app.py CHANGED
@@ -45,7 +45,7 @@ import chatfuncs.ingest as ing
45
  # Load preset embeddings, vectorstore, and model
46
  ###
47
 
48
- embeddings_name = "BAAI/bge-base-en-v1.5"
49
 
50
  def load_embeddings(embeddings_name = embeddings_name):
51
 
@@ -233,8 +233,8 @@ with app:
233
 
234
  with gr.Row():
235
  #chat_height = 500
236
- chatbot = gr.Chatbot(avatar_images=('user.jfif', 'bot.jpg'),bubble_full_width = False, scale = 1, type='tuples') # , height=chat_height
237
- with gr.Accordion("Open this tab to see the source paragraphs used to generate the answer", open = False):
238
  sources = gr.HTML(value = "Source paragraphs with the most relevant text will appear here") # , height=chat_height
239
 
240
  with gr.Row():
@@ -254,7 +254,7 @@ with app:
254
  "What are the commitments for Lambeth?",
255
  "What are the 2030 outcomes for Lambeth?"])
256
 
257
- current_topic = gr.Textbox(label="Feature currently disabled - Keywords related to current conversation topic.", placeholder="Keywords related to the conversation topic will appear here")
258
 
259
 
260
  with gr.Tab("Load in a different file to chat with"):
 
45
  # Load preset embeddings, vectorstore, and model
46
  ###
47
 
48
+ embeddings_name = "mixedbread-ai/mxbai-embed-xsmall-v1" #"BAAI/bge-base-en-v1.5"
49
 
50
  def load_embeddings(embeddings_name = embeddings_name):
51
 
 
233
 
234
  with gr.Row():
235
  #chat_height = 500
236
+ chatbot = gr.Chatbot(avatar_images=('user.jfif', 'bot.jpg'), scale = 1, resizable=True, show_copy_all_button=True, show_copy_button=True, show_share_button=True, type='tuples') # , height=chat_height
237
+ with gr.Accordion("Open this tab to see the source paragraphs used to generate the answer", open = True):
238
  sources = gr.HTML(value = "Source paragraphs with the most relevant text will appear here") # , height=chat_height
239
 
240
  with gr.Row():
 
254
  "What are the commitments for Lambeth?",
255
  "What are the 2030 outcomes for Lambeth?"])
256
 
257
+ current_topic = gr.Textbox(label="Feature currently disabled - Keywords related to current conversation topic.", placeholder="Keywords related to the conversation topic will appear here", visible=False)
258
 
259
 
260
  with gr.Tab("Load in a different file to chat with"):
app_save_docstore.py ADDED
@@ -0,0 +1,307 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Load in packages
2
+
3
+ import os
4
+
5
+ from typing import Type
6
+ from langchain_community.embeddings import HuggingFaceEmbeddings#, HuggingFaceInstructEmbeddings
7
+ from langchain_community.vectorstores import FAISS
8
+ import gradio as gr
9
+ import pandas as pd
10
+
11
+ from transformers import AutoTokenizer
12
+ import torch
13
+
14
+ from llama_cpp import Llama
15
+ from huggingface_hub import hf_hub_download
16
+
17
+ PandasDataFrame = Type[pd.DataFrame]
18
+
19
+ # Disable cuda devices if necessary
20
+ #os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
21
+
22
+ #from chatfuncs.chatfuncs import *
23
+ import chatfuncs.ingest as ing
24
+
25
+ ## Load preset embeddings, vectorstore, and model
26
+
27
+ embeddings_name = "BAAI/bge-base-en-v1.5"
28
+
29
+ def load_embeddings(embeddings_name = embeddings_name):
30
+
31
+ embeddings_func = HuggingFaceEmbeddings(model_name=embeddings_name)
32
+
33
+ global embeddings
34
+
35
+ embeddings = embeddings_func
36
+
37
+ return embeddings
38
+
39
+ def get_faiss_store(faiss_vstore_folder,embeddings):
40
+ import zipfile
41
+ with zipfile.ZipFile(faiss_vstore_folder + '/' + faiss_vstore_folder + '.zip', 'r') as zip_ref:
42
+ zip_ref.extractall(faiss_vstore_folder)
43
+
44
+ faiss_vstore = FAISS.load_local(folder_path=faiss_vstore_folder, embeddings=embeddings, allow_dangerous_deserialization=True)
45
+ os.remove(faiss_vstore_folder + "/index.faiss")
46
+ os.remove(faiss_vstore_folder + "/index.pkl")
47
+
48
+ global vectorstore
49
+
50
+ vectorstore = faiss_vstore
51
+
52
+ return vectorstore
53
+
54
+ import chatfuncs.chatfuncs as chatf
55
+
56
+ chatf.embeddings = load_embeddings(embeddings_name)
57
+ chatf.vectorstore = get_faiss_store(faiss_vstore_folder="faiss_embedding",embeddings=globals()["embeddings"])
58
+
59
+
60
+ def load_model(model_type, gpu_layers, gpu_config=None, cpu_config=None, torch_device=None):
61
+ print("Loading model")
62
+
63
+ # Default values inside the function
64
+ if gpu_config is None:
65
+ gpu_config = chatf.gpu_config
66
+ if cpu_config is None:
67
+ cpu_config = chatf.cpu_config
68
+ if torch_device is None:
69
+ torch_device = chatf.torch_device
70
+
71
+ if model_type == "Phi 3 Mini (larger, slow)":
72
+ if torch_device == "cuda":
73
+ gpu_config.update_gpu(gpu_layers)
74
+ print("Loading with", gpu_config.n_gpu_layers, "model layers sent to GPU.")
75
+ else:
76
+ gpu_config.update_gpu(gpu_layers)
77
+ cpu_config.update_gpu(gpu_layers)
78
+
79
+ print("Loading with", cpu_config.n_gpu_layers, "model layers sent to GPU.")
80
+
81
+ print(vars(gpu_config))
82
+ print(vars(cpu_config))
83
+
84
+ try:
85
+ model = Llama(
86
+ model_path=hf_hub_download(
87
+ repo_id=os.environ.get("REPO_ID", "QuantFactory/Phi-3-mini-128k-instruct-GGUF"),# "QuantFactory/Phi-3-mini-128k-instruct-GGUF"), # "QuantFactory/Meta-Llama-3-8B-Instruct-GGUF-v2"), #"microsoft/Phi-3-mini-4k-instruct-gguf"),#"TheBloke/Mistral-7B-OpenOrca-GGUF"),
88
+ filename=os.environ.get("MODEL_FILE", "Phi-3-mini-128k-instruct.Q4_K_M.gguf") #"Phi-3-mini-128k-instruct.Q4_K_M.gguf") #"Meta-Llama-3-8B-Instruct-v2.Q6_K.gguf") #"Phi-3-mini-4k-instruct-q4.gguf")#"mistral-7b-openorca.Q4_K_M.gguf"),
89
+ ),
90
+ **vars(gpu_config) # change n_gpu_layers if you have more or less VRAM
91
+ )
92
+
93
+ except Exception as e:
94
+ print("GPU load failed")
95
+ print(e)
96
+ model = Llama(
97
+ model_path=hf_hub_download(
98
+ repo_id=os.environ.get("REPO_ID", "QuantFactory/Phi-3-mini-128k-instruct-GGUF"), #"QuantFactory/Phi-3-mini-128k-instruct-GGUF"), #, "microsoft/Phi-3-mini-4k-instruct-gguf"),#"QuantFactory/Meta-Llama-3-8B-Instruct-GGUF-v2"), #"microsoft/Phi-3-mini-4k-instruct-gguf"),#"TheBloke/Mistral-7B-OpenOrca-GGUF"),
99
+ filename=os.environ.get("MODEL_FILE", "Phi-3-mini-128k-instruct.Q4_K_M.gguf"), # "Phi-3-mini-128k-instruct.Q4_K_M.gguf") # , #"Meta-Llama-3-8B-Instruct-v2.Q6_K.gguf") #"Phi-3-mini-4k-instruct-q4.gguf"),#"mistral-7b-openorca.Q4_K_M.gguf"),
100
+ ),
101
+ **vars(cpu_config)
102
+ )
103
+
104
+ tokenizer = []
105
+
106
+ if model_type == "Flan Alpaca (small, fast)":
107
+ # Huggingface chat model
108
+ hf_checkpoint = 'declare-lab/flan-alpaca-large'#'declare-lab/flan-alpaca-base' # # #
109
+
110
+ def create_hf_model(model_name):
111
+
112
+ from transformers import AutoModelForSeq2SeqLM, AutoModelForCausalLM
113
+
114
+ if torch_device == "cuda":
115
+ if "flan" in model_name:
116
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name, device_map="auto", torch_dtype=torch.float16)
117
+ else:
118
+ model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype=torch.float16)
119
+ else:
120
+ if "flan" in model_name:
121
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name, torch_dtype=torch.float16)
122
+ else:
123
+ model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True, torch_dtype=torch.float16)
124
+
125
+ tokenizer = AutoTokenizer.from_pretrained(model_name, model_max_length = chatf.context_length)
126
+
127
+ return model, tokenizer, model_type
128
+
129
+ model, tokenizer, model_type = create_hf_model(model_name = hf_checkpoint)
130
+
131
+ chatf.model = model
132
+ chatf.tokenizer = tokenizer
133
+ chatf.model_type = model_type
134
+
135
+ load_confirmation = "Finished loading model: " + model_type
136
+
137
+ print(load_confirmation)
138
+ return model_type, load_confirmation, model_type
139
+
140
+ # Both models are loaded on app initialisation so that users don't have to wait for the models to be downloaded
141
+ model_type = "Phi 3 Mini (larger, slow)"
142
+ load_model(model_type, chatf.gpu_layers, chatf.gpu_config, chatf.cpu_config, chatf.torch_device)
143
+
144
+ model_type = "Flan Alpaca (small, fast)"
145
+ load_model(model_type, 0, chatf.gpu_config, chatf.cpu_config, chatf.torch_device)
146
+
147
+ def docs_to_faiss_save(docs_out:PandasDataFrame, embeddings=embeddings):
148
+
149
+ print(f"> Total split documents: {len(docs_out)}")
150
+
151
+ print(docs_out)
152
+
153
+ vectorstore_func = FAISS.from_documents(documents=docs_out, embedding=embeddings)
154
+
155
+
156
+ chatf.vectorstore = vectorstore_func
157
+
158
+ out_message = "Document processing complete"
159
+
160
+ return out_message, vectorstore_func, out_file
161
+
162
+ # Gradio chat
163
+
164
+ block = gr.Blocks(theme = gr.themes.Base())#css=".gradio-container {background-color: black}")
165
+
166
+ with block:
167
+ ingest_text = gr.State()
168
+ ingest_metadata = gr.State()
169
+ ingest_docs = gr.State()
170
+
171
+ model_type_state = gr.State(model_type)
172
+ embeddings_state = gr.State(chatf.embeddings)#globals()["embeddings"])
173
+ vectorstore_state = gr.State(chatf.vectorstore)#globals()["vectorstore"])
174
+
175
+ model_state = gr.State() # chatf.model (gives error)
176
+ tokenizer_state = gr.State() # chatf.tokenizer (gives error)
177
+
178
+ chat_history_state = gr.State()
179
+ instruction_prompt_out = gr.State()
180
+
181
+ gr.Markdown("<h1><center>Lightweight PDF / web page QA bot</center></h1>")
182
+
183
+ gr.Markdown("Chat with PDF, web page or (new) csv/Excel documents. The default is a small model (Flan Alpaca), that can only answer specific questions that are answered in the text. It cannot give overall impressions of, or summarise the document. The alternative (Phi 3 Mini (larger, slow)), can reason a little better, but is much slower (See Advanced tab).\n\nBy default the Lambeth Borough Plan '[Lambeth 2030 : Our Future, Our Lambeth](https://www.lambeth.gov.uk/better-fairer-lambeth/projects/lambeth-2030-our-future-our-lambeth)' is loaded. If you want to talk about another document or web page, please select from the second tab. If switching topic, please click the 'Clear chat' button.\n\nCaution: This is a public app. Please ensure that the document you upload is not sensitive is any way as other users may see it! Also, please note that LLM chatbots may give incomplete or incorrect information, so please use with care.")
184
+
185
+ with gr.Row():
186
+ current_source = gr.Textbox(label="Current data source(s)", value="Lambeth_2030-Our_Future_Our_Lambeth.pdf", scale = 10)
187
+ current_model = gr.Textbox(label="Current model", value=model_type, scale = 3)
188
+
189
+ with gr.Tab("Chatbot"):
190
+
191
+ with gr.Row():
192
+ #chat_height = 500
193
+ chatbot = gr.Chatbot(avatar_images=('user.jfif', 'bot.jpg'),bubble_full_width = False, scale = 1) # , height=chat_height
194
+ with gr.Accordion("Open this tab to see the source paragraphs used to generate the answer", open = False):
195
+ sources = gr.HTML(value = "Source paragraphs with the most relevant text will appear here") # , height=chat_height
196
+
197
+ with gr.Row():
198
+ message = gr.Textbox(
199
+ label="Enter your question here",
200
+ lines=1,
201
+ )
202
+ with gr.Row():
203
+ submit = gr.Button(value="Send message", variant="secondary", scale = 1)
204
+ clear = gr.Button(value="Clear chat", variant="secondary", scale=0)
205
+ stop = gr.Button(value="Stop generating", variant="secondary", scale=0)
206
+
207
+ examples_set = gr.Radio(label="Examples for the Lambeth Borough Plan",
208
+ #value = "What were the five pillars of the previous borough plan?",
209
+ choices=["What were the five pillars of the previous borough plan?",
210
+ "What is the vision statement for Lambeth?",
211
+ "What are the commitments for Lambeth?",
212
+ "What are the 2030 outcomes for Lambeth?"])
213
+
214
+
215
+ current_topic = gr.Textbox(label="Feature currently disabled - Keywords related to current conversation topic.", placeholder="Keywords related to the conversation topic will appear here")
216
+
217
+
218
+ with gr.Tab("Load in a different file to chat with"):
219
+ with gr.Accordion("PDF file", open = False):
220
+ in_pdf = gr.File(label="Upload pdf", file_count="multiple", file_types=['.pdf'])
221
+ load_pdf = gr.Button(value="Load in file", variant="secondary", scale=0)
222
+
223
+ with gr.Accordion("Web page", open = False):
224
+ with gr.Row():
225
+ in_web = gr.Textbox(label="Enter web page url")
226
+ in_div = gr.Textbox(label="(Advanced) Web page div for text extraction", value="p", placeholder="p")
227
+ load_web = gr.Button(value="Load in webpage", variant="secondary", scale=0)
228
+
229
+ with gr.Accordion("CSV/Excel file", open = False):
230
+ in_csv = gr.File(label="Upload CSV/Excel file", file_count="multiple", file_types=['.csv', '.xlsx'])
231
+ in_text_column = gr.Textbox(label="Enter column name where text is stored")
232
+ load_csv = gr.Button(value="Load in CSV/Excel file", variant="secondary", scale=0)
233
+
234
+ with gr.Row():
235
+ ingest_embed_out = gr.Textbox(label="File/web page preparation progress")
236
+ out_file_box = gr.File(count='single', filetype=['.zip'])
237
+
238
+ with gr.Tab("Advanced features"):
239
+ out_passages = gr.Slider(minimum=1, value = 2, maximum=10, step=1, label="Choose number of passages to retrieve from the document. Numbers greater than 2 may lead to increased hallucinations or input text being truncated.")
240
+ temp_slide = gr.Slider(minimum=0.1, value = 0.5, maximum=1, step=0.1, label="Choose temperature setting for response generation.")
241
+ with gr.Row():
242
+ model_choice = gr.Radio(label="Choose a chat model", value="Flan Alpaca (small, fast)", choices = ["Flan Alpaca (small, fast)", "Phi 3 Mini (larger, slow)"])
243
+ change_model_button = gr.Button(value="Load model", scale=0)
244
+ with gr.Accordion("Choose number of model layers to send to GPU (WARNING: please don't modify unless you are sure you have a GPU).", open = False):
245
+ gpu_layer_choice = gr.Slider(label="Choose number of model layers to send to GPU.", value=0, minimum=0, maximum=100, step = 1, visible=True)
246
+
247
+ load_text = gr.Text(label="Load status")
248
+
249
+
250
+ gr.HTML(
251
+ "<center>This app is based on the models Flan Alpaca and Phi 3 Mini. It powered by Gradio, Transformers, and Llama.cpp.</a></center>"
252
+ )
253
+
254
+ examples_set.change(fn=chatf.update_message, inputs=[examples_set], outputs=[message])
255
+
256
+ change_model_button.click(fn=chatf.turn_off_interactivity, inputs=[message, chatbot], outputs=[message, chatbot], queue=False).\
257
+ then(fn=load_model, inputs=[model_choice, gpu_layer_choice], outputs = [model_type_state, load_text, current_model]).\
258
+ then(lambda: chatf.restore_interactivity(), None, [message], queue=False).\
259
+ then(chatf.clear_chat, inputs=[chat_history_state, sources, message, current_topic], outputs=[chat_history_state, sources, message, current_topic]).\
260
+ then(lambda: None, None, chatbot, queue=False)
261
+
262
+ # Load in a pdf
263
+ load_pdf_click = load_pdf.click(ing.parse_file, inputs=[in_pdf], outputs=[ingest_text, current_source]).\
264
+ then(ing.text_to_docs, inputs=[ingest_text], outputs=[ingest_docs]).\
265
+ then(docs_to_faiss_save, inputs=[ingest_docs], outputs=[ingest_embed_out, vectorstore_state, file_out_box]).\
266
+ then(chatf.hide_block, outputs = [examples_set])
267
+
268
+ # Load in a webpage
269
+ load_web_click = load_web.click(ing.parse_html, inputs=[in_web, in_div], outputs=[ingest_text, ingest_metadata, current_source]).\
270
+ then(ing.html_text_to_docs, inputs=[ingest_text, ingest_metadata], outputs=[ingest_docs]).\
271
+ then(docs_to_faiss_save, inputs=[ingest_docs], outputs=[ingest_embed_out, vectorstore_state, file_out_box]).\
272
+ then(chatf.hide_block, outputs = [examples_set])
273
+
274
+ # Load in a csv/excel file
275
+ load_csv_click = load_csv.click(ing.parse_csv_or_excel, inputs=[in_csv, in_text_column], outputs=[ingest_text, current_source]).\
276
+ then(ing.csv_excel_text_to_docs, inputs=[ingest_text, in_text_column], outputs=[ingest_docs]).\
277
+ then(docs_to_faiss_save, inputs=[ingest_docs], outputs=[ingest_embed_out, vectorstore_state, file_out_box]).\
278
+ then(chatf.hide_block, outputs = [examples_set])
279
+
280
+ # Load in a webpage
281
+
282
+ # Click/enter to send message action
283
+ response_click = submit.click(chatf.create_full_prompt, inputs=[message, chat_history_state, current_topic, vectorstore_state, embeddings_state, model_type_state, out_passages], outputs=[chat_history_state, sources, instruction_prompt_out], queue=False, api_name="retrieval").\
284
+ then(chatf.turn_off_interactivity, inputs=[message, chatbot], outputs=[message, chatbot], queue=False).\
285
+ then(chatf.produce_streaming_answer_chatbot, inputs=[chatbot, instruction_prompt_out, model_type_state, temp_slide], outputs=chatbot)
286
+ response_click.then(chatf.highlight_found_text, [chatbot, sources], [sources]).\
287
+ then(chatf.add_inputs_answer_to_history,[message, chatbot, current_topic], [chat_history_state, current_topic]).\
288
+ then(lambda: chatf.restore_interactivity(), None, [message], queue=False)
289
+
290
+ response_enter = message.submit(chatf.create_full_prompt, inputs=[message, chat_history_state, current_topic, vectorstore_state, embeddings_state, model_type_state, out_passages], outputs=[chat_history_state, sources, instruction_prompt_out], queue=False).\
291
+ then(chatf.turn_off_interactivity, inputs=[message, chatbot], outputs=[message, chatbot], queue=False).\
292
+ then(chatf.produce_streaming_answer_chatbot, [chatbot, instruction_prompt_out, model_type_state, temp_slide], chatbot)
293
+ response_enter.then(chatf.highlight_found_text, [chatbot, sources], [sources]).\
294
+ then(chatf.add_inputs_answer_to_history,[message, chatbot, current_topic], [chat_history_state, current_topic]).\
295
+ then(lambda: chatf.restore_interactivity(), None, [message], queue=False)
296
+
297
+ # Stop box
298
+ stop.click(fn=None, inputs=None, outputs=None, cancels=[response_click, response_enter])
299
+
300
+ # Clear box
301
+ clear.click(chatf.clear_chat, inputs=[chat_history_state, sources, message, current_topic], outputs=[chat_history_state, sources, message, current_topic])
302
+ clear.click(lambda: None, None, chatbot, queue=False)
303
+
304
+ # Thumbs up or thumbs down voting function
305
+ chatbot.like(chatf.vote, [chat_history_state, instruction_prompt_out, model_type_state], None)
306
+
307
+ block.queue().launch(debug=True)
chatfuncs/aws_functions.py CHANGED
@@ -10,15 +10,15 @@ PandasDataFrame = Type[pd.DataFrame]
10
  # Get AWS credentials if required
11
  bucket_name=""
12
 
13
- aws_var_val = get_or_create_env_var("RUN_AWS_FUNCTIONS", "1")
14
- print(f'The value of RUN_AWS_FUNCTIONS is {aws_var_val}')
15
 
16
- AWS_REGION = get_or_create_env_var('AWS_REGION', 'eu-west-2')
17
  print(f'The value of AWS_REGION is {AWS_REGION}')
18
 
19
- if aws_var_val == "1":
20
  try:
21
- bucket_name = os.environ['CONSULTATION_SUMMARY_BUCKET']
22
  session = boto3.Session() # profile_name="default"
23
  except Exception as e:
24
  print(e)
@@ -111,57 +111,6 @@ def download_files_from_s3(bucket_name, s3_folder, local_folder, filenames):
111
  except Exception as e:
112
  print(f"Error downloading 's3://{bucket_name}/{object_key}':", e)
113
 
114
- def load_data_from_aws(in_aws_keyword_file, aws_password="", bucket_name=bucket_name):
115
-
116
- temp_dir = tempfile.mkdtemp()
117
- local_address_stub = temp_dir + '/doc-redaction/'
118
- files = []
119
-
120
- if not 'LAMBETH_BOROUGH_PLAN_PASSWORD' in os.environ:
121
- out_message = "Can't verify password for dataset access. Do you have a valid AWS connection? Data not loaded."
122
- return files, out_message
123
-
124
- if aws_password:
125
- if "Lambeth borough plan" in in_aws_keyword_file and aws_password == os.environ['LAMBETH_BOROUGH_PLAN_PASSWORD']:
126
-
127
- s3_folder_stub = 'example-data/lambeth-borough-plan/latest/'
128
-
129
- local_folder_path = local_address_stub
130
-
131
- # Check if folder exists
132
- if not os.path.exists(local_folder_path):
133
- print(f"Folder {local_folder_path} does not exist! Making folder.")
134
-
135
- os.mkdir(local_folder_path)
136
-
137
- # Check if folder is empty
138
- if len(os.listdir(local_folder_path)) == 0:
139
- print(f"Folder {local_folder_path} is empty")
140
- # Download data
141
- download_files_from_s3(bucket_name, s3_folder_stub, local_folder_path, filenames='*')
142
-
143
- print("AWS data downloaded")
144
-
145
- else:
146
- print(f"Folder {local_folder_path} is not empty")
147
-
148
- #files = os.listdir(local_folder_stub)
149
- #print(files)
150
-
151
- files = [os.path.join(local_folder_path, f) for f in os.listdir(local_folder_path) if os.path.isfile(os.path.join(local_folder_path, f))]
152
-
153
- out_message = "Data successfully loaded from AWS"
154
- print(out_message)
155
-
156
- else:
157
- out_message = "Data not loaded from AWS"
158
- print(out_message)
159
- else:
160
- out_message = "No password provided. Please ask the data team for access if you need this."
161
- print(out_message)
162
-
163
- return files, out_message
164
-
165
  def upload_file_to_s3(local_file_paths:List[str], s3_key:str, s3_bucket:str=bucket_name):
166
  """
167
  Uploads a file from local machine to Amazon S3.
 
10
  # Get AWS credentials if required
11
  bucket_name=""
12
 
13
+ RUN_AWS_FUNCTIONS = get_or_create_env_var("RUN_AWS_FUNCTIONS", "0")
14
+ print(f'The value of RUN_AWS_FUNCTIONS is {RUN_AWS_FUNCTIONS}')
15
 
16
+ AWS_REGION = get_or_create_env_var('AWS_REGION', '')
17
  print(f'The value of AWS_REGION is {AWS_REGION}')
18
 
19
+ if RUN_AWS_FUNCTIONS == "1":
20
  try:
21
+ bucket_name = os.environ['']
22
  session = boto3.Session() # profile_name="default"
23
  except Exception as e:
24
  print(e)
 
111
  except Exception as e:
112
  print(f"Error downloading 's3://{bucket_name}/{object_key}':", e)
113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  def upload_file_to_s3(local_file_paths:List[str], s3_key:str, s3_bucket:str=bucket_name):
115
  """
116
  Uploads a file from local machine to Amazon S3.
requirements.txt CHANGED
@@ -1,23 +1,24 @@
1
- langchain
2
- langchain-community
3
- langchain-huggingface
4
- beautifulsoup4
5
- google-generativeai==0.7.2
6
- pandas
7
  transformers==4.41.2
8
- torch --extra-index-url https://download.pytorch.org/whl/cu121
9
- llama-cpp-python --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cu121
10
- sentence_transformers==3.0.1
11
- faiss-cpu==1.7.4
12
- pypdf
13
- python-docx
14
- keybert
15
- span_marker
16
- #gensim
17
- gradio
18
- nltk
19
- bm25s
20
- PyStemmer
21
- scipy<1.13
 
22
  numpy==1.26.4
23
- boto3
 
1
+ langchain==0.3.24
2
+ langchain-huggingface==0.1.2
3
+ langchain-community==0.3.22
4
+ beautifulsoup4==4.13.4
5
+ google-generativeai==0.8.5
6
+ pandas==2.2.3
7
  transformers==4.41.2
8
+ # For Windows https://github.com/abetlen/llama-cpp-python/releases/download/v0.3.2/llama_cpp_python-0.3.2-cp311-cp311-win_amd64.whl -C cmake.args="-DGGML_BLAS=ON;-DGGML_BLAS_VENDOR=OpenBLAS"
9
+ llama-cpp-python==0.3.2 --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cpu
10
+ #-C cmake.args="-DGGML_BLAS=ON;-DGGML_BLAS_VENDOR=OpenBLAS"
11
+ torch==2.5.1 --extra-index-url https://download.pytorch.org/whl/cpu
12
+ sentence_transformers==4.1.0
13
+ faiss-cpu==1.10.0
14
+ pypdf==5.4.0
15
+ python-docx==1.1.2
16
+ keybert==0.9.0
17
+ span-marker==1.7.0
18
+ gradio==5.25.2
19
+ nltk==3.9.1
20
+ bm25s==0.2.12
21
+ PyStemmer==2.2.0.3
22
+ scipy==1.15.2
23
  numpy==1.26.4
24
+ boto3==1.38.0
requirements_cpu.txt DELETED
@@ -1,23 +0,0 @@
1
- langchain
2
- langchain-huggingface
3
- langchain-community
4
- beautifulsoup4
5
- google-generativeai==0.7.2
6
- pandas
7
- transformers==4.41.2
8
- llama-cpp-python --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cpu
9
- torch==2.3.1
10
- sentence_transformers==3.0.1
11
- faiss-cpu==1.7.4
12
- pypdf
13
- python-docx
14
- keybert
15
- span_marker
16
- #gensim
17
- gradio
18
- nltk
19
- bm25s
20
- PyStemmer
21
- scipy<1.13
22
- numpy==1.26.4
23
- boto3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements_gpu.txt ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ langchain==0.3.24
2
+ langchain-community==0.3.22
3
+ langchain-huggingface==0.1.2
4
+ beautifulsoup4==4.13.4
5
+ google-generativeai==0.8.5
6
+ pandas==2.2.3
7
+ transformers==4.51.3
8
+ torch==2.5.1 --extra-index-url https://download.pytorch.org/whl/cu121
9
+ llama-cpp-python==0.3.2 --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cu121
10
+ #llama-cpp-python==0.3.8 -C cmake.args="-DGGML_CUDA=on"
11
+ sentence_transformers==4.1.0
12
+ faiss-cpu==1.10.0
13
+ pypdf==5.4.0
14
+ python-docx==1.1.2
15
+ keybert==0.9.0
16
+ span-marker==1.7.0
17
+ gradio==5.25.2
18
+ nltk==3.9.1
19
+ bm25s==0.2.12
20
+ PyStemmer==2.2.0.3
21
+ scipy==1.15.2
22
+ numpy==1.26.4
23
+ boto3==1.38.0