Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,44 +7,81 @@ from langchain.chains import RetrievalQA
|
|
7 |
from langchain_community.llms import HuggingFaceHub
|
8 |
|
9 |
def make_vectorstore(embeddings):
|
10 |
-
|
11 |
-
loader = PyPDFDirectoryLoader("data")
|
12 |
documents = loader.load()
|
13 |
-
text_splitter = CharacterTextSplitter(chunk_size=
|
14 |
texts = text_splitter.split_documents(documents)
|
15 |
docsearch = FAISS.from_documents(texts, embeddings)
|
16 |
|
17 |
return docsearch
|
18 |
|
19 |
-
def
|
20 |
|
21 |
-
|
22 |
-
llm=
|
23 |
-
chain_type="stuff",
|
24 |
retriever=vectorstore.as_retriever())
|
25 |
|
26 |
-
return
|
27 |
|
28 |
-
def get_response(
|
29 |
-
|
30 |
-
response =
|
31 |
-
|
32 |
return response
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
def main():
|
35 |
-
|
36 |
st.title("BetterZila RAG Enabled LLM")
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
for query in queries:
|
43 |
-
|
44 |
-
response
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
48 |
|
49 |
if __name__ == "__main__":
|
50 |
main()
|
|
|
7 |
from langchain_community.llms import HuggingFaceHub
|
8 |
|
9 |
def make_vectorstore(embeddings):
|
10 |
+
loader = PyPDFDirectoryLoader("/content/data")
|
|
|
11 |
documents = loader.load()
|
12 |
+
text_splitter = CharacterTextSplitter(chunk_size=400, chunk_overlap=0)
|
13 |
texts = text_splitter.split_documents(documents)
|
14 |
docsearch = FAISS.from_documents(texts, embeddings)
|
15 |
|
16 |
return docsearch
|
17 |
|
18 |
+
def get_conversation(vectorstore, model):
|
19 |
|
20 |
+
conversation_chain = RetrievalQA.from_llm(
|
21 |
+
llm=model,
|
22 |
+
# chain_type="stuff",
|
23 |
retriever=vectorstore.as_retriever())
|
24 |
|
25 |
+
return conversation_chain
|
26 |
|
27 |
+
def get_response(conversation_chain, query):
|
28 |
+
# get the response
|
29 |
+
response = conversation_chain.invoke(query)
|
|
|
30 |
return response
|
31 |
|
32 |
+
def response_formatter(resp_list):
|
33 |
+
queries = []
|
34 |
+
responses = []
|
35 |
+
for resp in resp_list:
|
36 |
+
# find the '\nQuestion: ' and '\nHelpful Answer: ' and take the text right in front of them a new list of query and responses
|
37 |
+
content = resp["result"]
|
38 |
+
# find '\nQuestion: ' in the text
|
39 |
+
question = content.split('\nQuestion: ')[1].split('\nHelpful Answer: ')[0]
|
40 |
+
queries.append(question)
|
41 |
+
# find '\nHelpful Answer: ' in the text
|
42 |
+
answer = content.split('\nHelpful Answer: ')[1]
|
43 |
+
responses.append(answer)
|
44 |
+
return queries, responses
|
45 |
+
|
46 |
def main():
|
|
|
47 |
st.title("BetterZila RAG Enabled LLM")
|
48 |
+
|
49 |
+
# a sidebar taht titles a disclaimer and a description of the app
|
50 |
+
st.sidebar.title("About")
|
51 |
+
st.sidebar.info("This app is a demo of BetterZila RAG Enabled LLM")
|
52 |
+
# write a paragraph in the sidebar
|
53 |
+
st.sidebar.write("This app is a demo of BetterZila RAG Enabled LLM")
|
54 |
+
|
55 |
+
response_list=[]
|
56 |
+
|
57 |
+
print("Downloading Embeddings Model")
|
58 |
+
with st.spinner('Downloading Embeddings Model...'):
|
59 |
+
embeddings = HuggingFaceInstructEmbeddings(model_name="hkunlp/instructor-base", model_kwargs = {'device': 'cpu'})
|
60 |
+
|
61 |
+
print("Loading LLM from HuggingFace")
|
62 |
+
with st.spinner('Loading LLM from HuggingFace...'):
|
63 |
+
llm = HuggingFaceHub(repo_id="HuggingFaceH4/zephyr-7b-beta", model_kwargs={"temperature":0.7, "max_new_tokens":512, "top_p":0.95, "top_k":50})
|
64 |
+
|
65 |
+
print("Creating Vector Database of PDF file content")
|
66 |
+
with st.spinner('Creating Vector Database of PDF file content...'):
|
67 |
+
vectorstore = make_vectorstore(embeddings)
|
68 |
+
|
69 |
+
print("Intializing LLM for inference with source material")
|
70 |
+
with st.spinner('Intializing LLM for inference with source material...'):
|
71 |
+
conversation_chain = get_conversation(vectorstore, llm)
|
72 |
+
|
73 |
+
queries = ["Can you give me an example from history where the enemy was crushed totally from the book?",
|
74 |
+
"What's the point of making myself less accessible?",
|
75 |
+
"Can you tell me the story of Queen Elizabeth I from this 48 laws of power book?"]
|
76 |
+
|
77 |
for query in queries:
|
78 |
+
response = get_response(conversation_chain, query)
|
79 |
+
response_list.append(response)
|
80 |
+
queries, responses = response_formatter(response_list)
|
81 |
+
for i in range(len(queries)):
|
82 |
+
st.write("Query: ", queries[i])
|
83 |
+
st.write("Response: ", responses[i])
|
84 |
+
st.write("--------------------------------------------------")
|
85 |
|
86 |
if __name__ == "__main__":
|
87 |
main()
|