Delete app.py
Browse files
app.py
DELETED
@@ -1,188 +0,0 @@
|
|
1 |
-
import warnings
|
2 |
-
warnings.filterwarnings("ignore")
|
3 |
-
|
4 |
-
import os
|
5 |
-
import glob
|
6 |
-
import textwrap
|
7 |
-
import time
|
8 |
-
|
9 |
-
import langchain
|
10 |
-
|
11 |
-
# Loaders
|
12 |
-
from langchain.document_loaders import PyPDFLoader, DirectoryLoader
|
13 |
-
|
14 |
-
# Splits
|
15 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
16 |
-
|
17 |
-
# Prompts
|
18 |
-
from langchain import PromptTemplate, LLMChain
|
19 |
-
|
20 |
-
# Vector stores
|
21 |
-
from langchain.vectorstores import FAISS
|
22 |
-
|
23 |
-
# Import HuggingFacePipeline from the new package
|
24 |
-
from langchain_huggingface import HuggingFacePipeline
|
25 |
-
from langchain.embeddings import HuggingFaceInstructEmbeddings
|
26 |
-
|
27 |
-
# Retrievers
|
28 |
-
from langchain.chains import RetrievalQA
|
29 |
-
|
30 |
-
import torch
|
31 |
-
import transformers
|
32 |
-
from transformers import (
|
33 |
-
AutoTokenizer, AutoModelForCausalLM,
|
34 |
-
pipeline
|
35 |
-
)
|
36 |
-
import gradio as gr
|
37 |
-
import locale
|
38 |
-
import shutil
|
39 |
-
|
40 |
-
# Clear transformers cache
|
41 |
-
transformers.logging.set_verbosity_error()
|
42 |
-
shutil.rmtree('./.cache', ignore_errors=True)
|
43 |
-
|
44 |
-
class CFG:
|
45 |
-
# LLMs configuration
|
46 |
-
model_name = 'llama2-13b-chat'
|
47 |
-
temperature = 0
|
48 |
-
top_p = 0.95
|
49 |
-
repetition_penalty = 1.15
|
50 |
-
|
51 |
-
# Text splitting configuration
|
52 |
-
split_chunk_size = 800
|
53 |
-
split_overlap = 0
|
54 |
-
|
55 |
-
# Embeddings configuration
|
56 |
-
embeddings_model_repo = 'sentence-transformers/all-MiniLM-L6-v2'
|
57 |
-
|
58 |
-
# Similar passages configuration
|
59 |
-
k = 6
|
60 |
-
|
61 |
-
# File paths configuration
|
62 |
-
PDFs_path = './'
|
63 |
-
Embeddings_path = './faiss-hp-sentence-transformers'
|
64 |
-
Output_folder = './rag-vectordb'
|
65 |
-
|
66 |
-
def get_model(model=CFG.model_name):
|
67 |
-
print('\nDownloading model: ', model, '\n\n')
|
68 |
-
|
69 |
-
model_repo = 'daryl149/llama-2-13b-chat-hf' if model == 'llama2-13b-chat' else None
|
70 |
-
|
71 |
-
if not model_repo:
|
72 |
-
raise ValueError("Model not implemented: " + model)
|
73 |
-
|
74 |
-
tokenizer = AutoTokenizer.from_pretrained(model_repo, use_fast=True)
|
75 |
-
model = AutoModelForCausalLM.from_pretrained(
|
76 |
-
model_repo,
|
77 |
-
device_map="auto",
|
78 |
-
offload_folder="./offload",
|
79 |
-
trust_remote_code=True
|
80 |
-
)
|
81 |
-
|
82 |
-
max_len = 2048
|
83 |
-
|
84 |
-
return tokenizer, model, max_len
|
85 |
-
|
86 |
-
def wrap_text_preserve_newlines(text, width=700):
|
87 |
-
lines = text.split('\n')
|
88 |
-
wrapped_lines = [textwrap.fill(line, width=width) for line in lines]
|
89 |
-
|
90 |
-
return '\n'.join(wrapped_lines)
|
91 |
-
|
92 |
-
def process_llm_response(llm_response):
|
93 |
-
ans = wrap_text_preserve_newlines(llm_response['result'])
|
94 |
-
|
95 |
-
sources_used = ' \n'.join(
|
96 |
-
[
|
97 |
-
f"{source.metadata['source'].split('/')[-1][:-4]} - page: {source.metadata['page']}"
|
98 |
-
for source in llm_response['source_documents']
|
99 |
-
]
|
100 |
-
)
|
101 |
-
|
102 |
-
return ans + '\n\nSources: \n' + sources_used
|
103 |
-
|
104 |
-
def llm_ans(query):
|
105 |
-
start = time.time()
|
106 |
-
|
107 |
-
llm_response = qa_chain.invoke(query)
|
108 |
-
ans = process_llm_response(llm_response)
|
109 |
-
|
110 |
-
end = time.time()
|
111 |
-
|
112 |
-
time_elapsed_str = f'\n\nTime elapsed: {int(round(end - start))} s'
|
113 |
-
|
114 |
-
return ans + time_elapsed_str
|
115 |
-
|
116 |
-
def predict(message, history):
|
117 |
-
output = str(llm_ans(message)).replace("\n", "<br/>")
|
118 |
-
return output
|
119 |
-
|
120 |
-
tokenizer, model, max_len = get_model(model=CFG.model_name)
|
121 |
-
|
122 |
-
pipe = pipeline(
|
123 |
-
task="text-generation",
|
124 |
-
model=model,
|
125 |
-
tokenizer=tokenizer,
|
126 |
-
pad_token_id=tokenizer.eos_token_id,
|
127 |
-
max_length=max_len,
|
128 |
-
temperature=CFG.temperature,
|
129 |
-
top_p=CFG.top_p,
|
130 |
-
repetition_penalty=CFG.repetition_penalty
|
131 |
-
)
|
132 |
-
|
133 |
-
# Use the updated HuggingFacePipeline class from langchain_huggingface
|
134 |
-
llm = HuggingFacePipeline(pipeline=pipe)
|
135 |
-
|
136 |
-
loader = DirectoryLoader(
|
137 |
-
CFG.PDFs_path,
|
138 |
-
glob="./*.pdf",
|
139 |
-
loader_cls=PyPDFLoader,
|
140 |
-
)
|
141 |
-
|
142 |
-
documents = loader.load()
|
143 |
-
text_splitter = RecursiveCharacterTextSplitter(
|
144 |
-
chunk_size=CFG.split_chunk_size,
|
145 |
-
chunk_overlap=CFG.split_overlap
|
146 |
-
)
|
147 |
-
|
148 |
-
texts = text_splitter.split_documents(documents)
|
149 |
-
|
150 |
-
vectordb = FAISS.from_documents(
|
151 |
-
texts,
|
152 |
-
HuggingFaceInstructEmbeddings(model_name='sentence-transformers/all-mpnet-base-v2')
|
153 |
-
)
|
154 |
-
|
155 |
-
# Persist vector database
|
156 |
-
vectordb.save_local(f"{CFG.Output_folder}/faiss_index_rag")
|
157 |
-
|
158 |
-
retriever = vectordb.as_retriever(search_kwargs={"k": CFG.k})
|
159 |
-
|
160 |
-
qa_chain = RetrievalQA.from_chain_type(
|
161 |
-
llm=llm,
|
162 |
-
chain_type="stuff",
|
163 |
-
)
|
164 |
-
|
165 |
-
prompt_template = """
|
166 |
-
Don't try to make up an answer; if you don't know just say that you don't know.
|
167 |
-
Answer in the same language the question was asked.
|
168 |
-
Use only the following pieces of context to answer the question at the end.
|
169 |
-
|
170 |
-
{context}
|
171 |
-
|
172 |
-
Question: {question}
|
173 |
-
Answer:"""
|
174 |
-
|
175 |
-
PROMPT = PromptTemplate(
|
176 |
-
template=prompt_template,
|
177 |
-
input_variables=["context", "question"]
|
178 |
-
)
|
179 |
-
|
180 |
-
locale.getpreferredencoding = lambda: "UTF-8"
|
181 |
-
|
182 |
-
demo = gr.ChatInterface(
|
183 |
-
fn=predict,
|
184 |
-
title=f'Open-Source LLM ({CFG.model_name}) Question Answering'
|
185 |
-
)
|
186 |
-
|
187 |
-
demo.queue()
|
188 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|