Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,52 +8,43 @@ from langchain_community.llms import HuggingFaceHub
|
|
8 |
from langchain.memory import ConversationBufferMemory
|
9 |
|
10 |
def make_vectorstore(embeddings):
|
11 |
-
|
12 |
loader = PyPDFDirectoryLoader("data")
|
13 |
-
|
14 |
-
# load the documents
|
15 |
documents = loader.load()
|
16 |
-
|
17 |
-
# split the documents into chunks of 1400 characters with 0 overlap
|
18 |
text_splitter = CharacterTextSplitter(chunk_size=1400, chunk_overlap=0)
|
19 |
-
|
20 |
-
# split the documents into chunks of 1400 characters with 0 overlap
|
21 |
texts = text_splitter.split_documents(documents)
|
22 |
-
|
23 |
-
# create a vector store from the documents
|
24 |
docsearch = FAISS.from_documents(texts, embeddings)
|
25 |
|
26 |
return docsearch
|
27 |
|
28 |
-
def
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
conversation_chain = ConversationalRetrievalChain.from_chain_type(
|
34 |
-
llm=HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature":0.5, "max_length":512}, huggingfacehub_api_token = st.secrets["hf_token"]),
|
35 |
chain_type="stuff",
|
36 |
-
retriever=vectorstore.as_retriever()
|
37 |
-
memory=memory)
|
38 |
|
39 |
-
return
|
40 |
|
41 |
-
def get_response(
|
42 |
-
|
43 |
-
response =
|
|
|
44 |
return response
|
45 |
|
46 |
def main():
|
|
|
47 |
st.title("BetterZila RAG Enabled LLM")
|
|
|
48 |
embeddings = HuggingFaceInstructEmbeddings(model_name="google/t5-v1_1-xl", model_kwargs = {'device': 'cpu'})
|
49 |
vectorstore = make_vectorstore(embeddings)
|
50 |
-
|
51 |
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?"]
|
52 |
for query in queries:
|
53 |
st.subheader(f"Query: {query}")
|
54 |
-
response = get_response(
|
55 |
st.write(query)
|
56 |
-
st.write(response
|
57 |
st.success("Responses generated!")
|
58 |
|
59 |
if __name__ == "__main__":
|
|
|
8 |
from langchain.memory import ConversationBufferMemory
|
9 |
|
10 |
def make_vectorstore(embeddings):
|
11 |
+
|
12 |
loader = PyPDFDirectoryLoader("data")
|
|
|
|
|
13 |
documents = loader.load()
|
|
|
|
|
14 |
text_splitter = CharacterTextSplitter(chunk_size=1400, chunk_overlap=0)
|
|
|
|
|
15 |
texts = text_splitter.split_documents(documents)
|
|
|
|
|
16 |
docsearch = FAISS.from_documents(texts, embeddings)
|
17 |
|
18 |
return docsearch
|
19 |
|
20 |
+
def get_qa(vectorstore, llmb):
|
21 |
|
22 |
+
qa = RetrievalQA.from_chain_type(
|
23 |
+
llm=llmb,
|
|
|
|
|
|
|
24 |
chain_type="stuff",
|
25 |
+
retriever=vectorstore.as_retriever())
|
|
|
26 |
|
27 |
+
return qa
|
28 |
|
29 |
+
def get_response(qa, query):
|
30 |
+
|
31 |
+
response = qa.run(query)
|
32 |
+
|
33 |
return response
|
34 |
|
35 |
def main():
|
36 |
+
|
37 |
st.title("BetterZila RAG Enabled LLM")
|
38 |
+
llm = HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature":0.5, "max_length":512}, huggingfacehub_api_token = st.secrets["hf_token"])
|
39 |
embeddings = HuggingFaceInstructEmbeddings(model_name="google/t5-v1_1-xl", model_kwargs = {'device': 'cpu'})
|
40 |
vectorstore = make_vectorstore(embeddings)
|
41 |
+
qa = get_qa(vectorstore, llm)
|
42 |
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?"]
|
43 |
for query in queries:
|
44 |
st.subheader(f"Query: {query}")
|
45 |
+
response = get_response(qa, query)
|
46 |
st.write(query)
|
47 |
+
st.write(response)
|
48 |
st.success("Responses generated!")
|
49 |
|
50 |
if __name__ == "__main__":
|