Dekode commited on
Commit
a0aac2d
·
verified ·
1 Parent(s): ffe87be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -23
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=1400, chunk_overlap=0)
14
  texts = text_splitter.split_documents(documents)
15
  docsearch = FAISS.from_documents(texts, embeddings)
16
 
17
  return docsearch
18
 
19
- def get_qa(vectorstore, llmb):
20
 
21
- qa = RetrievalQA.from_llm(
22
- llm=llmb,
23
- chain_type="stuff",
24
  retriever=vectorstore.as_retriever())
25
 
26
- return qa
27
 
28
- def get_response(qa, query):
29
-
30
- response = qa.run(query)
31
-
32
  return response
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  def main():
35
-
36
  st.title("BetterZila RAG Enabled LLM")
37
- llm = HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature":0.5, "max_length":512}, huggingfacehub_api_token = st.secrets["hf_token"])
38
- embeddings = HuggingFaceInstructEmbeddings(model_name="google/t5-v1_1-xl", model_kwargs = {'device': 'cpu'})
39
- vectorstore = make_vectorstore(embeddings)
40
- qa = get_qa(vectorstore, llm)
41
- queries = ["Can you give me an example from history where the enemy was crushed totally from the book?", "What's the point of making myself less accessible?", "Can you tell me the story of Queen Elizabeth I from this 48 laws of power book?"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  for query in queries:
43
- st.subheader(f"Query: {query}")
44
- response = get_response(qa, query)
45
- st.write(query)
46
- st.write(response)
47
- st.success("Responses generated!")
 
 
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()