Sean-Case
commited on
Commit
·
8aa3ebb
1
Parent(s):
6a76923
Minor dependency updates, increased max allowable layers for GPU
Browse files- README.md +1 -1
- app.py +13 -28
- chatfuncs/chatfuncs.py +7 -4
- chatfuncs/ingest.py +6 -24
- requirements.txt +3 -2
README.md
CHANGED
|
@@ -4,7 +4,7 @@ emoji: 🌍
|
|
| 4 |
colorFrom: yellow
|
| 5 |
colorTo: yellow
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 3.
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: apache-2.0
|
|
|
|
| 4 |
colorFrom: yellow
|
| 5 |
colorTo: yellow
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 3.50.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: apache-2.0
|
app.py
CHANGED
|
@@ -1,23 +1,17 @@
|
|
| 1 |
-
#
|
| 2 |
|
| 3 |
-
# +
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
from typing import TypeVar
|
| 11 |
-
from langchain.embeddings import HuggingFaceEmbeddings#, HuggingFaceInstructEmbeddings
|
| 12 |
-
from langchain.vectorstores import FAISS
|
| 13 |
import gradio as gr
|
|
|
|
| 14 |
|
| 15 |
from transformers import AutoTokenizer
|
| 16 |
-
|
| 17 |
-
# Alternative model sources
|
| 18 |
from ctransformers import AutoModelForCausalLM
|
| 19 |
|
| 20 |
-
PandasDataFrame =
|
| 21 |
|
| 22 |
# Disable cuda devices if necessary
|
| 23 |
#os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
|
|
@@ -29,16 +23,8 @@ import chatfuncs.ingest as ing
|
|
| 29 |
|
| 30 |
embeddings_name = "BAAI/bge-base-en-v1.5"
|
| 31 |
|
| 32 |
-
def load_embeddings(embeddings_name =
|
| 33 |
-
|
| 34 |
|
| 35 |
-
#if embeddings_name == "hkunlp/instructor-large":
|
| 36 |
-
# embeddings_func = HuggingFaceInstructEmbeddings(model_name=embeddings_name,
|
| 37 |
-
# embed_instruction="Represent the paragraph for retrieval: ",
|
| 38 |
-
# query_instruction="Represent the question for retrieving supporting documents: "
|
| 39 |
-
# )
|
| 40 |
-
|
| 41 |
-
#else:
|
| 42 |
embeddings_func = HuggingFaceEmbeddings(model_name=embeddings_name)
|
| 43 |
|
| 44 |
global embeddings
|
|
@@ -139,8 +125,8 @@ def load_model(model_type, gpu_layers, gpu_config=None, cpu_config=None, torch_d
|
|
| 139 |
return model_type, load_confirmation, model_type
|
| 140 |
|
| 141 |
# Both models are loaded on app initialisation so that users don't have to wait for the models to be downloaded
|
| 142 |
-
|
| 143 |
-
|
| 144 |
|
| 145 |
model_type = "Flan Alpaca (small, fast)"
|
| 146 |
load_model(model_type, 0, chatf.gpu_config, chatf.cpu_config, chatf.torch_device)
|
|
@@ -170,8 +156,8 @@ with block:
|
|
| 170 |
ingest_docs = gr.State()
|
| 171 |
|
| 172 |
model_type_state = gr.State(model_type)
|
| 173 |
-
embeddings_state = gr.State(globals()["embeddings"])
|
| 174 |
-
vectorstore_state = gr.State(globals()["vectorstore"])
|
| 175 |
|
| 176 |
model_state = gr.State() # chatf.model (gives error)
|
| 177 |
tokenizer_state = gr.State() # chatf.tokenizer (gives error)
|
|
@@ -213,8 +199,7 @@ with block:
|
|
| 213 |
"What are the 2030 outcomes for Lambeth?"])
|
| 214 |
|
| 215 |
|
| 216 |
-
current_topic = gr.Textbox(label="Feature currently disabled - Keywords related to current conversation topic.", placeholder="Keywords related to the conversation topic will appear here")
|
| 217 |
-
|
| 218 |
|
| 219 |
|
| 220 |
with gr.Tab("Load in a different file to chat with"):
|
|
@@ -242,7 +227,7 @@ with block:
|
|
| 242 |
model_choice = gr.Radio(label="Choose a chat model", value="Flan Alpaca (small, fast)", choices = ["Flan Alpaca (small, fast)", "Mistral Open Orca (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=
|
| 246 |
|
| 247 |
load_text = gr.Text(label="Load status")
|
| 248 |
|
|
|
|
| 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 |
from ctransformers import AutoModelForCausalLM
|
| 13 |
|
| 14 |
+
PandasDataFrame = Type[pd.DataFrame]
|
| 15 |
|
| 16 |
# Disable cuda devices if necessary
|
| 17 |
#os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
|
|
|
|
| 23 |
|
| 24 |
embeddings_name = "BAAI/bge-base-en-v1.5"
|
| 25 |
|
| 26 |
+
def load_embeddings(embeddings_name = embeddings_name):
|
|
|
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
embeddings_func = HuggingFaceEmbeddings(model_name=embeddings_name)
|
| 29 |
|
| 30 |
global embeddings
|
|
|
|
| 125 |
return model_type, load_confirmation, model_type
|
| 126 |
|
| 127 |
# Both models are loaded on app initialisation so that users don't have to wait for the models to be downloaded
|
| 128 |
+
model_type = "Mistral Open Orca (larger, slow)"
|
| 129 |
+
load_model(model_type, chatf.gpu_layers, chatf.gpu_config, chatf.cpu_config, chatf.torch_device)
|
| 130 |
|
| 131 |
model_type = "Flan Alpaca (small, fast)"
|
| 132 |
load_model(model_type, 0, chatf.gpu_config, chatf.cpu_config, chatf.torch_device)
|
|
|
|
| 156 |
ingest_docs = gr.State()
|
| 157 |
|
| 158 |
model_type_state = gr.State(model_type)
|
| 159 |
+
embeddings_state = gr.State(chatf.embeddings)#globals()["embeddings"])
|
| 160 |
+
vectorstore_state = gr.State(chatf.vectorstore)#globals()["vectorstore"])
|
| 161 |
|
| 162 |
model_state = gr.State() # chatf.model (gives error)
|
| 163 |
tokenizer_state = gr.State() # chatf.tokenizer (gives error)
|
|
|
|
| 199 |
"What are the 2030 outcomes for Lambeth?"])
|
| 200 |
|
| 201 |
|
| 202 |
+
current_topic = gr.Textbox(label="Feature currently disabled - Keywords related to current conversation topic.", placeholder="Keywords related to the conversation topic will appear here")
|
|
|
|
| 203 |
|
| 204 |
|
| 205 |
with gr.Tab("Load in a different file to chat with"):
|
|
|
|
| 227 |
model_choice = gr.Radio(label="Choose a chat model", value="Flan Alpaca (small, fast)", choices = ["Flan Alpaca (small, fast)", "Mistral Open Orca (larger, slow)"])
|
| 228 |
change_model_button = gr.Button(value="Load model", scale=0)
|
| 229 |
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):
|
| 230 |
+
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)
|
| 231 |
|
| 232 |
load_text = gr.Text(label="Load status")
|
| 233 |
|
chatfuncs/chatfuncs.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import re
|
| 2 |
import os
|
| 3 |
import datetime
|
| 4 |
-
from typing import
|
| 5 |
import time
|
| 6 |
from itertools import compress
|
| 7 |
import pandas as pd
|
|
@@ -17,8 +17,8 @@ from transformers import pipeline, TextIteratorStreamer
|
|
| 17 |
|
| 18 |
# Langchain functions
|
| 19 |
from langchain.prompts import PromptTemplate
|
| 20 |
-
from
|
| 21 |
-
from
|
| 22 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 23 |
from langchain.docstore.document import Document
|
| 24 |
|
|
@@ -42,7 +42,7 @@ import gradio as gr
|
|
| 42 |
|
| 43 |
torch.cuda.empty_cache()
|
| 44 |
|
| 45 |
-
PandasDataFrame =
|
| 46 |
|
| 47 |
embeddings = None # global variable setup
|
| 48 |
vectorstore = None # global variable setup
|
|
@@ -571,6 +571,8 @@ def hybrid_retrieval(new_question_kworded, vectorstore, embeddings, k_val, out_p
|
|
| 571 |
vec_rank = [*range(1, docs_keep_length+1)]
|
| 572 |
vec_score = [(docs_keep_length/x)*vec_weight for x in vec_rank]
|
| 573 |
|
|
|
|
|
|
|
| 574 |
# 2nd level check on retrieved docs with BM25
|
| 575 |
|
| 576 |
content_keep=[]
|
|
@@ -610,6 +612,7 @@ def hybrid_retrieval(new_question_kworded, vectorstore, embeddings, k_val, out_p
|
|
| 610 |
bm25_score.append((docs_keep_length/x)*bm25_weight)
|
| 611 |
|
| 612 |
# 3rd level check on retrieved docs with SVM retriever
|
|
|
|
| 613 |
svm_retriever = SVMRetriever.from_texts(content_keep, embeddings, k = k_val)
|
| 614 |
svm_result = svm_retriever.get_relevant_documents(new_question_kworded)
|
| 615 |
|
|
|
|
| 1 |
import re
|
| 2 |
import os
|
| 3 |
import datetime
|
| 4 |
+
from typing import Type, Dict, List, Tuple
|
| 5 |
import time
|
| 6 |
from itertools import compress
|
| 7 |
import pandas as pd
|
|
|
|
| 17 |
|
| 18 |
# Langchain functions
|
| 19 |
from langchain.prompts import PromptTemplate
|
| 20 |
+
from langchain_community.vectorstores import FAISS
|
| 21 |
+
from langchain_community.retrievers import SVMRetriever
|
| 22 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 23 |
from langchain.docstore.document import Document
|
| 24 |
|
|
|
|
| 42 |
|
| 43 |
torch.cuda.empty_cache()
|
| 44 |
|
| 45 |
+
PandasDataFrame = Type[pd.DataFrame]
|
| 46 |
|
| 47 |
embeddings = None # global variable setup
|
| 48 |
vectorstore = None # global variable setup
|
|
|
|
| 571 |
vec_rank = [*range(1, docs_keep_length+1)]
|
| 572 |
vec_score = [(docs_keep_length/x)*vec_weight for x in vec_rank]
|
| 573 |
|
| 574 |
+
print("Number of documents remaining: ", docs_keep_length)
|
| 575 |
+
|
| 576 |
# 2nd level check on retrieved docs with BM25
|
| 577 |
|
| 578 |
content_keep=[]
|
|
|
|
| 612 |
bm25_score.append((docs_keep_length/x)*bm25_weight)
|
| 613 |
|
| 614 |
# 3rd level check on retrieved docs with SVM retriever
|
| 615 |
+
|
| 616 |
svm_retriever = SVMRetriever.from_texts(content_keep, embeddings, k = k_val)
|
| 617 |
svm_result = svm_retriever.get_relevant_documents(new_question_kworded)
|
| 618 |
|
chatfuncs/ingest.py
CHANGED
|
@@ -1,21 +1,4 @@
|
|
| 1 |
-
#
|
| 2 |
-
# jupyter:
|
| 3 |
-
# jupytext:
|
| 4 |
-
# formats: ipynb,py:light
|
| 5 |
-
# text_representation:
|
| 6 |
-
# extension: .py
|
| 7 |
-
# format_name: light
|
| 8 |
-
# format_version: '1.5'
|
| 9 |
-
# jupytext_version: 1.14.6
|
| 10 |
-
# kernelspec:
|
| 11 |
-
# display_name: Python 3 (ipykernel)
|
| 12 |
-
# language: python
|
| 13 |
-
# name: python3
|
| 14 |
-
# ---
|
| 15 |
-
|
| 16 |
-
# # Ingest website to FAISS
|
| 17 |
-
|
| 18 |
-
# ## Install/ import stuff we need
|
| 19 |
|
| 20 |
import os
|
| 21 |
from pathlib import Path
|
|
@@ -23,11 +6,11 @@ import re
|
|
| 23 |
import requests
|
| 24 |
import pandas as pd
|
| 25 |
import dateutil.parser
|
| 26 |
-
from typing import
|
| 27 |
|
| 28 |
-
from
|
| 29 |
-
from
|
| 30 |
-
from
|
| 31 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 32 |
from langchain.docstore.document import Document
|
| 33 |
|
|
@@ -35,8 +18,7 @@ from bs4 import BeautifulSoup
|
|
| 35 |
from docx import Document as Doc
|
| 36 |
from pypdf import PdfReader
|
| 37 |
|
| 38 |
-
PandasDataFrame =
|
| 39 |
-
# -
|
| 40 |
|
| 41 |
split_strat = ["\n\n", "\n", ". ", "! ", "? "]
|
| 42 |
chunk_size = 500
|
|
|
|
| 1 |
+
# Import package
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
import os
|
| 4 |
from pathlib import Path
|
|
|
|
| 6 |
import requests
|
| 7 |
import pandas as pd
|
| 8 |
import dateutil.parser
|
| 9 |
+
from typing import Type, List
|
| 10 |
|
| 11 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings # HuggingFaceInstructEmbeddings,
|
| 12 |
+
from langchain_community.vectorstores.faiss import FAISS
|
| 13 |
+
#from langchain_community.vectorstores import Chroma
|
| 14 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 15 |
from langchain.docstore.document import Document
|
| 16 |
|
|
|
|
| 18 |
from docx import Document as Doc
|
| 19 |
from pypdf import PdfReader
|
| 20 |
|
| 21 |
+
PandasDataFrame = Type[pd.DataFrame]
|
|
|
|
| 22 |
|
| 23 |
split_strat = ["\n\n", "\n", ". ", "! ", "? "]
|
| 24 |
chunk_size = 500
|
requirements.txt
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
langchain
|
|
|
|
| 2 |
beautifulsoup4
|
| 3 |
pandas
|
| 4 |
transformers
|
| 5 |
-
--extra-index-url https://download.pytorch.org/whl/
|
| 6 |
torch
|
| 7 |
sentence_transformers
|
| 8 |
faiss-cpu
|
|
@@ -12,5 +13,5 @@ ctransformers[cuda]
|
|
| 12 |
keybert
|
| 13 |
span_marker
|
| 14 |
gensim
|
| 15 |
-
gradio==3.
|
| 16 |
gradio_client
|
|
|
|
| 1 |
langchain
|
| 2 |
+
langchain-community
|
| 3 |
beautifulsoup4
|
| 4 |
pandas
|
| 5 |
transformers
|
| 6 |
+
--extra-index-url https://download.pytorch.org/whl/cu118
|
| 7 |
torch
|
| 8 |
sentence_transformers
|
| 9 |
faiss-cpu
|
|
|
|
| 13 |
keybert
|
| 14 |
span_marker
|
| 15 |
gensim
|
| 16 |
+
gradio==3.50.0
|
| 17 |
gradio_client
|